diff --git a/lua/flatbuffers/binaryarray.lua b/lua/flatbuffers/binaryarray.lua index 3d16c36e1..4d723753e 100644 --- a/lua/flatbuffers/binaryarray.lua +++ b/lua/flatbuffers/binaryarray.lua @@ -50,7 +50,10 @@ function mt:Slice(startPos, endPos) -- updated the startPos based on the size of the -- value while startPos < endPos do - local v = d[startPos] or '/0' + local v = d[startPos] + if not v or v == "" then + v = '/0' + end table.insert(b, v) startPos = startPos + #v end diff --git a/tests/luatest.lua b/tests/luatest.lua index 7380d7d2b..26b0a98de 100644 --- a/tests/luatest.lua +++ b/tests/luatest.lua @@ -222,6 +222,18 @@ local function testCanonicalData() checkReadBuffer(wireData) end +local function testCreateEmptyString() + local b = flatbuffers.Builder(0) + local str = b:CreateString("") + monster.Start(b) + monster.AddName(b, str) + b:Finish(monster.End(b)) + local s = b:Output() + local data = flatbuffers.binaryArray.New(s) + local mon = monster.GetRootAsMonster(data, 0) + assert(mon:Name() == "") +end + local function benchmarkMakeMonster(count, reuseBuilder) local fbb = reuseBuilder and flatbuffers.Builder(0) local length = #(generateMonster(false, fbb)) @@ -285,6 +297,10 @@ local tests = f = testCanonicalData, d = "Tests Canonical flatbuffer file included in repo" }, + { + f = testCreateEmptyString, + d = "Avoid infinite loop when creating empty string" + }, { f = getRootAs_canAcceptString, d = "Tests that GetRootAs() generated methods accept strings"