From 0eb715bd17b62f9dd0bf3ae09ccb801ec63cfe4a Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Thu, 16 May 2019 12:28:11 +0200 Subject: [PATCH] better error handling in Extract_path() --- src/io.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/io.c b/src/io.c index 0b534d16..f3a10b25 100644 --- a/src/io.c +++ b/src/io.c @@ -354,20 +354,31 @@ char * Extract_path(char *dest, const char *source) char * path; path = Realpath(source, dest); + if (path == NULL) + { + GFX2_Log(GFX2_ERROR, "Realpath(\"%s\", %p) failed !\n", source, dest); + return NULL; + } position = Find_last_separator(path); if (position) position[1] = '\0'; else { + size_t len = strlen(path); if (dest != NULL) - strcat(dest, PATH_SEPARATOR); + strcpy(path + len, PATH_SEPARATOR); else { - char * tmp = realloc(path, strlen(path) + strlen(PATH_SEPARATOR) + 1); + char * tmp = realloc(path, len + strlen(PATH_SEPARATOR) + 1); if (tmp != NULL) { path = tmp; - strcat(path, PATH_SEPARATOR); + strcpy(path + len, PATH_SEPARATOR); + } + else + { + GFX2_Log(GFX2_ERROR, "Extract_path(): Failed to realloc %lu bytes\n", + (unsigned long)(len + strlen(PATH_SEPARATOR) + 1)); } } }