From 43972d307ad232d926c250becc3ad80e866f1494 Mon Sep 17 00:00:00 2001 From: Christophe CURIS Date: Tue, 20 May 2014 21:46:30 +0200 Subject: [PATCH] WMaker: fix memory leak in windows menu (Coverity #50109) As pointed by Coverity, there was a memory leak when updating the shortcuts in the menu for windows which occured when a shortcut was associated with the window and this shortcut did not change. Signed-off-by: Christophe CURIS --- src/winmenu.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/winmenu.c b/src/winmenu.c index d8b3f4f3..5cf568b1 100644 --- a/src/winmenu.c +++ b/src/winmenu.c @@ -361,7 +361,6 @@ static void updateMakeShortcutMenu(WMenu *menu, WWindow *wwin) buffer = wmalloc(buflen); for (i = WO_ENTRIES; i < smenu->entry_no; i++) { - char *tmp; int shortcutNo = i - WO_ENTRIES; WMenuEntry *entry = smenu->entries[i]; WMArray *shortSelWindows = w_global.shortcut.windows[shortcutNo]; @@ -387,12 +386,28 @@ static void updateMakeShortcutMenu(WMenu *menu, WWindow *wwin) kcode = wKeyBindings[WKBD_WINDOW1 + shortcutNo].keycode; if (kcode) { - if ((tmp = GetShortcutKey(wKeyBindings[WKBD_WINDOW1 + shortcutNo])) - && (!entry->rtext || strcmp(tmp, entry->rtext) != 0)) { - if (entry->rtext) + char *tmp; + + tmp = GetShortcutKey(wKeyBindings[WKBD_WINDOW1 + shortcutNo]); + if (tmp == NULL) { + if (entry->rtext != NULL) { + /* There was a shortcut, but there is no more */ wfree(entry->rtext); + entry->rtext = NULL; + smenu->flags.realized = 0; + } + } else if (entry->rtext == NULL) { + /* There was no shortcut, but there is one now */ entry->rtext = tmp; smenu->flags.realized = 0; + } else if (strcmp(tmp, entry->rtext) != 0) { + /* There was a shortcut, but it has changed */ + wfree(entry->rtext); + entry->rtext = tmp; + smenu->flags.realized = 0; + } else { + /* There was a shortcut but it did not change */ + wfree(tmp); } wMenuSetEnabled(smenu, i, True); } else {