Skip to content

Commit

Permalink
Fix stack overflow from type recursion.
Browse files Browse the repository at this point in the history
  • Loading branch information
Neopallium committed Oct 5, 2017
1 parent 1210d8e commit 1253c85
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
7 changes: 4 additions & 3 deletions pb/standard/dump.lua
Original file line number Diff line number Diff line change
Expand Up @@ -292,23 +292,24 @@ local function get_type_dump(mt)
dump = function(buf, off, msg, depth)
return message(buf, off, msg, fields, depth)
end
register_fields(mt, fields)
register_fields(mt, fields, dump)
elseif mt.is_group then
local fields = mt.fields
dump = function(buf, off, msg, depth)
return group(buf, off, msg, fields, depth)
end
register_fields(mt, fields)
register_fields(mt, fields, dump)
end
-- cache dump function.
mt.dump = dump
end
return dump
end

function register_fields(mt, fields)
function register_fields(mt, fields, dump)
-- check if the fields where already registered.
if mt.dump then return end
mt.dump = dump
for i=1,#fields do
local field = fields[i]
-- check if the field is a user type
Expand Down
8 changes: 4 additions & 4 deletions pb/standard/pack.lua
Original file line number Diff line number Diff line change
Expand Up @@ -460,26 +460,26 @@ local function get_type_pack(mt)
pack = function(buf, off, len, msg)
return message(buf, off, len, msg, fields)
end
register_fields(mt, fields)
register_fields(mt, fields, pack)
elseif mt.is_group then
local fields = mt.fields
-- encode group end tag.
local end_tag = encode_field_tag(mt.tag, wire_types.group_end)
pack = function(buf, off, len, msg)
return group(buf, off, len, msg, fields, end_tag)
end
register_fields(mt, fields)
register_fields(mt, fields, pack)
end
-- cache pack function.
mt.pack = pack
end
return pack
end

function register_fields(mt, fields)
function register_fields(mt, fields, pack)
-- check if the fields where already registered.
if mt.pack then return end
local tags = mt.tags
mt.pack = pack
for i=1,#fields do
local field = fields[i]
local tag = field.tag
Expand Down
8 changes: 4 additions & 4 deletions pb/standard/unpack.lua
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ local function get_type_unpack(mt)
end
return message(data, off, max_off, msg, tags)
end
register_fields(mt)
register_fields(mt, unpack)
elseif mt.is_group then
local tags = mt.tags
local new = mt.new
Expand All @@ -508,18 +508,18 @@ local function get_type_unpack(mt)
end
return group(data, off, max_off, msg, tags, end_tag)
end
register_fields(mt)
register_fields(mt, unpack)
end
-- cache unpack function.
mt.unpack = unpack
end
return unpack
end

function register_fields(mt)
function register_fields(mt, unpack)
-- check if the fields where already registered.
if mt.unpack then return end
local tags = mt.tags
mt.unpack = unpack
local fields = mt.fields
for i=1,#fields do
local field = fields[i]
Expand Down

0 comments on commit 1253c85

Please sign in to comment.