From c37c989ed6bce5075ce7bf528fd6e531765d6afb Mon Sep 17 00:00:00 2001 From: Dmitriy Kovalev Date: Thu, 26 Mar 2020 08:54:52 -0700 Subject: [PATCH] Correct calculation of vector element size (#5831) Number of elements on the stack shouldn't affect the calculation of ElemWidth(). Variable 'start' needs to be subtracted from the loop variable 'i' to make indexing zero-based. There is an additional unit test to pack nested vectors. Size of the packed buffer *without* this fix is 798 and only 664 bytes *with* the fix. --- include/flatbuffers/flexbuffers.h | 3 ++- tests/test.cpp | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/include/flatbuffers/flexbuffers.h b/include/flatbuffers/flexbuffers.h index dceaa3b41..4dd71ae1b 100644 --- a/include/flatbuffers/flexbuffers.h +++ b/include/flatbuffers/flexbuffers.h @@ -1522,7 +1522,8 @@ class Builder FLATBUFFERS_FINAL_CLASS { Type vector_type = FBT_KEY; // Check bit widths and types for all elements. for (size_t i = start; i < stack_.size(); i += step) { - auto elem_width = stack_[i].ElemWidth(buf_.size(), i + prefix_elems); + auto elem_width = + stack_[i].ElemWidth(buf_.size(), i - start + prefix_elems); bit_width = (std::max)(bit_width, elem_width); if (typed) { if (i == start) { diff --git a/tests/test.cpp b/tests/test.cpp index 408194482..87a11691d 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -2885,6 +2885,17 @@ void FlexBuffersTest() { // And from FlexBuffer back to JSON: auto jsonback = jroot.ToString(); TEST_EQ_STR(jsontest, jsonback.c_str()); + + slb.Clear(); + slb.Vector([&]() { + for (int i = 0; i < 130; ++i) slb.Add(static_cast(255)); + slb.Vector([&]() { + for (int i = 0; i < 130; ++i) slb.Add(static_cast(255)); + slb.Vector([] {}); + }); + }); + slb.Finish(); + TEST_EQ(slb.GetSize(), 664); } void FlexBuffersDeprecatedTest() {