highpriority: adding check for malloc failure + cleanup on exit

ref. https://git.suckless.org/sites/commit/9bbc02d13a89e427a046817771c6f8fd332d0167.html
This commit is contained in:
bakkeby 2022-03-27 22:38:32 +02:00
parent 67180f6288
commit 40ee9e2b7d
2 changed files with 16 additions and 8 deletions

View File

@ -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);

View File

@ -2,16 +2,19 @@ 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);
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++;
}