From c25bf4c8d3bb8faebcebab06517068a2799a9452 Mon Sep 17 00:00:00 2001 From: MysterD Date: Sat, 16 Apr 2022 20:27:51 -0700 Subject: [PATCH] Check hook return types before using them --- src/pc/lua/smlua_hooks.c | 11 +++++++---- src/pc/lua/smlua_utils.c | 5 ++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/pc/lua/smlua_hooks.c b/src/pc/lua/smlua_hooks.c index 09c7d432..a57885c9 100644 --- a/src/pc/lua/smlua_hooks.c +++ b/src/pc/lua/smlua_hooks.c @@ -440,11 +440,11 @@ bool smlua_call_action_hook(struct MarioState* m, s32* returnValue) { } // output the return value - *returnValue = smlua_to_integer(L, -1); + if (lua_type(L, -1) == LUA_TBOOLEAN || lua_type(L, -1) == LUA_TNUMBER) { + *returnValue = smlua_to_integer(L, -1); + } lua_pop(L, 1); - if (!gSmLuaConvertSuccess) { return false; } - return true; } } @@ -753,7 +753,10 @@ bool smlua_call_chat_command_hook(char* command) { } // output the return value - bool returnValue = smlua_to_boolean(L, -1); + bool returnValue = false; + if (lua_type(L, -1) == LUA_TBOOLEAN) { + returnValue = smlua_to_boolean(L, -1); + } lua_pop(L, 1); if (!gSmLuaConvertSuccess) { return false; } diff --git a/src/pc/lua/smlua_utils.c b/src/pc/lua/smlua_utils.c index e91019fe..0eaa970f 100644 --- a/src/pc/lua/smlua_utils.c +++ b/src/pc/lua/smlua_utils.c @@ -66,7 +66,10 @@ lua_Integer smlua_to_integer(lua_State* L, int index) { } lua_Number smlua_to_number(lua_State* L, int index) { - if (lua_type(L, index) != LUA_TNUMBER) { + if (lua_type(L, index) == LUA_TBOOLEAN) { + gSmLuaConvertSuccess = true; + return lua_toboolean(L, index) ? 1 : 0; + } else if (lua_type(L, index) != LUA_TNUMBER) { LOG_LUA("smlua_to_number received improper type '%d'", lua_type(L, index)); smlua_logline(); gSmLuaConvertSuccess = false;