From: Peter van Dijk Date: Thu, 11 Mar 2021 13:31:14 +0000 (+0100) Subject: lua: don't destroy keys during table iteration X-Git-Tag: dnsdist-1.6.0-rc1~17^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a6fb00c9df403ecc94d385d1637b9dcfb4807e9a;p=thirdparty%2Fpdns.git lua: don't destroy keys during table iteration via https://github.com/ahupowerdns/luawrapper/pull/38 --- diff --git a/ext/luawrapper/include/LuaContext.hpp b/ext/luawrapper/include/LuaContext.hpp index fa2ff91bf2..49aed17bbf 100644 --- a/ext/luawrapper/include/LuaContext.hpp +++ b/ext/luawrapper/include/LuaContext.hpp @@ -2681,11 +2681,21 @@ struct LuaContext::Reader static auto read(lua_State* state, int index) -> boost::optional { + 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::move(result) } : boost::none; } };