From 939211d6408dd54ff5f210e1d8eaeb86f0b344fb Mon Sep 17 00:00:00 2001 From: Vasilii Rogin Date: Sun, 12 Jan 2025 22:59:34 +0200 Subject: [PATCH] Copy sound string because dictionary can re-allocate it (#436) Co-authored-by: Alexander Batalov --- src/game_sound.cc | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/game_sound.cc b/src/game_sound.cc index a40afd2..7ed4833 100644 --- a/src/game_sound.cc +++ b/src/game_sound.cc @@ -107,10 +107,10 @@ static int _background_loop_requested = -1; static char* _sound_sfx_path = _aSoundSfx; // 0x518E78 -static char* _sound_music_path1 = _aSoundMusic_0; +static char* _sound_music_path1 = nullptr; // 0x518E7C -static char* _sound_music_path2 = _aSoundMusic_0; +static char* _sound_music_path2 = nullptr; // 0x518E80 static char* _sound_speech_path = _aSoundSpeech_0; @@ -391,6 +391,9 @@ int gameSoundExit() audioFileExit(); audioExit(); + internal_free(_sound_music_path1); + internal_free(_sound_music_path2); + gGameSoundInitialized = false; return 0; @@ -1957,12 +1960,15 @@ int _gsound_get_music_path(char** out_value, const char* key) char* copy; char* value; - configGetString(&gGameConfig, GAME_CONFIG_SOUND_KEY, key, out_value); + if (!configGetString(&gGameConfig, GAME_CONFIG_SOUND_KEY, key, &value)) { + *out_value = internal_strdup(_aSoundMusic_0); + return 0; + } - value = *out_value; len = strlen(value); if (value[len - 1] == '\\' || value[len - 1] == '/') { + *out_value = internal_strdup(value); return 0; } @@ -1978,7 +1984,9 @@ int _gsound_get_music_path(char** out_value, const char* key) copy[len] = '\\'; copy[len + 1] = '\0'; - if (configSetString(&gGameConfig, GAME_CONFIG_SOUND_KEY, key, copy) != 1) { + if (!configSetString(&gGameConfig, GAME_CONFIG_SOUND_KEY, key, copy)) { + internal_free(copy); + if (gGameSoundDebugEnabled) { debugPrint("config_set_string failed in gsound_music_path.\n"); } @@ -1986,16 +1994,20 @@ int _gsound_get_music_path(char** out_value, const char* key) return -1; } - if (configGetString(&gGameConfig, GAME_CONFIG_SOUND_KEY, key, out_value)) { + if (!configGetString(&gGameConfig, GAME_CONFIG_SOUND_KEY, key, &value)) { internal_free(copy); - return 0; + + if (gGameSoundDebugEnabled) { + debugPrint("config_get_string failed in gsound_music_path.\n"); + } + + return -1; } - if (gGameSoundDebugEnabled) { - debugPrint("config_get_string failed in gsound_music_path.\n"); - } + internal_free(copy); - return -1; + *out_value = internal_strdup(value); + return 0; } // 0x452378