Fixed bugs that could cause struct values not to be stored or misaligned

Change-Id: Ie36fe581c000fa4571c96fafd39a9e12fa29e1ca
Tested: on Linux
This commit is contained in:
Wouter van Oortmerssen 2014-08-11 17:38:54 -07:00
parent d8a173ddc7
commit be3c874258
2 changed files with 8 additions and 3 deletions

View File

@ -603,7 +603,7 @@ class FlatBufferBuilder {
template<typename T> Offset<Vector<const T *>> CreateVectorOfStructs( template<typename T> Offset<Vector<const T *>> CreateVectorOfStructs(
const T *v, size_t len) { const T *v, size_t len) {
NotNested(); NotNested();
StartVector(len, AlignOf<T>()); StartVector(len * sizeof(T) / AlignOf<T>(), AlignOf<T>());
PushBytes(reinterpret_cast<const uint8_t *>(v), sizeof(T) * len); PushBytes(reinterpret_cast<const uint8_t *>(v), sizeof(T) * len);
return Offset<Vector<const T *>>(EndVector(len)); return Offset<Vector<const T *>>(EndVector(len));
} }

View File

@ -446,9 +446,13 @@ uoffset_t Parser::ParseTable(const StructDef &struct_def) {
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) \ #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) \
case BASE_TYPE_ ## ENUM: \ case BASE_TYPE_ ## ENUM: \
builder_.Pad(field->padding); \ builder_.Pad(field->padding); \
if (struct_def.fixed) { \
builder_.PushElement(atot<CTYPE>(value.constant.c_str())); \
} else { \
builder_.AddElement(value.offset, \ builder_.AddElement(value.offset, \
atot<CTYPE>( value.constant.c_str()), \ atot<CTYPE>( value.constant.c_str()), \
atot<CTYPE>(field->value.constant.c_str())); \ atot<CTYPE>(field->value.constant.c_str())); \
} \
break; break;
FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD); FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD);
#undef FLATBUFFERS_TD #undef FLATBUFFERS_TD
@ -501,7 +505,8 @@ uoffset_t Parser::ParseVector(const Type &type) {
} }
Next(); Next();
builder_.StartVector(count * InlineSize(type), InlineAlignment((type))); builder_.StartVector(count * InlineSize(type) / InlineAlignment(type),
InlineAlignment(type));
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
// start at the back, since we're building the data backwards. // start at the back, since we're building the data backwards.
auto &val = field_stack_.back().first; auto &val = field_stack_.back().first;