diff --git a/src/reflection.cpp b/src/reflection.cpp index 35ae16feb..ab39e1e20 100644 --- a/src/reflection.cpp +++ b/src/reflection.cpp @@ -287,14 +287,17 @@ void SetString(const reflection::Schema &schema, const std::string &val, const String *str, std::vector *flatbuf, const reflection::Object *root_table) { auto delta = static_cast(val.size()) - static_cast(str->Length()); - auto start = static_cast(reinterpret_cast(str) - - flatbuf->data() + - sizeof(uoffset_t)); + auto str_start = static_cast( + reinterpret_cast(str) - flatbuf->data()); + auto start = str_start + sizeof(uoffset_t); if (delta) { // Clear the old string, since we don't want parts of it remaining. memset(flatbuf->data() + start, 0, str->Length()); // Different size, we must expand (or contract). ResizeContext(schema, start, delta, flatbuf, root_table); + // Set the new length. + WriteScalar(flatbuf->data() + str_start, + static_cast(val.size())); } // Copy new data. Safe because we created the right amount of space. memcpy(flatbuf->data() + start, val.c_str(), val.size() + 1); diff --git a/tests/test.cpp b/tests/test.cpp index 10435e3eb..d009438ea 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -404,6 +404,11 @@ void ReflectionTest(uint8_t *flatbuf, size_t length) { rtestarrayofstring->MutateOffset(2, string_ptr); TEST_EQ_STR(rtestarrayofstring->Get(0)->c_str(), "bob"); TEST_EQ_STR(rtestarrayofstring->Get(2)->c_str(), "hank"); + // Test integrity of all resize operations above. + flatbuffers::Verifier resize_verifier( + reinterpret_cast(resizingbuf.data()), + resizingbuf.size()); + TEST_EQ(VerifyMonsterBuffer(resize_verifier), true); // As an additional test, also set it on the name field. // Note: unlike the name change above, this just overwrites the offset, // rather than changing the string in-place.