Check serialization string length limit

This commit is contained in:
stujones11 2017-07-05 20:37:45 +01:00
parent 26929187a5
commit f35f4815b8
1 changed files with 33 additions and 21 deletions

54
api.lua
View File

@ -108,27 +108,37 @@ local function restore_facedir(node, delta, yaw)
end end
end end
local function serialize(data) local function inventory_to_table(data)
if data.inventory then for _, list in pairs(data) do
for i, v in pairs(data.inventory) do for i, stack in ipairs(list) do
if type(v) == "string" then local t = ItemStack(stack):to_table() or {}
data.inventory[i] = minetest.compress(v, "deflate", 9) for k, v in pairs(t) do
if type(v) == "string" then
local str = minetest.compress(v, "deflate", 9)
if str:len() > 0xffff then
minetest.log("error",
"String too long for serialization!")
str = ""
end
t[k] = str
end
end end
list[i] = t
end end
end end
return minetest.serialize(data)
end end
local function deserialize(str) local function inventory_from_table(data)
local data = minetest.deserialize(str) or {} for _, list in pairs(data) do
if data.inventory then for i, stack in ipairs(list) do
for i, v in pairs(data.inventory) do for k, v in pairs(stack) do
if type(v) == "string" then if type(v) == "string" and v ~= "" then
data.inventory[i] = minetest.decompress(v, "deflate", 9) stack[k] = minetest.decompress(v, "deflate", 9)
end
end end
list[i] = ItemStack(stack)
end end
end end
return data
end end
meshnode.new_id = function() meshnode.new_id = function()
@ -357,14 +367,10 @@ meshnode.create = function(pos, parent)
local meta_str = nil local meta_str = nil
local meta_tab = meta:to_table() or {} local meta_tab = meta:to_table() or {}
if meta_tab.inventory then if meta_tab.inventory then
for _, list in pairs(meta_tab.inventory) do inventory_to_table(meta_tab.inventory)
for i, stack in ipairs(list) do
list[i] = ItemStack(stack):to_string()
end
end
end end
if next(meta_tab) then if next(meta_tab) then
meta_str = serialize(meta_tab) meta_str = minetest.serialize(meta_tab)
end end
local ref = { local ref = {
id = meshnode.new_id(), id = meshnode.new_id(),
@ -397,7 +403,10 @@ meshnode.restore = function(ref, parent)
end end
if ref.meta then if ref.meta then
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local meta_tab = deserialize(ref.meta) or {} local meta_tab = minetest.deserialize(ref.meta) or {}
if meta_tab.inventory then
inventory_from_table(meta_tab.inventory)
end
meta:from_table(meta_tab) meta:from_table(meta_tab)
end end
end end
@ -446,7 +455,10 @@ meshnode.restore_all = function(parent, name)
end end
if data.ref.meta then if data.ref.meta then
local meta = minetest.get_meta(data.pos) local meta = minetest.get_meta(data.pos)
local meta_tab = deserialize(data.ref.meta) or {} local meta_tab = minetest.deserialize(data.ref.meta) or {}
if meta_tab.inventory then
inventory_from_table(meta_tab.inventory)
end
meta:from_table(meta_tab) meta:from_table(meta_tab)
end end
table.insert(positions, data.pos) table.insert(positions, data.pos)