From a9ec08b9989747791e42eca1b618a89732ebe7c4 Mon Sep 17 00:00:00 2001 From: Simon Levermann Date: Thu, 8 Aug 2013 16:36:16 +0200 Subject: [PATCH] Cleanup and Error fixes Add several frees that were missing to prevent memory leaks Replace strcpy with strdup where appropriate Replace _stat with __stat64 for building on Windows --- configdir.c | 36 +++++++++++++----------------------- configdir.h | 2 +- main.c | 1 + 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/configdir.c b/configdir.c index 7d11d02..d91d080 100644 --- a/configdir.c +++ b/configdir.c @@ -56,10 +56,8 @@ char *get_user_config_dir(void) ) if (!result) return NULL; - user_config_dir = malloc(strlen(appdata) + 1); - if (user_config_dir) { - strcpy(user_config_dir, appdata); - } + user_config_dir = strdup(appdata); + return user_config_dir; #elif defined __APPLE__ @@ -78,10 +76,7 @@ char *get_user_config_dir(void) #else if (getenv("XDG_CONFIG_HOME")) { - user_config_dir = malloc(strlen(getenv("XDG_CONFIG_HOME")) + 1); - if (user_config_dir) { - strcpy(user_config_dir, getenv("XDG_CONFIG_HOME")); - } + user_config_dir = strdup(getenv("XDG_CONFIG_HOME")); } else { user_config_dir = malloc(strlen(getenv("HOME")) + strlen("/.config") + 1); if (user_config_dir) { @@ -109,12 +104,10 @@ int create_user_config_dir(char *path) strcat(fullpath, CONFIGDIR); mkdir_err = _mkdir(fullpath); - - if (mkdir_err) { - if(errno != EEXIST) return -1; - struct _stat buf; - if(_wstat64(fullpath, &buf)) return -1; - if(!S_ISDIR(buf.st_mode)) return -1; + struct __stat64 buf; + if (mkdir_err && (errno != EEXIST || _wstat64(fullpath, &buf) || !S_ISDIR(buf.st_mode))) { + free(fullpath); + return -1; } #else @@ -122,10 +115,8 @@ int create_user_config_dir(char *path) mkdir_err = mkdir(path, 0700); struct stat buf; - if(mkdir_err) { - if(errno != EEXIST) return -1; - if(stat(path, &buf)) return -1; - if(!S_ISDIR(buf.st_mode)) return -1; + if(mkdir_err && (errno != EEXIST || stat(path, &buf) || !S_ISDIR(buf.st_mode))) { + return -1; } char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1); @@ -134,13 +125,12 @@ int create_user_config_dir(char *path) mkdir_err = mkdir(fullpath, 0700); - if(mkdir_err) { - if(errno != EEXIST) return -1; - if(stat(fullpath, &buf)) return -1; - if(!S_ISDIR(buf.st_mode)) return -1; + if(mkdir_err && (errno != EEXIST || stat(fullpath, &buf) || !S_ISDIR(buf.st_mode))) { + free(fullpath); + return -1; } #endif return 0; -} \ No newline at end of file +} diff --git a/configdir.h b/configdir.h index d9837d7..fad949c 100644 --- a/configdir.h +++ b/configdir.h @@ -30,4 +30,4 @@ char *get_user_config_dir(void); -int create_user_config_dir(char *path); \ No newline at end of file +int create_user_config_dir(char *path); diff --git a/main.c b/main.c index cb0d9e1..13577cc 100644 --- a/main.c +++ b/main.c @@ -370,6 +370,7 @@ int main(int argc, char *argv[]) init_term(); init_tox(); load_data(filename); + free(filename); init_windows(); init_window_status();