From 660e787502e488b74470e92e37c3dc13e14bd261 Mon Sep 17 00:00:00 2001 From: MysterD Date: Sun, 20 Feb 2022 22:06:53 -0800 Subject: [PATCH] Fix bugs pointed out by valgrind --- src/game/camera.c | 8 ++++---- src/pc/configfile.c | 6 +++++- src/pc/debuglog.h | 6 ++++-- src/pc/logfile.h | 4 +++- src/pc/pc_main.c | 2 +- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/game/camera.c b/src/game/camera.c index ac6fc984..8c82adf6 100644 --- a/src/game/camera.c +++ b/src/game/camera.c @@ -176,10 +176,10 @@ extern struct Camera *gCamera; * Lakitu's position and focus. * @see LakituState */ -struct LakituState gLakituState; -struct CameraFOVStatus sFOVState; -struct TransitionInfo sModeTransition; -struct PlayerGeometry sMarioGeometry; +struct LakituState gLakituState = { 0 }; +struct CameraFOVStatus sFOVState = { 0 }; +struct TransitionInfo sModeTransition = { 0 }; +struct PlayerGeometry sMarioGeometry = { 0 }; struct Camera *gCamera; s16 unusedFreeRoamWallYaw; s16 sAvoidYawVel; diff --git a/src/pc/configfile.c b/src/pc/configfile.c index 9334dab7..2245f1a4 100644 --- a/src/pc/configfile.c +++ b/src/pc/configfile.c @@ -298,8 +298,10 @@ void configfile_load(const char *filename) { while (isspace(*p)) p++; - if (!*p || *p == '#') // comment or empty line + if (!*p || *p == '#') { // comment or empty line + free(line); continue; + } numTokens = tokenize_string(p, sizeof(tokens) / sizeof(tokens[0]), tokens); if (numTokens != 0) { @@ -315,12 +317,14 @@ void configfile_load(const char *filename) { break; } } + free(line); continue; } // ban list if (!strcmp(tokens[0], "ban:")) { ban_list_add(tokens[1], true); + free(line); continue; } diff --git a/src/pc/debuglog.h b/src/pc/debuglog.h index 14e23423..66af0e82 100644 --- a/src/pc/debuglog.h +++ b/src/pc/debuglog.h @@ -7,9 +7,11 @@ static void _debuglog_print_timestamp(void) { time_t ltime = time(NULL); - char* str = asctime(localtime(<ime)); + struct tm ltime2 = { 0 }; + localtime_r(<ime, <ime2); + char* str = asctime(<ime2); printf("%.*s", (int)strlen(str) - 1, str); - } +} static void _debuglog_print_network_type(void) { printf(" [%02d] ", (gNetworkPlayerLocal != NULL) ? gNetworkPlayerLocal->globalIndex : -1); diff --git a/src/pc/logfile.h b/src/pc/logfile.h index e1993c4c..99f6eb52 100644 --- a/src/pc/logfile.h +++ b/src/pc/logfile.h @@ -26,7 +26,9 @@ void logfile_close(enum LogFileType logFileType); static void _logfile_print_timestamp(enum LogFileType logFileType) { FILE* f = gLogFiles[logFileType].file; time_t ltime = time(NULL); - char* str = asctime(localtime(<ime)); + struct tm ltime2 = { 0 }; + localtime_r(<ime, <ime2); + char* str = asctime(<ime2); fprintf(f, "%.*s", (int)strlen(str) - 1, str); } diff --git a/src/pc/pc_main.c b/src/pc/pc_main.c index e5fc9fed..361f3b58 100644 --- a/src/pc/pc_main.c +++ b/src/pc/pc_main.c @@ -232,7 +232,7 @@ void main_func(void) { configWindow.fullscreen = false; const size_t poolsize = gCLIOpts.PoolSize ? gCLIOpts.PoolSize : DEFAULT_POOL_SIZE; - u64 *pool = malloc(poolsize); + u64 *pool = calloc(poolsize, 1); if (!pool) sys_fatal("Could not alloc %u bytes for main pool.\n", poolsize); main_pool_init(pool, pool + poolsize / sizeof(pool[0])); gEffectsMemoryPool = mem_pool_init(0x4000, MEMORY_POOL_LEFT);