From 40ee9e2b7d7f10adb12932d71e896fe97650f754 Mon Sep 17 00:00:00 2001 From: bakkeby Date: Sun, 27 Mar 2022 22:38:32 +0200 Subject: [PATCH] highpriority: adding check for malloc failure + cleanup on exit ref. https://git.suckless.org/sites/commit/9bbc02d13a89e427a046817771c6f8fd332d0167.html --- dmenu.c | 5 +++++ patch/highpriority.c | 19 +++++++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/dmenu.c b/dmenu.c index fce63db..50c7793 100644 --- a/dmenu.c +++ b/dmenu.c @@ -271,6 +271,11 @@ cleanup(void) XUngrabKey(dpy, AnyKey, AnyModifier, root); for (i = 0; i < SchemeLast; i++) free(scheme[i]); + #if HIGHPRIORITY_PATCH + for (i = 0; i < hplength; ++i) + free(hpitems[i]); + free(hpitems); + #endif // HIGHPRIORITY_PATCH drw_free(drw); XSync(dpy, False); XCloseDisplay(dpy); diff --git a/patch/highpriority.c b/patch/highpriority.c index 568c705..2b3577a 100644 --- a/patch/highpriority.c +++ b/patch/highpriority.c @@ -1,17 +1,20 @@ static char **hpitems = NULL; static int hplength = 0; -static char** -tokenize(char *source, const char *delim, int *llen) { - int listlength = 0; - char **list = malloc(1 * sizeof(char*)); - char *token = strtok(source, delim); +static char ** +tokenize(char *source, const char *delim, int *llen) +{ + int listlength = 0, list_size = 0; + char **list = NULL, *token; + token = strtok(source, delim); while (token) { - if (!(list = realloc(list, sizeof(char*) * (listlength + 1)))) - die("Unable to realloc %d bytes\n", sizeof(char*) * (listlength + 1)); + if (listlength + 1 >= list_size) { + if (!(list = realloc(list, (list_size += 8) * sizeof(*list)))) + die("Unable to realloc %zu bytes\n", list_size * sizeof(*list)); + } if (!(list[listlength] = strdup(token))) - die("Unable to strdup %d bytes\n", strlen(token) + 1); + die("Unable to strdup %zu bytes\n", strlen(token) + 1); token = strtok(NULL, delim); listlength++; }