From ac3212b0012bb9aaafe0eec4e28210679f777462 Mon Sep 17 00:00:00 2001 From: Christophe CURIS Date: Sat, 15 Nov 2014 19:40:50 +0100 Subject: [PATCH] wmaker: fix memory leak in get_icon_filename (Coverity #50132) Coverity pointed that in the typical code path the function FindImage would be called twice, leading in leakage of the allocated result from the first call. This patch updates the condition so that the case won't arise. Signed-off-by: Christophe CURIS Signed-off-by: Carlos R. Mafra --- src/wdefaults.c | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/src/wdefaults.c b/src/wdefaults.c index 3371e710..c2d9eb80 100644 --- a/src/wdefaults.c +++ b/src/wdefaults.c @@ -375,8 +375,8 @@ static WMPropList *get_generic_value(const char *instance, const char *class, char *get_icon_filename(const char *winstance, const char *wclass, const char *command, Bool default_icon) { - char *file_name = NULL; - char *file_path = NULL; + char *file_name; + char *file_path; /* Get the file name of the image, using instance and class */ file_name = wDefaultGetIconFile(winstance, wclass, default_icon); @@ -384,29 +384,32 @@ char *get_icon_filename(const char *winstance, const char *wclass, const char *c /* Check if the file really exists in the disk */ if (file_name) file_path = FindImage(wPreferences.icon_path, file_name); + else + file_path = NULL; /* If the specific icon filename is not found, and command is specified, * then include the .app icons and re-do the search. */ - if ((!file_name || !file_path ) && command) { + if (!file_path && command) { wApplicationExtractDirPackIcon(command, winstance, wclass); file_name = wDefaultGetIconFile(winstance, wclass, False); + + if (file_name) { + file_path = FindImage(wPreferences.icon_path, file_name); + if (!file_path) + wwarning(_("icon \"%s\" doesn't exist, check your config files"), file_name); + + /* FIXME: Here, if file_path does not exist then the icon is still in the + * "icon database" (w_global.domain.window_attr->dictionary), but the file + * for the icon is no more on disk. Therefore, we should remove it from the + * database. Is possible to do that using wDefaultChangeIcon() */ + } } - /* Get the full path for the image file */ - if (file_name) { - file_path = FindImage(wPreferences.icon_path, file_name); - - if (!file_path) - wwarning(_("icon \"%s\" doesn't exist, check your config files"), file_name); - - /* FIXME: Here, if file_path don't exists, then the icon is in the - * "icon database" (w_global.domain.window_attr->dictionary), but the icon - * is not en disk. Therefore, we should remove it from the icon - * database. Is possible to do that using wDefaultChangeIcon() */ - - /* Don't wfree(file_name) here, because is a pointer to the icon - * dictionary (w_global.domain.window_attr->dictionary) value. */ - } + /* + * Don't wfree(file_name) because it is a direct pointer inside the icon + * dictionary (w_global.domain.window_attr->dictionary) and not a result + * allocated with wstrdup() + */ if (!file_path && default_icon) file_path = get_default_image_path();