Fixed reflection setting string not changing size field.

Change-Id: I3c3e88a79667d1733b6c47e43b55d8b2604ca8e2
Tested: on Linux.
This commit is contained in:
Wouter van Oortmerssen
2015-12-21 16:17:50 -08:00
parent d7d527d154
commit 1891537236
2 changed files with 11 additions and 3 deletions

View File

@@ -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);