From 254b4a642ab5ea7623cd1671d951bdc1f53eb082 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Thu, 31 Mar 2022 16:46:24 +0200 Subject: [PATCH] Fix cryptic level 0 Lua error built-in elements sometimes produce If the built-in update function is allowed to run, it can change the particle's type. The code path assumes that there is a Lua update function to call on the particle, but this type change may break this assumption and cause the code to call the update function of an element that doesn't even have one, producing a weird error message with no line number. --- src/lua/LuaScriptInterface.cpp | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/lua/LuaScriptInterface.cpp b/src/lua/LuaScriptInterface.cpp index 54578928a..3593df684 100644 --- a/src/lua/LuaScriptInterface.cpp +++ b/src/lua/LuaScriptInterface.cpp @@ -2949,16 +2949,16 @@ int LuaScriptInterface::elements_allocate(lua_State * l) static int luaUpdateWrapper(UPDATE_FUNC_ARGS) { + auto *builtinUpdate = GetElements()[parts[i].type].Update; + if (builtinUpdate && lua_el_mode[parts[i].type] == 1) + { + if (builtinUpdate(UPDATE_FUNC_SUBCALL_ARGS)) + return 1; + x = (int)(parts[i].x+0.5f); + y = (int)(parts[i].y+0.5f); + } if (lua_el_func[parts[i].type]) { - auto *builtinUpdate = GetElements()[parts[i].type].Update; - if (builtinUpdate && lua_el_mode[parts[i].type] == 1) - { - if (builtinUpdate(UPDATE_FUNC_SUBCALL_ARGS)) - return 1; - x = (int)(parts[i].x+0.5f); - y = (int)(parts[i].y+0.5f); - } int retval = 0, callret; lua_rawgeti(luacon_ci->l, LUA_REGISTRYINDEX, lua_el_func[parts[i].type]); lua_pushinteger(luacon_ci->l, i); @@ -2979,13 +2979,13 @@ static int luaUpdateWrapper(UPDATE_FUNC_ARGS) } x = (int)(parts[i].x+0.5f); y = (int)(parts[i].y+0.5f); - if (builtinUpdate && lua_el_mode[parts[i].type] == 3) - { - if (builtinUpdate(UPDATE_FUNC_SUBCALL_ARGS)) - return 1; - x = (int)(parts[i].x+0.5f); - y = (int)(parts[i].y+0.5f); - } + } + if (builtinUpdate && lua_el_mode[parts[i].type] == 3) + { + if (builtinUpdate(UPDATE_FUNC_SUBCALL_ARGS)) + return 1; + x = (int)(parts[i].x+0.5f); + y = (int)(parts[i].y+0.5f); } return 0; }