Skip to content

Commit

Permalink
Correct lua stack usage in find_callback
Browse files Browse the repository at this point in the history
When we dont match lua_next pops the key.
When we find a match we need to pop both key
and value as we do not call lua_next again.
  • Loading branch information
carlgsmith committed Oct 9, 2023
1 parent 33f87d8 commit 722bf48
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -408,20 +408,18 @@ find_callback (lua_State *L, int index)
{
int ref = 0;
luaL_checktype (L, -1, LUA_TTABLE);
lua_pushvalue (L, -1);
lua_pushnil (L);
/* lua_next pops requested key and pushes next key and value */
lua_pushnil (L); /* push nil key to get first table entry */
while (lua_next (L, -2))
{
lua_pushvalue (L, -2);
if (lua_rawequal (L, index, -2))
if (lua_rawequal (L, index, -1))
{
ref = lua_tonumber (L, -1);
lua_pop (L, 2);
ref = lua_tonumber (L, -2);
lua_pop (L, 2); /* pop key and value */
break;
}
lua_pop(L, 2);
lua_pop(L, 1); /* pop value but leave key on stack for lua_next */
}
lua_pop(L, 1);
return ref;
}

Expand Down

0 comments on commit 722bf48

Please sign in to comment.