Skip to content

Commit 6cae381

Browse files
electrodudeJeija
authored andcommittedApr 26, 2016
Luacontroller: Fix remove_functions stack overflow bug
1 parent 4249ed4 commit 6cae381

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed
 

‎mesecons_luacontroller/init.lua

+21-9
Original file line numberDiff line numberDiff line change
@@ -229,23 +229,35 @@ end
229229

230230
local function remove_functions(x)
231231
local tp = type(x)
232-
if tp == "table" then
232+
if tp == "function" then
233+
return nil
234+
end
235+
236+
-- Make sure to not serialize the same table multiple times, otherwise
237+
-- writing mem.test = mem in the LuaController will lead to infinite recursion
238+
local seen = {}
239+
240+
local function rfuncs(x)
241+
if seen[x] then return end
242+
seen[x] = true
243+
if type(x) ~= "table" then return end
244+
233245
for key, value in pairs(x) do
234-
local key_t, val_t = type(key), type(value)
235-
if key_t == "function" or val_t == "function" then
246+
if type(key) == "function" or type(value) == "function" then
236247
x[key] = nil
237248
else
238-
if key_t == "table" then
239-
remove_functions(key)
249+
if type(key) == "table" then
250+
rfuncs(key)
240251
end
241-
if val_t == "table" then
242-
remove_functions(value)
252+
if type(value) == "table" then
253+
rfuncs(value)
243254
end
244255
end
245256
end
246-
elseif tp == "function" then
247-
return nil
248257
end
258+
259+
rfuncs(x)
260+
249261
return x
250262
end
251263

0 commit comments

Comments
 (0)
Please sign in to comment.