From 6cae381c2788d2e4061c530cc143ca38bd0862e3 Mon Sep 17 00:00:00 2001 From: electrodude Date: Wed, 20 Apr 2016 18:09:38 -0400 Subject: Luacontroller: Fix `remove_functions` stack overflow bug --- mesecons_luacontroller/init.lua | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/mesecons_luacontroller/init.lua b/mesecons_luacontroller/init.lua index e669f93..01f5878 100644 --- a/mesecons_luacontroller/init.lua +++ b/mesecons_luacontroller/init.lua @@ -229,23 +229,35 @@ end local function remove_functions(x) local tp = type(x) - if tp == "table" then + if tp == "function" then + return nil + end + + -- Make sure to not serialize the same table multiple times, otherwise + -- writing mem.test = mem in the LuaController will lead to infinite recursion + local seen = {} + + local function rfuncs(x) + if seen[x] then return end + seen[x] = true + if type(x) ~= "table" then return end + for key, value in pairs(x) do - local key_t, val_t = type(key), type(value) - if key_t == "function" or val_t == "function" then + if type(key) == "function" or type(value) == "function" then x[key] = nil else - if key_t == "table" then - remove_functions(key) + if type(key) == "table" then + rfuncs(key) end - if val_t == "table" then - remove_functions(value) + if type(value) == "table" then + rfuncs(value) end end end - elseif tp == "function" then - return nil end + + rfuncs(x) + return x end -- cgit v1.2.3