Re: [hackers] [dwm][PATCH RESEND 0/2] Const-correctness fixes

From: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Mon, 22 Aug 2022 11:10:03 +0200

On Mon, Aug 22, 2022 at 02:01:25PM +0600, NRK wrote:
> On Sun, Aug 21, 2022 at 11:08:43PM +0100, Chris Down wrote:
> > I originally sent these a few years ago and the reply was that
> > const-correctness wasn't something cared about, but seeing other const
> > changes in dwm, and purely const-correctness improvements in
> > quark/st/libgrapheme, I figured maybe it's worth reevaluating and seeing
> > if they are of interest now.
>
> I would add the attached patch to the list as well.
>
> String literals in C are of the type `char []`, however modifying them
> is actually UB [0] and in practice it can even result in a segfault.
>
> The `-Wwrite-strings` flag treats string literals as `const char []` and
> thus warns about cases where a string literal gets assigned to a `char *`.
>
> P.S: I didn't add the flag to config.mk/CFLAGS in the patch since
> changing the default warning flags can be seen as an opinionated change.
>
> [0]: https://port70.net/~nsz/c/c99/n1256.html#6.4.5p6
>
> - NRK

> From 49148ee9fb0bba0e1e503f70016381a779672b36 Mon Sep 17 00:00:00 2001
> From: NRK <nrk_AT_disroot.org>
> Date: Mon, 22 Aug 2022 13:56:09 +0600
> Subject: [PATCH] fix const-correctness reguarding string literals
>
> String literals in C are of the type `char []`, however modifying them
> is actually UB [0] and in practice it can even result in a segfault.
>
> The `-Wwrite-strings` flag treats string literals as `const char []` and
> thus warns about cases where a string literal gets assigned to a `char *`.
>
> [0]: https://port70.net/~nsz/c/c99/n1256.html#6.4.5p6
> ---
> dwm.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/dwm.c b/dwm.c
> index 253aba7..55dd68c 100644
> --- a/dwm.c
> +++ b/dwm.c
> _AT_@ -1808,7 +1808,8 @@ updatebars(void)
> .background_pixmap = ParentRelative,
> .event_mask = ButtonPressMask|ExposureMask
> };
> - XClassHint ch = {"dwm", "dwm"};
> + char s[] = "dwm";
> + XClassHint ch = { s, s };
> for (m = mons; m; m = m->next) {
> if (m->barwin)
> continue;
> --
> 2.35.1
>

I think this is fine, even when it segfaults because we want it/assume to not
be modified.

For reference in Xutil.h:
typedef struct {
        char *res_name;
        char *res_class;
} XClassHint

-- 
Kind regards,
Hiltjo
Received on Mon Aug 22 2022 - 11:10:03 CEST

This archive was generated by hypermail 2.3.0 : Mon Aug 22 2022 - 11:12:37 CEST