mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-11 15:37:27 +00:00
Fixed reflection setting string not changing size field.
Change-Id: I3c3e88a79667d1733b6c47e43b55d8b2604ca8e2 Tested: on Linux.
This commit is contained in:
@@ -287,14 +287,17 @@ void SetString(const reflection::Schema &schema, const std::string &val,
|
||||
const String *str, std::vector<uint8_t> *flatbuf,
|
||||
const reflection::Object *root_table) {
|
||||
auto delta = static_cast<int>(val.size()) - static_cast<int>(str->Length());
|
||||
auto start = static_cast<uoffset_t>(reinterpret_cast<const uint8_t *>(str) -
|
||||
flatbuf->data() +
|
||||
sizeof(uoffset_t));
|
||||
auto str_start = static_cast<uoffset_t>(
|
||||
reinterpret_cast<const uint8_t *>(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<uoffset_t>(val.size()));
|
||||
}
|
||||
// Copy new data. Safe because we created the right amount of space.
|
||||
memcpy(flatbuf->data() + start, val.c_str(), val.size() + 1);
|
||||
|
||||
@@ -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<const uint8_t *>(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.
|
||||
|
||||
Reference in New Issue
Block a user