Skip to content
Snippets Groups Projects
Commit e9c41b82 authored by Vladislav Shpilevoy's avatar Vladislav Shpilevoy Committed by Kirill Yukhin
Browse files

msgpack: make msgpackffi use encode_max_depth option

Msgpack Lua module is not a simple set of functions. It is a
global serializer object used by plenty of other Lua and C
modules. Msgpack as a serializer can be configured, and in theory
its configuration updates should affect all other modules. For
example, a user could change encode_max_depth:

    require('msgpack').cfg({encode_max_depth = <new_value>})

And that would make tuple:update() accept tables with <new_value>
depth without a crop.

But in fact msgpack configuration didn't affect some places, such
as this one. And all the others who use msgpackffi.

This patch fixes it, for encode_max_depth option. Other options
are still ignored.

Part of #4434

(cherry picked from commit 4bb253f7)
parent ad46eb01
No related branches found
No related tags found
No related merge requests found
...@@ -4,7 +4,6 @@ local ffi = require('ffi') ...@@ -4,7 +4,6 @@ local ffi = require('ffi')
local buffer = require('buffer') local buffer = require('buffer')
local builtin = ffi.C local builtin = ffi.C
local msgpack = require('msgpack') -- .NULL, .array_mt, .map_mt, .cfg local msgpack = require('msgpack') -- .NULL, .array_mt, .map_mt, .cfg
local MAXNESTING = 16
local int8_ptr_t = ffi.typeof('int8_t *') local int8_ptr_t = ffi.typeof('int8_t *')
local uint8_ptr_t = ffi.typeof('uint8_t *') local uint8_ptr_t = ffi.typeof('uint8_t *')
local uint16_ptr_t = ffi.typeof('uint16_t *') local uint16_ptr_t = ffi.typeof('uint16_t *')
...@@ -206,7 +205,7 @@ local function encode_r(buf, obj, level) ...@@ -206,7 +205,7 @@ local function encode_r(buf, obj, level)
elseif type(obj) == "string" then elseif type(obj) == "string" then
encode_str(buf, obj) encode_str(buf, obj)
elseif type(obj) == "table" then elseif type(obj) == "table" then
if level >= MAXNESTING then -- Limit nested tables if level >= msgpack.cfg.encode_max_depth then
encode_nil(buf) encode_nil(buf)
return return
end end
......
...@@ -36,7 +36,7 @@ local function test_offsets(test, s) ...@@ -36,7 +36,7 @@ local function test_offsets(test, s)
end end
local function test_other(test, s) local function test_other(test, s)
test:plan(19) test:plan(23)
local buf = string.char(0x93, 0x6e, 0xcb, 0x42, 0x2b, 0xed, 0x30, 0x47, local buf = string.char(0x93, 0x6e, 0xcb, 0x42, 0x2b, 0xed, 0x30, 0x47,
0x6f, 0xff, 0xff, 0xac, 0x77, 0x6b, 0x61, 0x71, 0x66, 0x7a, 0x73, 0x6f, 0xff, 0xff, 0xac, 0x77, 0x6b, 0x61, 0x71, 0x66, 0x7a, 0x73,
0x7a, 0x75, 0x71, 0x71, 0x78) 0x7a, 0x75, 0x71, 0x71, 0x78)
...@@ -68,6 +68,44 @@ local function test_other(test, s) ...@@ -68,6 +68,44 @@ local function test_other(test, s)
test:is(#s.encode(-0x8001), 5, "len(encode(-0x8001))") test:is(#s.encode(-0x8001), 5, "len(encode(-0x8001))")
test:is(#s.encode(-0x80000000), 5, "len(encode(-0x80000000))") test:is(#s.encode(-0x80000000), 5, "len(encode(-0x80000000))")
test:is(#s.encode(-0x80000001), 9, "len(encode(-0x80000001))") test:is(#s.encode(-0x80000001), 9, "len(encode(-0x80000001))")
--
-- gh-4434: msgpackffi does not care about msgpack serializer
-- configuration, but it should.
--
local function check_depth(depth_to_try)
local t = nil
for i = 1, depth_to_try do t = {t} end
t = s.decode_unchecked(s.encode(t))
local level = 0
while t ~= nil do level = level + 1 t = t[1] end
return level
end
local msgpack = require('msgpack')
local max_depth = msgpack.cfg.encode_max_depth
local result_depth = check_depth(max_depth + 5)
test:is(result_depth, max_depth,
"msgpackffi uses msgpack.cfg.encode_max_depth")
msgpack.cfg({encode_max_depth = max_depth + 5})
result_depth = check_depth(max_depth + 5)
test:is(result_depth, max_depth + 5, "and uses it dynamically")
-- Recursive tables are handled correctly.
local level = 0
local t = {}
t[1] = t
t = s.decode(s.encode(t))
while t ~= nil do level = level + 1 t = t[1] end
test:is(level, max_depth + 5, "recursive array")
t = {}
t.key = t
t = s.decode(s.encode(t))
level = 0
while t ~= nil do level = level + 1 t = t.key end
test:is(level, max_depth + 5, "recursive map")
msgpack.cfg({encode_max_depth = max_depth})
end end
tap.test("msgpackffi", function(test) tap.test("msgpackffi", function(test)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment