From 720e8562e7f95693d13ee544fa4fb62781baec9e Mon Sep 17 00:00:00 2001 From: Marc Date: Sun, 11 Sep 2022 15:41:02 +0200 Subject: [PATCH] cleanup more memory leaks --- src/main.c | 36 +++++++++++++++++++++++++----------- src/steam.c | 8 ++++++-- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/main.c b/src/main.c index 44f7840..59f60d5 100644 --- a/src/main.c +++ b/src/main.c @@ -89,6 +89,7 @@ int validateAppId(const char * appIdStr) { int listGames(int argc, char ** argv) { if(argc != 2) return usage(); GList * gamesIds = g_hash_table_get_keys(gamePaths); + GList * gamesIdsFirstPointer = gamesIds; if(g_list_length(gamesIds) == 0) { printf("No game found\n"); } else { @@ -98,6 +99,7 @@ int listGames(int argc, char ** argv) { gamesIds = g_list_next(gamesIds); } } + g_list_free(gamesIdsFirstPointer); return EXIT_SUCCESS; } @@ -161,6 +163,8 @@ int installAndRemoveMod(int argc, char ** argv, bool install) { return EXIT_FAILURE; } + int returnValue = EXIT_SUCCESS; + //strtoul set EINVAL(after C99) if the string is invalid u_int32_t modId = strtoul(argv[3], NULL, 10); if(errno == EINVAL) { @@ -188,7 +192,8 @@ int installAndRemoveMod(int argc, char ** argv, bool install) { if(install) { if(access(modFlag, F_OK) == 0) { printf("The mod is already activated \n"); - return EXIT_SUCCESS; + returnValue = EXIT_SUCCESS; + goto exit; } //Create activated file @@ -198,19 +203,24 @@ int installAndRemoveMod(int argc, char ** argv, bool install) { } else { if(access(modFlag, F_OK) != 0) { printf("The mod is not activated \n"); - return EXIT_SUCCESS; + returnValue = EXIT_SUCCESS; + goto exit; } if(unlink(modFlag) != 0) { printf("Error could not disable the mod.\n"); printf("please remove this file '%s' as root\n", modFlag); - return EXIT_FAILURE; + returnValue = EXIT_FAILURE; + goto exit; } } - free(modFolder); - g_list_free(modsFirstPointer); - return EXIT_SUCCESS; +exit: + + g_free(modFolder); + g_list_free_full(modsFirstPointer, free); + g_free(modFlag); + return returnValue; } int deploy(int argc, char ** argv) { @@ -339,6 +349,7 @@ int setup(int argc, char ** argv) { return EXIT_SUCCESS; } + int unbind(int argc, char ** argv) { if(argc != 3 ) return usage(); char * appIdStr = argv[2]; @@ -358,7 +369,6 @@ int unbind(int argc, char ** argv) { int main(int argc, char ** argv) { if(argc < 2 ) return usage(); - if(audit_getloginuid() == 0) { printf("Root user should not be using this\n"); return EXIT_FAILURE; @@ -366,14 +376,17 @@ int main(int argc, char ** argv) { int searchStatus; gamePaths = search_games(&searchStatus); - int returnValue = EXIT_SUCCESS; + int returnValue = EXIT_SUCCESS; if(searchStatus == EXIT_FAILURE) { + returnValue = EXIT_FAILURE; printf("Error while looking up libraries, do you have steam installed ?"); goto exit; } - char * configFolder = g_build_filename(getHome(), MANAGER_FILES, NULL); + char * home = getHome(); + char * configFolder = g_build_filename(home, MANAGER_FILES, NULL); + free(home); if(access(configFolder, F_OK) != 0) { //check to NOT run this as root @@ -394,7 +407,7 @@ int main(int argc, char ** argv) { //failure would have no impact char * chattrcommand = g_strjoin("", "chattr +F ",configFolder, " 2> /dev/null", NULL); system(chattrcommand); - free(chattrcommand); + g_free(chattrcommand); } } @@ -452,7 +465,8 @@ int main(int argc, char ** argv) { } exit2: - free(configFolder); + g_free(configFolder); + g_hash_table_destroy(gamePaths); exit: return returnValue; } diff --git a/src/steam.c b/src/steam.c index 92532fb..e7fd02b 100644 --- a/src/steam.c +++ b/src/steam.c @@ -99,6 +99,7 @@ ValveLibraries_t * parseVDF(const char * path, size_t * size, int * status) { case FIELD_TOTAL_SIZE: library->totalSize = strtoul(value, NULL, 0); + free(value); break; case FIELD_CLEAN_BYTES: @@ -162,6 +163,7 @@ ValveLibraries_t * parseVDF(const char * path, size_t * size, int * status) { } exit: + if(line != NULL) free(line); fclose(fd); return libraries; } @@ -189,8 +191,10 @@ GHashTable* search_games(int * status) { if (access(path, F_OK) == 0) { int parserStatus; libraries = parseVDF(path, &size, &parserStatus); - if(parserStatus == EXIT_SUCCESS) + if(parserStatus == EXIT_SUCCESS) { + g_free(path); break; + } } g_free(path); @@ -202,7 +206,7 @@ GHashTable* search_games(int * status) { return NULL; } - GHashTable* table = g_hash_table_new(g_int_hash, g_int_equal); + GHashTable* table = g_hash_table_new_full(g_int_hash, g_int_equal, free, free); //fill the table for(int i = 0; i < size; i++) {