Re: [hackers] [dwm][PATCH] ensure layout symbol is properly nul-terminated

From: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Sat, 13 Aug 2022 12:48:57 +0200

Yes it affects readability and I don't like the patch.

BTW please don't send HTML mail to this mailinglist.

Thanks,

On Sat, Aug 13, 2022 at 08:47:41AM +0200, Stein Gunnar Bakkeby wrote:
> Doesn't this affect readability? Would it be easier to make the layout
> symbol size 15 (one less than the monitor symbol)?
> On Sat, 13 Aug 2022, 05:00 NRK, <[1]nrk_AT_disroot.org> wrote:
>
> the dwm source code somewhat silently assumes that the symbol will
> fit
> into 16 bytes. but this may not be the case since users can define
> longer symbols in `config.h` (and there's no comment or note stating
> anything about the size requirement).
> this patch syncs the sizeof Layout->symbol and Monitor->ltsymbol to
> be
> the same which should emit a warning if the symbol in config.h is
> longer
> than 17 bytes.
> however, there's still an edge case where the symbol is exactly 17
> bytes
> (including the nul character), in which case there won't be any
> warnings. so use stpncpy and manually ensure nul termination.
> ---
> Â dwm.c | 10 +++++-----
> Â 1 file changed, 5 insertions(+), 5 deletions(-)
> diff --git a/dwm.c b/dwm.c
> index 967c9e8..86c4985 100644
> --- a/dwm.c
> +++ b/dwm.c
> _AT_@ -107,12 +107,12 @@ typedef struct {
> Â } Key;
> Â typedef struct {
> -Â Â Â Â const char *symbol;
> +Â Â Â Â const char symbol[16];
> Â Â Â Â void (*arrange)(Monitor *);
> Â } Layout;
> Â struct Monitor {
> -Â Â Â Â char ltsymbol[16];
> +Â Â Â Â char ltsymbol[sizeof ((Layout *)0)->symbol];
> Â Â Â Â float mfact;
> Â Â Â Â int nmaster;
> Â Â Â Â int num;
> _AT_@ -397,7 +397,7 @@ arrange(Monitor *m)
> Â void
> Â arrangemon(Monitor *m)
> Â {
> -Â Â Â Â strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof
> m->ltsymbol);
> +Â Â Â Â *stpncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof
> m->ltsymbol - 1) = '\0';
> Â Â Â Â if (m->lt[m->sellt]->arrange)
> Â Â Â Â Â Â Â Â m->lt[m->sellt]->arrange(m);
> Â }
> _AT_@ -644,7 +644,7 @@ createmon(void)
> Â Â Â Â m->topbar = topbar;
> Â Â Â Â m->lt[0] = &layouts[0];
> Â Â Â Â m->lt[1] = &layouts[1 % LENGTH(layouts)];
> -Â Â Â Â strncpy(m->ltsymbol, layouts[0].symbol, sizeof
> m->ltsymbol);
> +Â Â Â Â *stpncpy(m->ltsymbol, layouts[0].symbol, sizeof
> m->ltsymbol - 1) = '\0';
> Â Â Â Â return m;
> Â }
> _AT_@ -1507,7 +1507,7 @@ setlayout(const Arg *arg)
> Â Â Â Â Â Â Â Â selmon->sellt ^= 1;
> Â Â Â Â if (arg && arg->v)
> Â Â Â Â Â Â Â Â selmon->lt[selmon->sellt] = (Layout
> *)arg->v;
> -Â Â Â Â strncpy(selmon->ltsymbol,
> selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
> +Â Â Â Â *stpncpy(selmon->ltsymbol,
> selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol - 1) =
> '\0';
> Â Â Â Â if (selmon->sel)
> Â Â Â Â Â Â Â Â arrange(selmon);
> Â Â Â Â else
> --
> 2.35.1
>
> References
>
> 1. mailto:nrk_AT_disroot.org

-- 
Kind regards,
Hiltjo
Received on Sat Aug 13 2022 - 12:48:57 CEST

This archive was generated by hypermail 2.3.0 : Sat Aug 13 2022 - 13:00:39 CEST