Re: [hackers] [dwm][PATCH] Add ' - 1' to size arg to avoid warnings with -O2

From: hiltjo=codemadness.org <Hiltjo>
Date: Wed, 28 Oct 2026 09:40:42 +0100

On Wed, Oct 28, 2026 at 12:02:46AM +0000, Vedant Sule via suckless hackers list wrote:
> On Gentoo I use -O2 to compile packages, which causes the compiler to
> enable some additional warnings, such as when the size argument of
> strncpy is equal to the size of the destination buffer. This patch
> avoids those warnings and makes strncpy calls more consistent with those
> on lines 921 and 923.
>
> Alternatively, we could replace all of these with snprintf.
> ---
> dwm.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/dwm.c b/dwm.c
> index ab3a84c..96fda94 100644
> --- a/dwm.c
> +++ b/dwm.c
> _at_@ -395,7 +395,7 @@ arrange(Monitor *m)
> void
> arrangemon(Monitor *m)
> {
> - strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
> + strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol - 1);
> if (m->lt[m->sellt]->arrange)
> m->lt[m->sellt]->arrange(m);
> }
> _at_@ -642,7 +642,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);
> + strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol - 1);
> return m;
> }
>
> _at_@ -1514,7 +1514,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);
> + strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol - 1);
> if (selmon->sel)
> arrange(selmon);
> else
> --
> 2.55.0
>
>

Thanks for the patch, it has recently been fixed already:
https://git.suckless.org/dwm/commit/ce41d3479103fe818466cde9394cce9ac314f519.html

(Theres not a new dwm release for it yet though)

-- 
Kind regards,
Hiltjo
Received on Wed Oct 28 2026 - 09:40:42 CET

This archive was generated by hypermail 2.3.0 : Wed Oct 28 2026 - 09:48:40 CET