[hackers] [dwm][PATCH RESEND 2/2] callables: Retain const-correctness for arg->v

From: Chris Down <chris_AT_chrisdown.name>
Date: Sun, 21 Aug 2022 23:08:56 +0100

The Arg union is generally passed to functions called from a Key or
Button. One of its members, "v", allows passing a `const void *` with
the receiving function determining the intent and casting as
appropriate.

There are a few places in the dwm code where we cast away the constness
of this pointer unnecessarily, potentially allowing its mutation. For
example, execvp's argv is a `char *const *`, but we just cast away the
outer const pointer.

The other case is in setlayout(), but that case is trivial: there's no
issue just casting the const void pointer to a `const Layout *`.
---
 dwm.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/dwm.c b/dwm.c
index 253aba7..4c0f679 100644
--- a/dwm.c
+++ b/dwm.c
_AT_@ -1504,7 +1504,7 @@ setlayout(const Arg *arg)
 	if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
 		selmon->sellt ^= 1;
 	if (arg && arg->v)
-		selmon->lt[selmon->sellt] = (Layout *)arg->v;
+		selmon->lt[selmon->sellt] = (const Layout *)arg->v;
 	strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
 	if (selmon->sel)
 		arrange(selmon);
_AT_@ -1642,11 +1642,12 @@ spawn(const Arg *arg)
 	if (arg->v == dmenucmd)
 		dmenumon[0] = '0' + selmon->num;
 	if (fork() == 0) {
+		char *const *cmd = arg->v;
 		if (dpy)
 			close(ConnectionNumber(dpy));
 		setsid();
-		execvp(((char **)arg->v)[0], (char **)arg->v);
-		die("dwm: execvp '%s' failed:", ((char **)arg->v)[0]);
+		execvp(cmd[0], cmd);
+		die("dwm: execvp '%s' failed:", cmd[0]);
 	}
 }
 
-- 
2.37.2
Received on Mon Aug 22 2022 - 00:08:56 CEST

This archive was generated by hypermail 2.3.0 : Mon Aug 22 2022 - 07:00:48 CEST