From ac42dff6c1a55baf1f73a98dee9b6c4541b64e7b Mon Sep 17 00:00:00 2001 From: okan Date: Sat, 14 Feb 2015 18:24:12 +0000 Subject: [PATCH 1/6] simplify error messages; discussed with doug@ --- parse.y | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/parse.y b/parse.y index 235bca5..827472c 100644 --- a/parse.y +++ b/parse.y @@ -119,21 +119,21 @@ main : FONTNAME STRING { } | BORDERWIDTH NUMBER { if ($2 < 0 || $2 > UINT_MAX) { - yyerror("invalid borderwidth: %lld", $2); + yyerror("invalid borderwidth"); YYERROR; } conf->bwidth = $2; } | MOVEAMOUNT NUMBER { if ($2 < 0 || $2 > INT_MAX) { - yyerror("invalid movemount: %lld", $2); + yyerror("invalid movemount"); YYERROR; } conf->mamount = $2; } | SNAPDIST NUMBER { if ($2 < 0 || $2 > INT_MAX) { - yyerror("invalid snapdist: %lld", $2); + yyerror("invalid snapdist"); YYERROR; } conf->snapdist = $2; @@ -150,8 +150,8 @@ main : FONTNAME STRING { } | AUTOGROUP NUMBER STRING { if ($2 < 0 || $2 > 9) { + yyerror("invalid autogroup"); free($3); - yyerror("invalid autogroup: %lld", $2); YYERROR; } conf_autogroup(conf, $2, $3); @@ -176,8 +176,7 @@ main : FONTNAME STRING { $3 < 0 || $3 > INT_MAX || $4 < 0 || $4 > INT_MAX || $5 < 0 || $5 > INT_MAX) { - yyerror("invalid gap: %lld %lld %lld %lld", - $2, $3, $4, $5); + yyerror("invalid gap"); YYERROR; } conf->gap.top = $2; From bad97699f9142838adec5005dc7a44d6b5bb187e Mon Sep 17 00:00:00 2001 From: okan Date: Thu, 26 Mar 2015 21:41:43 +0000 Subject: [PATCH 2/6] Simplify key/mb binding moving argtype into flags and dropping another variable; removes the need to zero out struct binding, leaving a simple malloc. --- calmwm.h | 2 +- conf.c | 20 ++++++++------------ 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/calmwm.h b/calmwm.h index 0e5652a..1d1cbe5 100644 --- a/calmwm.h +++ b/calmwm.h @@ -84,6 +84,7 @@ #define CWM_NOGAP 0x0002 #define CWM_WIN 0x0001 +#define CWM_CMD 0x0002 #define CWM_QUIT 0x0000 #define CWM_RUNNING 0x0001 @@ -252,7 +253,6 @@ struct binding { unsigned int modmask; union press press; int flags; - int argtype; }; TAILQ_HEAD(keybinding_q, binding); TAILQ_HEAD(mousebinding_q, binding); diff --git a/conf.c b/conf.c index 9d3918d..e5191a3 100644 --- a/conf.c +++ b/conf.c @@ -491,11 +491,10 @@ conf_bind_kbd(struct conf *c, const char *bind, const char *cmd) { struct binding *kb; const char *key; - unsigned int i, mask; + unsigned int i; - kb = xcalloc(1, sizeof(*kb)); - key = conf_bind_getmask(bind, &mask); - kb->modmask |= mask; + kb = xmalloc(sizeof(*kb)); + key = conf_bind_getmask(bind, &kb->modmask); kb->press.keysym = XStringToKeysym(key); if (kb->press.keysym == NoSymbol) { @@ -519,15 +518,13 @@ conf_bind_kbd(struct conf *c, const char *bind, const char *cmd) kb->callback = name_to_func[i].handler; kb->flags = name_to_func[i].flags; kb->argument = name_to_func[i].argument; - kb->argtype |= ARG_INT; TAILQ_INSERT_TAIL(&c->keybindingq, kb, entry); return(1); } kb->callback = kbfunc_cmdexec; - kb->flags = 0; + kb->flags = CWM_CMD; kb->argument.c = xstrdup(cmd); - kb->argtype |= ARG_CHAR; TAILQ_INSERT_TAIL(&c->keybindingq, kb, entry); return(1); } @@ -543,7 +540,7 @@ conf_unbind_kbd(struct conf *c, struct binding *unbind) if (key->press.keysym == unbind->press.keysym) { TAILQ_REMOVE(&c->keybindingq, key, entry); - if (key->argtype & ARG_CHAR) + if (key->flags & CWM_CMD) free(key->argument.c); free(key); } @@ -555,11 +552,10 @@ conf_bind_mouse(struct conf *c, const char *bind, const char *cmd) { struct binding *mb; const char *button, *errstr; - unsigned int i, mask; + unsigned int i; - mb = xcalloc(1, sizeof(*mb)); - button = conf_bind_getmask(bind, &mask); - mb->modmask |= mask; + mb = xmalloc(sizeof(*mb)); + button = conf_bind_getmask(bind, &mb->modmask); mb->press.button = strtonum(button, Button1, Button5, &errstr); if (errstr) { From 5b64e1540c01fb9e24dde30e05308be3d4dfc86c Mon Sep 17 00:00:00 2001 From: okan Date: Sat, 28 Mar 2015 21:55:48 +0000 Subject: [PATCH 3/6] plug a leak --- xutil.c | 1 + 1 file changed, 1 insertion(+) diff --git a/xutil.c b/xutil.c index edc1426..85276ba 100644 --- a/xutil.c +++ b/xutil.c @@ -332,6 +332,7 @@ xu_ewmh_net_desktop_names(struct screen_ctx *sc) XChangeProperty(X_Dpy, sc->rootwin, ewmh[_NET_DESKTOP_NAMES], cwmh[UTF8_STRING], 8, PropModeReplace, (unsigned char *)p, len); + free(p); } /* Application Window Properties */ From 0bbe0ad98c7b8ed5d081747ee3bdb2dbf7a1b848 Mon Sep 17 00:00:00 2001 From: okan Date: Sat, 28 Mar 2015 22:09:10 +0000 Subject: [PATCH 4/6] Reshuffle and slightly simplify menu_complete_path(), removing an allocation, for tab-completion; checked by Alexander Polakov as well. --- menu.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/menu.c b/menu.c index f73bc5e..1026347 100644 --- a/menu.c +++ b/menu.c @@ -189,9 +189,7 @@ menu_complete_path(struct menu_ctx *mc) { struct menu *mi, *mr; struct menu_q menuq; - char *path = NULL; - path = xcalloc(1, sizeof(mr->text)); mr = xcalloc(1, sizeof(*mr)); TAILQ_INIT(&menuq); @@ -200,17 +198,15 @@ menu_complete_path(struct menu_ctx *mc) CWM_MENU_DUMMY, search_match_path_any, NULL)) != NULL) { mr->abort = mi->abort; mr->dummy = mi->dummy; - strlcpy(path, mi->text, sizeof(mi->text)); + if (mi->text[0] != '\0') + snprintf(mr->text, sizeof(mr->text), "%s \"%s\"", + mc->searchstr, mi->text); + else if (!mr->abort) + strlcpy(mr->text, mc->searchstr, sizeof(mr->text)); } menuq_clear(&menuq); - if (path[0] != '\0') - snprintf(mr->text, sizeof(mr->text), "%s \"%s\"", - mc->searchstr, path); - else if (!mr->abort) - strlcpy(mr->text, mc->searchstr, sizeof(mr->text)); - free(path); return(mr); } From a4a414b68bf8a59255ee68959863f4c5fdee85d9 Mon Sep 17 00:00:00 2001 From: okan Date: Sat, 28 Mar 2015 23:12:47 +0000 Subject: [PATCH 5/6] Introduce a xreallocarray and convert a few xcalloc instances that do not require zero'ing. --- calmwm.h | 1 + group.c | 2 +- xmalloc.c | 12 ++++++++++++ xutil.c | 8 ++++---- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/calmwm.h b/calmwm.h index 1d1cbe5..fd4b554 100644 --- a/calmwm.h +++ b/calmwm.h @@ -559,6 +559,7 @@ void u_spawn(char *); void *xcalloc(size_t, size_t); void *xmalloc(size_t); +void *xreallocarray(void *, size_t, size_t); char *xstrdup(const char *); int xasprintf(char **, const char *, ...) __attribute__((__format__ (printf, 2, 3))) diff --git a/group.c b/group.c index 834c2f7..7ab65f9 100644 --- a/group.c +++ b/group.c @@ -91,7 +91,7 @@ group_restack(struct group_ctx *gc) if (cc->stackingorder > highstack) highstack = cc->stackingorder; } - winlist = xcalloc((highstack + 1), sizeof(*winlist)); + winlist = xreallocarray(NULL, (highstack + 1), sizeof(*winlist)); /* Invert the stacking order for XRestackWindows(). */ TAILQ_FOREACH(cc, &gc->clientq, group_entry) { diff --git a/xmalloc.c b/xmalloc.c index 2db79ab..9cf824a 100644 --- a/xmalloc.c +++ b/xmalloc.c @@ -61,6 +61,18 @@ xcalloc(size_t no, size_t siz) return(p); } +void * +xreallocarray(void *ptr, size_t nmemb, size_t size) +{ + void *p; + + p = reallocarray(ptr, nmemb, size); + if (p == NULL) + errx(1, "xreallocarray: out of memory (new_size %zu bytes)", + nmemb * size); + return(p); +} + char * xstrdup(const char *str) { diff --git a/xutil.c b/xutil.c index 85276ba..c6248c4 100644 --- a/xutil.c +++ b/xutil.c @@ -220,7 +220,7 @@ xu_ewmh_net_client_list(struct screen_ctx *sc) if (i == 0) return; - winlist = xcalloc(i, sizeof(*winlist)); + winlist = xreallocarray(NULL, i, sizeof(*winlist)); TAILQ_FOREACH(cc, &sc->clientq, entry) winlist[j++] = cc->win; XChangeProperty(X_Dpy, sc->rootwin, ewmh[_NET_CLIENT_LIST], @@ -320,7 +320,7 @@ xu_ewmh_net_desktop_names(struct screen_ctx *sc) TAILQ_FOREACH(gc, &sc->groupq, entry) len += strlen(gc->name) + 1; - q = p = xcalloc(len, sizeof(*p)); + q = p = xreallocarray(NULL, len, sizeof(*p)); tlen = len; TAILQ_FOREACH(gc, &sc->groupq, entry) { @@ -357,7 +357,7 @@ xu_ewmh_get_net_wm_state(struct client_ctx *cc, int *n) (unsigned char **)&p)) <= 0) return(NULL); - state = xcalloc(*n, sizeof(Atom)); + state = xreallocarray(NULL, *n, sizeof(Atom)); (void)memcpy(state, p, *n * sizeof(Atom)); XFree((char *)p); @@ -444,7 +444,7 @@ xu_ewmh_set_net_wm_state(struct client_ctx *cc) int n, i, j; oatoms = xu_ewmh_get_net_wm_state(cc, &n); - atoms = xcalloc((n + _NET_WM_STATES_NITEMS), sizeof(Atom)); + atoms = xreallocarray(NULL, (n + _NET_WM_STATES_NITEMS), sizeof(Atom)); for (i = j = 0; i < n; i++) { if (oatoms[i] != ewmh[_NET_WM_STATE_STICKY] && oatoms[i] != ewmh[_NET_WM_STATE_MAXIMIZED_HORZ] && From 557cd382dcd973fd96605c3359e3d6dfa45a4fd9 Mon Sep 17 00:00:00 2001 From: okan Date: Sun, 29 Mar 2015 00:21:05 +0000 Subject: [PATCH 6/6] plug a leak --- client.c | 1 + 1 file changed, 1 insertion(+) diff --git a/client.c b/client.c index 5e9a267..3e01c0c 100644 --- a/client.c +++ b/client.c @@ -887,6 +887,7 @@ client_mwm_hints(struct client_ctx *cc) !(mwmh->decorations & MWM_DECOR_ALL) && !(mwmh->decorations & MWM_DECOR_BORDER)) cc->bwidth = 0; + XFree(mwmh); } }