From 03d6958a3039b5a4fc5d4e1aa9393fd51383bb43 Mon Sep 17 00:00:00 2001 From: MysterD Date: Wed, 8 Nov 2023 12:14:26 -0800 Subject: [PATCH] Added bounds checking to gActiveMods.entries[] --- src/engine/behavior_script.c | 5 +++++ src/pc/lua/smlua_sync_table.c | 9 +++++++++ src/pc/lua/smlua_utils.c | 10 ++++++++++ 3 files changed, 24 insertions(+) diff --git a/src/engine/behavior_script.c b/src/engine/behavior_script.c index 8044ce2b..ff086fbf 100644 --- a/src/engine/behavior_script.c +++ b/src/engine/behavior_script.c @@ -1000,6 +1000,11 @@ static s32 bhv_cmd_call_native_ext(void) { } // Get our mod. + if (modIndex >= gActiveMods.entryCount) { + LOG_LUA("Failed to call lua function, could not find mod"); + gCurBhvCommand += 2; + return BHV_PROC_CONTINUE; + } struct Mod *mod = gActiveMods.entries[modIndex]; // Push the callback onto the stack diff --git a/src/pc/lua/smlua_sync_table.c b/src/pc/lua/smlua_sync_table.c index 07f29a5e..886f425a 100644 --- a/src/pc/lua/smlua_sync_table.c +++ b/src/pc/lua/smlua_sync_table.c @@ -135,6 +135,11 @@ static void smlua_sync_table_call_hook(int syncTableIndex, int keyIndex, int pre // get entry u16 modRemoteIndex = smlua_get_integer_field(syncTableIndex, "_remoteIndex"); + if (modRemoteIndex >= gActiveMods.entryCount) { + LOG_ERROR("Failed to find mod"); + lua_pop(L, 4); + return; + } struct Mod* mod = gActiveMods.entries[modRemoteIndex]; // call hook @@ -303,6 +308,10 @@ void smlua_set_sync_table_field_from_network(u64 seq, u16 modRemoteIndex, u16 ln lua_State* L = gLuaState; // figure out entry + if (modRemoteIndex >= gActiveMods.entryCount) { + LOG_ERROR("Could not find mod list entry for modRemoteIndex: %u", modRemoteIndex); + return; + } struct Mod* mod = gActiveMods.entries[modRemoteIndex]; if (mod == NULL) { LOG_ERROR("Could not find mod list entry for modRemoteIndex: %u", modRemoteIndex); diff --git a/src/pc/lua/smlua_utils.c b/src/pc/lua/smlua_utils.c index 52346d84..86312241 100644 --- a/src/pc/lua/smlua_utils.c +++ b/src/pc/lua/smlua_utils.c @@ -547,6 +547,11 @@ s64 smlua_get_integer_mod_variable(u16 modIndex, const char* variable) { return 0; } + if (modIndex >= gActiveMods.entryCount) { + LOG_ERROR("Could not find mod list entry"); + return 0; + } + // figure out entry struct Mod* mod = gActiveMods.entries[modIndex]; if (mod == NULL) { @@ -598,6 +603,11 @@ s64 smlua_get_any_integer_mod_variable(const char* variable) { LuaFunction smlua_get_function_mod_variable(u16 modIndex, const char *variable) { lua_State *L = gLuaState; + if (modIndex >= gActiveMods.entryCount) { + LOG_ERROR("Could not find mod list entry for modIndex: %u", modIndex); + return 0; + } + // figure out entry struct Mod *mod = gActiveMods.entries[modIndex]; if (mod == NULL) {