]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
lua: don't destroy keys during table iteration
authorPeter van Dijk <peter.van.dijk@powerdns.com>
Thu, 11 Mar 2021 13:31:14 +0000 (14:31 +0100)
committerPeter van Dijk <peter.van.dijk@powerdns.com>
Thu, 11 Mar 2021 13:31:14 +0000 (14:31 +0100)
via https://github.com/ahupowerdns/luawrapper/pull/38

ext/luawrapper/include/LuaContext.hpp

index fa2ff91bf29d5298dd87615a45898cc09749ea35..49aed17bbf524b99feba9430361c5c6f4ea63007 100644 (file)
@@ -2681,11 +2681,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;
     }
 };