Skip to content

Instantly share code, notes, and snippets.

@roman-orekhov
Created April 29, 2024 11:20
Show Gist options
  • Save roman-orekhov/a989d7ffd49118eb1f7d8c7d618b9eda to your computer and use it in GitHub Desktop.
Save roman-orekhov/a989d7ffd49118eb1f7d8c7d618b9eda to your computer and use it in GitHub Desktop.
Lua script to check if Lua registry is corrupted
function check_registry()
local reg = debug.getregistry()
local s, cur, min, max = #reg + 1, 0
local ids, lst = {}, {n = 0}
while type(reg[cur]) == "number" do
if ids[cur] then return false, (table.concat(lst, "-")..": cycle "..cur) end
ids[cur] = 1
lst.n = lst.n + 1
lst[lst.n] = cur
if lst.n == 1 then
min = reg[cur]
max = reg[cur]
else
if cur < min then min = cur end
if cur > max then max = cur end
end
cur = reg[cur]
end
local path = table.concat(lst, "-").."#"..cur
if reg[cur] == nil then
if cur ~= s then return false, (path..": nil end at "..cur.." instead of "..s)
else
if min and max then
for i = min, max do
if type(reg[i]) == "number" then
local id = ids[reg[i]]
if id then
if id < 1 then return false, (path..": duplicate int "..reg[i].." within "..min.."-"..max) end
ids[reg[i]] = id - 1
end
end
end
end
return true, path
end
else
return false, (path..": value of type "..type(reg[cur]).." at "..cur.." instead of number or last-nil")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment