diff options
author | 罗泽轩 <spacewanderlzx@gmail.com> | 2021-05-06 10:39:42 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-05 19:39:42 -0700 |
commit | a4bb8f0c2e763c75731dc21a6f30240380d0cded (patch) | |
tree | 9f8748788ca90d2a65bdff55ce200e01a1e57e33 | |
parent | 82aed82b8462ec1f083d9471edef2868692ded6a (diff) | |
download | flatbuffers-a4bb8f0c2e763c75731dc21a6f30240380d0cded.tar.gz flatbuffers-a4bb8f0c2e763c75731dc21a6f30240380d0cded.tar.bz2 flatbuffers-a4bb8f0c2e763c75731dc21a6f30240380d0cded.zip |
[Lua] Avoid infinite loop when creating empty string (#6614)
* [Lua] Avoid infinite loop when creating empty string
* [Lua] Check empty string output
-rw-r--r-- | lua/flatbuffers/binaryarray.lua | 5 | ||||
-rw-r--r-- | tests/luatest.lua | 16 |
2 files changed, 20 insertions, 1 deletions
diff --git a/lua/flatbuffers/binaryarray.lua b/lua/flatbuffers/binaryarray.lua index 3d16c36e..4d723753 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 7380d7d2..26b0a98d 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)) @@ -286,6 +298,10 @@ local tests = 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<type>() generated methods accept strings" }, |