Skip to content

Commit

Permalink
lua: don't destroy keys during table iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
Habbie committed Mar 18, 2020
1 parent 95aa266 commit 33c19e1
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions ext/luawrapper/include/LuaContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2649,11 +2649,21 @@ struct LuaContext::Reader<std::string>
static auto read(lua_State* state, int index)
-> boost::optional<std::string>
{
std::string result;

// lua_tolstring might convert the variable that would confuse lua_next, so we
// make a copy of the variable.
lua_pushvalue(state, index);

size_t len;
const auto val = lua_tolstring(state, index, &len);
if (val == 0)
return boost::none;
return std::string(val, len);
const auto val = lua_tolstring(state, -1, &len);

if (val != 0)
result.assign(val, len);

lua_pop(state, 1);

return val != 0 ? boost::optional<std::string>{ std::move(result) } : boost::none;
}
};

Expand Down

0 comments on commit 33c19e1

Please sign in to comment.