From 9bab3d579554491cf60c288bf941efd70b5cc5c6 Mon Sep 17 00:00:00 2001 From: Alug Date: Fri, 19 Sep 2025 13:37:56 +0200 Subject: [PATCH] fix some potential leaks in M_DrawSplitText --- src/m_menu.c | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/m_menu.c b/src/m_menu.c index 3ccbed5eb..bbe54ab1c 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -2143,7 +2143,8 @@ static void M_DrawSplitText(INT32 x, INT32 y, INT32 option, const char* str, INT char** clines = NULL; INT16 num_lines = 0; - if (icopy == NULL) return; + if (icopy == NULL) + return; char* tok = strtok(icopy, "\n"); @@ -2151,17 +2152,26 @@ static void M_DrawSplitText(INT32 x, INT32 y, INT32 option, const char* str, INT { char* line = strdup(tok); - if (line == NULL) return; + if (line == NULL) + { + goto cleanup; + } - clines = realloc(clines, (num_lines + 1) * sizeof(char*)); + char **tmp = realloc(clines, (num_lines + 1) * sizeof(char *)); + + if (tmp == NULL) + { + free(line); + goto cleanup; + } + + clines = tmp; clines[num_lines] = line; num_lines++; tok = strtok(NULL, "\n"); } - free(icopy); - INT16 yoffset; yoffset = (((5*10 - num_lines*10))); @@ -2180,11 +2190,17 @@ static void M_DrawSplitText(INT32 x, INT32 y, INT32 option, const char* str, INT V_DrawCenteredThinString(x, y + yoffset, option, clines[i]); V_DrawCenteredThinString(x, y + yoffset, option|V_YELLOWMAP|((9 - alpha) << V_ALPHASHIFT), clines[i]); yoffset += 10; - // Remember to free the memory for each line when you're done with it. - free(clines[i]); } - free(clines); +cleanup: + if (clines) + { + // Remember to free the memory for each line when you're done with it. + for (int i = 0; i < num_lines; i++) + free(clines[i]); + free(clines); + } + free(icopy); } static void M_DoToolTips(menu_t* menu)