From 1563572d1de565ebbe8b8d4eaf9f538f9b68f355 Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Fri, 30 Oct 2026 11:31:16 +0100 Subject: 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. --- dwm.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/dwm.c b/dwm.c index 35cce9a..604def2 100644 --- 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 -- cgit v1.3.1