From be3c8742585326447a6f9e776ad90a6b0fceb953 Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Mon, 11 Aug 2014 17:38:54 -0700 Subject: [PATCH] Fixed bugs that could cause struct values not to be stored or misaligned Change-Id: Ie36fe581c000fa4571c96fafd39a9e12fa29e1ca Tested: on Linux --- include/flatbuffers/flatbuffers.h | 2 +- src/idl_parser.cpp | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index b8a05a9..5234c4f 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -603,7 +603,7 @@ class FlatBufferBuilder { template Offset> CreateVectorOfStructs( const T *v, size_t len) { NotNested(); - StartVector(len, AlignOf()); + StartVector(len * sizeof(T) / AlignOf(), AlignOf()); PushBytes(reinterpret_cast(v), sizeof(T) * len); return Offset>(EndVector(len)); } diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index 1d9018e..b7cb614 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -446,9 +446,13 @@ uoffset_t Parser::ParseTable(const StructDef &struct_def) { #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) \ case BASE_TYPE_ ## ENUM: \ builder_.Pad(field->padding); \ - builder_.AddElement(value.offset, \ + if (struct_def.fixed) { \ + builder_.PushElement(atot(value.constant.c_str())); \ + } else { \ + builder_.AddElement(value.offset, \ atot( value.constant.c_str()), \ atot(field->value.constant.c_str())); \ + } \ break; FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD); #undef FLATBUFFERS_TD @@ -501,7 +505,8 @@ uoffset_t Parser::ParseVector(const Type &type) { } Next(); - builder_.StartVector(count * InlineSize(type), InlineAlignment((type))); + builder_.StartVector(count * InlineSize(type) / InlineAlignment(type), + InlineAlignment(type)); for (int i = 0; i < count; i++) { // start at the back, since we're building the data backwards. auto &val = field_stack_.back().first; -- 2.7.4