Skip to content

Commit

Permalink
Add stack test
Browse files Browse the repository at this point in the history
  • Loading branch information
nuoun committed Jul 30, 2024
1 parent 3c47264 commit 991f8ad
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/common/LuaSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ bool Surge::LuaSupport::setSurgeFunctionEnvironment(lua_State *L)
// stack is now func > table

// List of whitelisted functions and modules
std::vector<std::string> sandboxWhitelist = {"ipairs", "error", "math", "surge"};
std::vector<std::string> sandboxWhitelist = {"ipairs", "error", "math", "surge", "global"};

for (const auto &f : sandboxWhitelist)
{
Expand Down Expand Up @@ -165,16 +165,20 @@ bool Surge::LuaSupport::setSurgeFunctionEnvironment(lua_State *L)
lua_pop(L, 1);

// clear global table
int stacktest = lua_gettop(L);
lua_getglobal(L, "global");
lua_pushnil(L);
while (lua_next(L, -2))
{
lua_pop(L, 1); // pop value
lua_pushvalue(L, -1); // duplicate the key
int stacktest2 = lua_gettop(L);
if (lua_type(L, -1) == LUA_TTABLE && lua_type(L, -2) == LUA_TTABLE && (stacktest2 - stacktest == 1)) {
lua_pushnil(L);
lua_settable(L, -4); // clear the key
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);
}
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 991f8ad

Please sign in to comment.