1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-22 22:13:02 +01:00

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
This commit is contained in:
Simon Levermann 2013-08-08 16:36:16 +02:00
parent c3c144ddd0
commit a9ec08b998
3 changed files with 15 additions and 24 deletions

View File

@ -56,10 +56,8 @@ char *get_user_config_dir(void)
) )
if (!result) return NULL; if (!result) return NULL;
user_config_dir = malloc(strlen(appdata) + 1); user_config_dir = strdup(appdata);
if (user_config_dir) {
strcpy(user_config_dir, appdata);
}
return user_config_dir; return user_config_dir;
#elif defined __APPLE__ #elif defined __APPLE__
@ -78,10 +76,7 @@ char *get_user_config_dir(void)
#else #else
if (getenv("XDG_CONFIG_HOME")) { if (getenv("XDG_CONFIG_HOME")) {
user_config_dir = malloc(strlen(getenv("XDG_CONFIG_HOME")) + 1); user_config_dir = strdup(getenv("XDG_CONFIG_HOME"));
if (user_config_dir) {
strcpy(user_config_dir, getenv("XDG_CONFIG_HOME"));
}
} else { } else {
user_config_dir = malloc(strlen(getenv("HOME")) + strlen("/.config") + 1); user_config_dir = malloc(strlen(getenv("HOME")) + strlen("/.config") + 1);
if (user_config_dir) { if (user_config_dir) {
@ -109,12 +104,10 @@ int create_user_config_dir(char *path)
strcat(fullpath, CONFIGDIR); strcat(fullpath, CONFIGDIR);
mkdir_err = _mkdir(fullpath); mkdir_err = _mkdir(fullpath);
struct __stat64 buf;
if (mkdir_err) { if (mkdir_err && (errno != EEXIST || _wstat64(fullpath, &buf) || !S_ISDIR(buf.st_mode))) {
if(errno != EEXIST) return -1; free(fullpath);
struct _stat buf; return -1;
if(_wstat64(fullpath, &buf)) return -1;
if(!S_ISDIR(buf.st_mode)) return -1;
} }
#else #else
@ -122,10 +115,8 @@ int create_user_config_dir(char *path)
mkdir_err = mkdir(path, 0700); mkdir_err = mkdir(path, 0700);
struct stat buf; struct stat buf;
if(mkdir_err) { if(mkdir_err && (errno != EEXIST || stat(path, &buf) || !S_ISDIR(buf.st_mode))) {
if(errno != EEXIST) return -1; return -1;
if(stat(path, &buf)) return -1;
if(!S_ISDIR(buf.st_mode)) return -1;
} }
char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 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); mkdir_err = mkdir(fullpath, 0700);
if(mkdir_err) { if(mkdir_err && (errno != EEXIST || stat(fullpath, &buf) || !S_ISDIR(buf.st_mode))) {
if(errno != EEXIST) return -1; free(fullpath);
if(stat(fullpath, &buf)) return -1; return -1;
if(!S_ISDIR(buf.st_mode)) return -1;
} }
#endif #endif
return 0; return 0;
} }

View File

@ -30,4 +30,4 @@
char *get_user_config_dir(void); char *get_user_config_dir(void);
int create_user_config_dir(char *path); int create_user_config_dir(char *path);

1
main.c
View File

@ -370,6 +370,7 @@ int main(int argc, char *argv[])
init_term(); init_term();
init_tox(); init_tox();
load_data(filename); load_data(filename);
free(filename);
init_windows(); init_windows();
init_window_status(); init_window_status();