Skip to content

Commit

Permalink
Fix segfault in testrunner
Browse files Browse the repository at this point in the history
  • Loading branch information
nuoun committed Jul 31, 2024
1 parent 50ebf02 commit a8ca0a9
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/common/LuaSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,23 +164,21 @@ bool Surge::LuaSupport::setSurgeFunctionEnvironment(lua_State *L)
// stack is now f>t>(m). Pop m
lua_pop(L, 1);

// clear global table
int stacktest = lua_gettop(L);
// retrieve the "global" table
lua_getglobal(L, "global");
int stacktest2 = lua_gettop(L);
if (lua_type(L, -1) == LUA_TTABLE && lua_type(L, -2) == LUA_TTABLE &&
(stacktest2 - stacktest == 1))
{
// check if the retrieved value is a table
if (lua_istable(L, -1)) {
lua_pushnil(L);
while (lua_next(L, -2))
{
// set table entries to nil
while (lua_next(L, -2)) {
lua_pop(L, 1); // pop value
lua_pushvalue(L, -1); // duplicate the key
lua_pushnil(L);
lua_settable(L, -4); // clear the key
}
lua_pop(L, 1);
}
// pop the retrieved value (either table or nil) from the stack
lua_pop(L, 1);

// and now we are back to f>t so we can setfenv it
lua_setfenv(L, -2);
Expand Down

0 comments on commit a8ca0a9

Please sign in to comment.