Skip to content

Commit

Permalink
Fix crashes if returning invalid arguments from graphics or ctypeDraw…
Browse files Browse the repository at this point in the history
… lua functions
  • Loading branch information
jacob1 committed Nov 25, 2019
1 parent b091582 commit 876a9b3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
30 changes: 20 additions & 10 deletions src/lua/LegacyLuaAPI.cpp
Expand Up @@ -396,16 +396,26 @@ int luacon_graphicsReplacement(GRAPHICS_FUNC_ARGS, int i)
}
else
{
cache = luaL_optint(luacon_ci->l, -10, 0);
*pixel_mode = luaL_optint(luacon_ci->l, -9, *pixel_mode);
*cola = luaL_optint(luacon_ci->l, -8, *cola);
*colr = luaL_optint(luacon_ci->l, -7, *colr);
*colg = luaL_optint(luacon_ci->l, -6, *colg);
*colb = luaL_optint(luacon_ci->l, -5, *colb);
*firea = luaL_optint(luacon_ci->l, -4, *firea);
*firer = luaL_optint(luacon_ci->l, -3, *firer);
*fireg = luaL_optint(luacon_ci->l, -2, *fireg);
*fireb = luaL_optint(luacon_ci->l, -1, *fireb);
bool valid = true;
for (int i = -10; i < 0; i++)
if (!lua_isnumber(luacon_ci->l, i))
{
valid = false;
break;
}
if (valid)
{
cache = luaL_optint(luacon_ci->l, -10, 0);
*pixel_mode = luaL_optint(luacon_ci->l, -9, *pixel_mode);
*cola = luaL_optint(luacon_ci->l, -8, *cola);
*colr = luaL_optint(luacon_ci->l, -7, *colr);
*colg = luaL_optint(luacon_ci->l, -6, *colg);
*colb = luaL_optint(luacon_ci->l, -5, *colb);
*firea = luaL_optint(luacon_ci->l, -4, *firea);
*firer = luaL_optint(luacon_ci->l, -3, *firer);
*fireg = luaL_optint(luacon_ci->l, -2, *fireg);
*fireb = luaL_optint(luacon_ci->l, -1, *fireb);
}
lua_pop(luacon_ci->l, 10);
}
return cache;
Expand Down
7 changes: 4 additions & 3 deletions src/lua/LuaScriptInterface.cpp
Expand Up @@ -2701,7 +2701,8 @@ static bool luaCtypeDrawWrapper(CTYPEDRAW_FUNC_ARGS)
}
else
{
ret = luaL_optinteger(luacon_ci->l, -1, 0);
if (lua_isboolean(luacon_ci->l, -1))
ret = lua_toboolean(luacon_ci->l, -1);
lua_pop(luacon_ci->l, 1);
}
}
Expand Down Expand Up @@ -2864,7 +2865,7 @@ int LuaScriptInterface::elements_property(lua_State * l)
{
lua_gr_func[id].Assign(3);
}
else if (lua_type(l, 3) == LUA_TBOOLEAN && !lua_toboolean(l, -1))
else if (lua_type(l, 3) == LUA_TBOOLEAN && !lua_toboolean(l, 3))
{
lua_gr_func[id].Clear();
luacon_sim->elements[id].Graphics = NULL;
Expand All @@ -2879,7 +2880,7 @@ int LuaScriptInterface::elements_property(lua_State * l)
lua_cd_func[id].Assign(3);
luacon_sim->elements[id].CtypeDraw = luaCtypeDrawWrapper;
}
else if (lua_type(l, 3) == LUA_TBOOLEAN && !lua_toboolean(l, -1))
else if (lua_type(l, 3) == LUA_TBOOLEAN && !lua_toboolean(l, 3))
{
lua_cd_func[id].Clear();
luacon_sim->elements[id].CtypeDraw = nullptr;
Expand Down

0 comments on commit 876a9b3

Please sign in to comment.