From c950e8fd38c0f5108cfad6a2484929de44518dab Mon Sep 17 00:00:00 2001 From: jfreegman Date: Thu, 22 Feb 2024 17:43:19 -0500 Subject: [PATCH] cleanup: Don't call realloc() with size value of 0 This is apparently going to be UB in the future, and it's ugly either way. --- src/windows.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/windows.c b/src/windows.c index 47abd4030..44cf77b2a 100644 --- a/src/windows.c +++ b/src/windows.c @@ -20,6 +20,7 @@ * */ +#include #include #include #include @@ -862,7 +863,10 @@ static void set_next_window(Windows *windows, const Client_Config *c_config, int if (ch == c_config->key_prev_tab) { windows->active_index = windows->active_index > 0 ? windows->active_index - 1 : windows->count - 1; + return; } + + fprintf(stderr, "Warning: set_next_window() got invalid key: %c\n", ch); } /* Deletes window w and cleans up */ @@ -878,6 +882,8 @@ void del_window(ToxWindow *w, Windows *windows, const Client_Config *c_config) delwin(w->window); free(w); + assert(windows->count > 0); + --windows->count; if (windows->count != idx) { @@ -886,10 +892,17 @@ void del_window(ToxWindow *w, Windows *windows, const Client_Config *c_config) windows->list[windows->count] = NULL; + if (windows->count == 0) { + free(windows->list); + windows->list = NULL; + return; + } + ToxWindow **tmp_list = (ToxWindow **)realloc(windows->list, windows->count * sizeof(ToxWindow *)); - if (tmp_list == NULL && windows->count != 0) { - exit_toxic_err(FATALERR_MEMORY, "realloc(_, %d * sizeof(ToxWindow *)) failed in del_window()", windows->count); + if (tmp_list == NULL) { + exit_toxic_err(FATALERR_MEMORY, + "realloc(_, %d * sizeof(ToxWindow *)) failed in del_window()", windows->count); } windows->list = tmp_list;