Fixed bugs that could cause struct values not to be stored or misaligned
authorWouter van Oortmerssen <wvo@google.com>
Tue, 12 Aug 2014 00:38:54 +0000 (17:38 -0700)
committerWouter van Oortmerssen <wvo@google.com>
Tue, 12 Aug 2014 00:42:55 +0000 (17:42 -0700)
Change-Id: Ie36fe581c000fa4571c96fafd39a9e12fa29e1ca
Tested: on Linux

include/flatbuffers/flatbuffers.h
src/idl_parser.cpp

index b8a05a9..5234c4f 100644 (file)
@@ -603,7 +603,7 @@ class FlatBufferBuilder {
   template<typename T> Offset<Vector<const T *>> CreateVectorOfStructs(
                                                       const T *v, size_t len) {
     NotNested();
-    StartVector(len, AlignOf<T>());
+    StartVector(len * sizeof(T) / AlignOf<T>(), AlignOf<T>());
     PushBytes(reinterpret_cast<const uint8_t *>(v), sizeof(T) * len);
     return Offset<Vector<const T *>>(EndVector(len));
   }
index 1d9018e..b7cb614 100644 (file)
@@ -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<CTYPE>(value.constant.c_str())); \
+              } else { \
+                builder_.AddElement(value.offset, \
                              atot<CTYPE>(       value.constant.c_str()), \
                              atot<CTYPE>(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;