From 0a18fe6cd3cbc288774b83b9223a284f8832c703 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Wed, 24 Dec 2014 16:24:51 -0800 Subject: [PATCH] Fix stack corruption when a script tries to select an invalid line --- libaegisub/include/libaegisub/lua/utils.h | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/libaegisub/include/libaegisub/lua/utils.h b/libaegisub/include/libaegisub/lua/utils.h index 634d9246b..3bf8b2d9a 100644 --- a/libaegisub/include/libaegisub/lua/utils.h +++ b/libaegisub/include/libaegisub/lua/utils.h @@ -140,16 +140,20 @@ struct LuaForEachBreak {}; template void lua_for_each(lua_State *L, Func&& func) { - lua_pushnil(L); // initial key - while (lua_next(L, -2)) { - try { - func(); + { + LuaStackcheck stackcheck(L); + lua_pushnil(L); // initial key + while (lua_next(L, -2)) { + try { + func(); + } + catch (LuaForEachBreak) { + lua_pop(L, 2); // pop value and key + break; + } + lua_pop(L, 1); // pop value, leave key } - catch (LuaForEachBreak) { - lua_pop(L, 1); - break; - } - lua_pop(L, 1); // pop value, leave key + stackcheck.check_stack(0); } lua_pop(L, 1); // pop table }