From 8939bdba3bd4ffeb02c9f1fb7fabefe50c62a22c Mon Sep 17 00:00:00 2001 From: Agent X <44549182+Agent-11@users.noreply.github.com> Date: Sat, 6 Jan 2024 09:47:28 -0500 Subject: [PATCH] Fix potential mod storage crashes and increase max key/value length to 256 --- autogen/lua_definitions/constants.lua | 2 +- src/pc/lua/smlua_constants_autogen.c | 2 +- src/pc/mods/mod_storage.cpp | 53 +++++++++------------------ src/pc/mods/mod_storage.h | 2 +- 4 files changed, 20 insertions(+), 39 deletions(-) diff --git a/autogen/lua_definitions/constants.lua b/autogen/lua_definitions/constants.lua index a2109c03..bab0d432 100644 --- a/autogen/lua_definitions/constants.lua +++ b/autogen/lua_definitions/constants.lua @@ -5675,7 +5675,7 @@ MARIO_HAND_RIGHT_OPEN = 5 MAX_KEYS = 512 --- @type integer -MAX_KEY_VALUE_LENGTH = 64 +MAX_KEY_VALUE_LENGTH = 256 --- @type integer PACKET_LENGTH = 3000 diff --git a/src/pc/lua/smlua_constants_autogen.c b/src/pc/lua/smlua_constants_autogen.c index a7039011..698e9be3 100644 --- a/src/pc/lua/smlua_constants_autogen.c +++ b/src/pc/lua/smlua_constants_autogen.c @@ -2094,7 +2094,7 @@ char gSmluaConstants[] = "" "GRAB_POS_HEAVY_OBJ = 2\n" "GRAB_POS_BOWSER = 3\n" "MAX_KEYS = 512\n" -"MAX_KEY_VALUE_LENGTH = 64\n" +"MAX_KEY_VALUE_LENGTH = 256\n" "SYNC_DISTANCE_ONLY_DEATH = -1\n" "SYNC_DISTANCE_ONLY_EVENTS = -2\n" "SYNC_DISTANCE_INFINITE = 0\n" diff --git a/src/pc/mods/mod_storage.cpp b/src/pc/mods/mod_storage.cpp index 3fa6f587..554d42df 100644 --- a/src/pc/mods/mod_storage.cpp +++ b/src/pc/mods/mod_storage.cpp @@ -64,30 +64,23 @@ void mod_storage_get_filename(char* dest) { } C_FIELD bool mod_storage_save(const char* key, const char* value) { - if (strlen(key) > MAX_KEY_VALUE_LENGTH || strlen(value) > MAX_KEY_VALUE_LENGTH) { - return false; - } - if (!char_valid((char *)key) || !char_valid((char *)value)) { - return false; - } + if (gLuaActiveMod == NULL) { return false; } + if (strlen(key) > MAX_KEY_VALUE_LENGTH || strlen(value) > MAX_KEY_VALUE_LENGTH) { return false; } + if (!char_valid((char *)key) || !char_valid((char *)value)) { return false; } char filename[SYS_MAX_PATH] = {0}; mod_storage_get_filename(filename); // ensure savPath exists char savPath[SYS_MAX_PATH] = { 0 }; - if (snprintf(savPath, SYS_MAX_PATH - 1, "%s", fs_get_write_path(SAVE_DIRECTORY)) < 0) { - return false; - } + if (snprintf(savPath, SYS_MAX_PATH - 1, "%s", fs_get_write_path(SAVE_DIRECTORY)) < 0) { return false; } if (!fs_sys_dir_exists(savPath)) { fs_sys_mkdir(savPath); } mINI::INIFile file(filename); mINI::INIStructure ini; file.read(ini); - if (ini["storage"].size() + 1 > MAX_KEYS) { - return false; - } + if (ini["storage"].size() + 1 > MAX_KEYS) { return false; } ini["storage"][key] = value; @@ -106,19 +99,14 @@ C_FIELD bool mod_storage_save_bool(const char* key, bool value) { } C_FIELD const char* mod_storage_load(const char* key) { - if (strlen(key) > MAX_KEY_VALUE_LENGTH) { - return NULL; - } - if (!char_valid((char *)key)) { - return NULL; - } + if (gLuaActiveMod == NULL) { return NULL; } + if (strlen(key) > MAX_KEY_VALUE_LENGTH) { return NULL; } + if (!char_valid((char *)key)) { return NULL; } char filename[SYS_MAX_PATH] = {0}; mod_storage_get_filename(filename); - if (!path_exists(filename)) { - return NULL; - } + if (!path_exists(filename)) { return NULL; } mINI::INIFile file(filename); mINI::INIStructure ini; @@ -142,19 +130,14 @@ C_FIELD bool mod_storage_load_bool(const char* key) { } C_FIELD bool mod_storage_remove(const char* key) { - if (strlen(key) > MAX_KEY_VALUE_LENGTH) { - return false; - } - if (!char_valid((char *)key)) { - return false; - } + if (gLuaActiveMod == NULL) { return false; } + if (strlen(key) > MAX_KEY_VALUE_LENGTH) { return false; } + if (!char_valid((char *)key)) { return false; } char filename[SYS_MAX_PATH] = {0}; mod_storage_get_filename(filename); - if (!path_exists(filename)) { - return false; - } + if (!path_exists(filename)) { return false; } mINI::INIFile file(filename); mINI::INIStructure ini; @@ -170,20 +153,18 @@ C_FIELD bool mod_storage_remove(const char* key) { } C_FIELD bool mod_storage_clear(void) { + if (gLuaActiveMod == NULL) { return false; } + char filename[SYS_MAX_PATH] = {0}; mod_storage_get_filename(filename); - if (!path_exists(filename)) { - return false; - } + if (!path_exists(filename)) { return false; } mINI::INIFile file(filename); mINI::INIStructure ini; file.read(ini); - if (ini["storage"].size() == 0) { - return false; - } + if (ini["storage"].size() == 0) { return false; } ini["storage"].clear(); diff --git a/src/pc/mods/mod_storage.h b/src/pc/mods/mod_storage.h index 1b614c1b..a5847f59 100644 --- a/src/pc/mods/mod_storage.h +++ b/src/pc/mods/mod_storage.h @@ -6,7 +6,7 @@ extern "C" { #endif #define MAX_KEYS 512 -#define MAX_KEY_VALUE_LENGTH 64 +#define MAX_KEY_VALUE_LENGTH 256 #define SAVE_DIRECTORY "sav" #define SAVE_EXTENSION ".sav"