commit bcb369a656d09ae2c2bb76d99622afc9ba33474b
parent a98965225be0bdad50d9711bbf0c54ef4d18888a
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Sat, 8 Dec 2018 13:36:42 +0100
check memory allocations, malloc+memset to calloc
- some consistency for strdup allocations.
- free allocates strings at exit too.
Signed-off-by: Christoph Lohmann <20h@r-36.net>
Diffstat:
1 file changed, 19 insertions(+), 14 deletions(-)
diff --git a/thingmenu.c b/thingmenu.c
@@ -663,26 +663,28 @@ main(int argc, char *argv[])
i++;
cmd = argv[i];
- entries = realloc(entries, sizeof(entries[0])*(++nentries));
- entries[nentries-1] = malloc(sizeof(*entries[0]));
- memset(entries[nentries-1], 0, sizeof(*entries[0]));
-
- entries[nentries-1]->label = strdup(label);
- if (entries[nentries-1]->label == NULL)
+ if (!(entries = realloc(entries, sizeof(entries[0])*(++nentries))))
+ die("realloc returned NULL");
+ if (!(entries[nentries-1] = calloc(1, sizeof(*entries[0]))))
+ die("calloc returned NULL");
+ if (!(entries[nentries-1]->label = strdup(label)))
die("strdup returned NULL\n");
- entries[nentries-1]->cmd = strdup(cmd);
- if (entries[nentries-1]->cmd == NULL)
+ if (!(entries[nentries-1]->cmd = strdup(cmd)))
die("strdup returned NULL\n");
+ entries[nentries-1]->forceexit = False;
}
if (nentries < 1)
usage();
if (addexit) {
- entries = realloc(entries, sizeof(entries[0])*(++nentries));
- entries[nentries-1] = malloc(sizeof(*entries[0]));
- memset(entries[nentries-1], 0, sizeof(*entries[0]));
- entries[nentries-1]->label = strdup("cancel");
- entries[nentries-1]->cmd = "exit";
+ if (!(entries = realloc(entries, sizeof(entries[0])*(++nentries))))
+ die("realloc returned NULL");
+ if (!(entries[nentries-1] = calloc(1, sizeof(*entries[0]))))
+ die("calloc returned NULL");
+ if (!(entries[nentries-1]->label = strdup("cancel")))
+ die("strdup returned NULL\n");
+ if (!(entries[nentries-1]->cmd = strdup("exit")))
+ die("strdup returned NULL\n");
entries[nentries-1]->forceexit = True;
}
@@ -696,8 +698,11 @@ main(int argc, char *argv[])
cleanup();
XCloseDisplay(dpy);
- for (i = 0; i < nentries; i++)
+ for (i = 0; i < nentries; i++) {
+ free(entries[i]->label);
+ free(entries[i]->cmd);
free(entries[i]);
+ }
free(entries);
return 0;