starwing / lua-protobuf

A Lua module to work with Google protobuf
MIT License
1.71k stars 388 forks source link

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

Open Water-Melon opened 10 months ago

Water-Melon commented 10 months ago
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中对这种数据循环引用的情况做一些检查来避免栈满的情况呢?

starwing commented 10 months ago

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