From b80118fb419107e1af935c61dbe37e6a0db542f7 Mon Sep 17 00:00:00 2001 From: Christophe CURIS Date: Sat, 29 Nov 2014 16:35:16 +0100 Subject: [PATCH] WINGs: fix WMPathForResourceOfType to check for all the paths it claims to check The "documentation" of the function claims to check for the resource in a number of path, but factually if the application did provide its argument list when creating the WINGs App structure (which is likely) then the search would stop before checking all paths. The code now continues as expectable if the resource was not found in the path. Took opportunity to avoid a temporary allocation that participated in memory fragmentation. Signed-off-by: Christophe CURIS --- WINGs/wapplication.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/WINGs/wapplication.c b/WINGs/wapplication.c index 90874c06..39b37d96 100644 --- a/WINGs/wapplication.c +++ b/WINGs/wapplication.c @@ -131,11 +131,10 @@ error: char *WMPathForResourceOfType(const char *resource, const char *ext) { - char *path, *tmp, *appdir; - int i; + char *path, *appdir; size_t slen; - path = tmp = appdir = NULL; + path = appdir = NULL; /* * Paths are searched in this order: @@ -156,17 +155,19 @@ char *WMPathForResourceOfType(const char *resource, const char *ext) } if (WMApplication.argv[0]) { - tmp = wstrdup(WMApplication.argv[0]); - i = strlen(tmp); - while (i > 0 && tmp[i] != '/') - i--; - tmp[i] = 0; - if (i > 0) { + char *ptr_slash; + + ptr_slash = strrchr(WMApplication.argv[0], '/'); + if (ptr_slash != NULL) { + char tmp[ptr_slash - WMApplication.argv[0] + 1]; + + strncpy(tmp, WMApplication.argv[0], sizeof(tmp)-1); + tmp[sizeof(tmp) - 1] = '\0'; + path = checkFile(tmp, NULL, ext, resource); - } else { - path = NULL; + if (path) + goto out; } - goto out; } slen = strlen(WMApplication.applicationName) + sizeof("Applications/.app"); @@ -197,8 +198,6 @@ char *WMPathForResourceOfType(const char *resource, const char *ext) path = checkFile("/usr/GNUstep", appdir, ext, resource); /* falls through */ out: - if (tmp) - wfree(tmp); if (appdir) wfree(appdir);