lua-protobuf icon indicating copy to clipboard operation
lua-protobuf copied to clipboard

循环引用场景下会导致stack overflow

Open Water-Melon opened this issue 2 years ago • 1 comments

local pb = require "pb"
local protoc = require "protoc"

assert(protoc:load [[
   message Phone {
      optional string name        = 1;
      optional int64  phonenumber = 2;
      optional Person item        = 3;
   }
   message Person {
      optional string name     = 1;
      optional int32  age      = 2;
      optional string address  = 3;
      repeated Phone  contacts = 4;
   } ]])

local data = {
   name = "ilse",
   age  = 18,
   contacts = {
      { name = "alice", phonenumber = 12312341234 }
   }
}
data.contacts[1].item = data

local bytes = pb.encode("Person", data)

执行这段代码会得到如下结果

lua: a.lua:26: stack overflow (message too many levels)
stack traceback:
	[C]: in function 'encode'
	a.lua:26: in main chunk
	[C]: ?

虽然可以利用pcall来解决,但是是否应该在encode中对这种数据循环引用的情况做一些检查来避免栈满的情况呢?

Water-Melon avatar Sep 11 '23 10:09 Water-Melon

这种肯定是直接报error的,没别的处理方法。lua-pb的设计是尽量不做检查,以提高性能。

starwing avatar Sep 12 '23 12:09 starwing