commit 1563572d1de565ebbe8b8d4eaf9f538f9b68f355
parent f08763b74e32d83dd5d706ce2090b235ed518a89
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Fri, 30 Oct 2026 11:31:16 +0100
replace strncpy with snprintf()
Using snprintf() like this is similar to using strlcpy().
Truncation is silently ignored, which is fine here.
strncpy also pads the buffer with zeroes, which is not necesary here.
Diffstat:
| M | dwm.c | | | 14 | +++++--------- |
1 file changed, 5 insertions(+), 9 deletions(-)
diff --git a/dwm.c b/dwm.c
@@ -395,8 +395,7 @@ arrange(Monitor *m)
void
arrangemon(Monitor *m)
{
- strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof(m->ltsymbol) - 1);
- m->ltsymbol[sizeof(m->ltsymbol) - 1] = '\0';
+ snprintf(m->ltsymbol, sizeof(m->ltsymbol), "%s", m->lt[m->sellt]->symbol);
if (m->lt[m->sellt]->arrange)
m->lt[m->sellt]->arrange(m);
}
@@ -646,8 +645,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) - 1);
- m->ltsymbol[sizeof(m->ltsymbol) - 1] = '\0';
+ snprintf(m->ltsymbol, sizeof(m->ltsymbol), "%s", layouts[0].symbol);
return m;
}
@@ -923,13 +921,12 @@ gettextprop(Window w, Atom atom, char *text, unsigned int size)
if (!XGetTextProperty(dpy, w, &name, atom) || !name.nitems)
return 0;
if (name.encoding == XA_STRING) {
- strncpy(text, (char *)name.value, size - 1);
+ snprintf(text, size, "%s", (char *)name.value);
} else if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success) {
if (n > 0 && *list)
- strncpy(text, *list, size - 1);
+ snprintf(text, size, "%s", *list);
XFreeStringList(list);
}
- text[size - 1] = '\0';
XFree(name.value);
return 1;
}
@@ -1520,8 +1517,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) - 1);
- selmon->ltsymbol[sizeof(selmon->ltsymbol) - 1] = '\0';
+ snprintf(selmon->ltsymbol, sizeof(selmon->ltsymbol), "%s", selmon->lt[selmon->sellt]->symbol);
if (selmon->sel)
arrange(selmon);
else