Lua log file: Fix infinite recursion

This commit is contained in:
Lars Mueller 2021-05-26 10:33:34 +02:00
parent 257b9bc4d7
commit ff8a4f6f76

@ -87,6 +87,10 @@ function lua_log_file:_dump(value, is_key)
reference = self.reference_count + 1 reference = self.reference_count + 1
local key = "R[" .. reference .."]" local key = "R[" .. reference .."]"
local formatted local formatted
local function create_reference()
self.reference_count = reference
self.references[value] = reference
end
if _type == "string" then if _type == "string" then
if is_key and value:len() <= key:len() and value:match"[%a_][%a%d_]*" then if is_key and value:len() <= key:len() and value:match"[%a_][%a%d_]*" then
-- Short key -- Short key
@ -97,7 +101,11 @@ function lua_log_file:_dump(value, is_key)
-- Short string -- Short string
return formatted return formatted
end end
-- Use reference
create_reference()
elseif _type == "table" then elseif _type == "table" then
-- Tables always need a reference before they are traversed to prevent infinite recursion
create_reference()
local entries = {} local entries = {}
for _, value in ipairs(value) do for _, value in ipairs(value) do
table.insert(entries, self:_dump(value)) table.insert(entries, self:_dump(value))
@ -113,8 +121,6 @@ function lua_log_file:_dump(value, is_key)
else else
error("unsupported type: " .. _type) error("unsupported type: " .. _type)
end end
self.reference_count = reference
self.references[value] = reference
self:log(key .. "=" .. formatted) self:log(key .. "=" .. formatted)
return key return key
end end