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