diff --git a/src/runtime/builder.c b/src/runtime/builder.c index c4d159d6..98e68b42 100644 --- a/src/runtime/builder.c +++ b/src/runtime/builder.c @@ -608,7 +608,10 @@ static int enter_frame(flatcc_builder_t *B, uint16_t align) static inline void exit_frame(flatcc_builder_t *B) { - memset(B->ds, 0, B->ds_offset); + /* Clear the ds stack (if any struct frames have been allocated). */ + if (B->ds) { + memset(B->ds, 0, B->ds_offset); + } B->ds_offset = frame(ds_offset); B->ds_first = frame(ds_first); refresh_ds(B, frame(type_limit)); diff --git a/test/optional_scalars_test/optional_scalars_test.c b/test/optional_scalars_test/optional_scalars_test.c index 7566c056..391372a2 100644 --- a/test/optional_scalars_test/optional_scalars_test.c +++ b/test/optional_scalars_test/optional_scalars_test.c @@ -21,6 +21,11 @@ int create_scalar_stuff(flatcc_builder_t *builder) { ns(ScalarStuff_start_as_root(builder)); + /* Test of creating a table before any fields are added. */ + ns(NestedTable_ref_t) nested_table; + ns(NestedTable_start(builder)); + nested_table = ns(NestedTable_end(builder)); + ns(ScalarStuff_just_i8_add(builder, 10)); ns(ScalarStuff_maybe_i8_add(builder, 11)); ns(ScalarStuff_default_i8_add(builder, 12)); @@ -49,6 +54,8 @@ int create_scalar_stuff(flatcc_builder_t *builder) ns(ScalarStuff_maybe_xfactor_add)(builder, ns(OptionalFactor_Twice)); ns(ScalarStuff_default_xfactor_add)(builder, ns(OptionalFactor_Twice)); + ns(ScalarStuff_nested_table_add)(builder,nested_table); + ns(ScalarStuff_end_as_root(builder)); return 0; @@ -176,7 +183,7 @@ int test(void) } const char *expected_json = -"{\"just_i8\":10,\"maybe_i8\":11,\"default_i8\":12,\"just_i16\":42,\"maybe_i16\":42,\"maybe_u32\":0,\"default_u32\":0,\"just_f32\":42,\"maybe_f32\":42,\"just_bool\":true,\"maybe_bool\":true,\"just_enum\":\"One\",\"maybe_enum\":\"One\",\"just_xfactor\":\"Twice\",\"maybe_xfactor\":\"Twice\"}"; +"{\"just_i8\":10,\"maybe_i8\":11,\"default_i8\":12,\"just_i16\":42,\"maybe_i16\":42,\"maybe_u32\":0,\"default_u32\":0,\"just_f32\":42,\"maybe_f32\":42,\"just_bool\":true,\"maybe_bool\":true,\"just_enum\":\"One\",\"maybe_enum\":\"One\",\"just_xfactor\":\"Twice\",\"maybe_xfactor\":\"Twice\",\"nested_table\":{}}"; #if 0 int print_buffer(const void *buf, size_t size) diff --git a/test/optional_scalars_test/optional_scalars_test.fbs b/test/optional_scalars_test/optional_scalars_test.fbs index ba4c9d4c..6a3cdd85 100644 --- a/test/optional_scalars_test/optional_scalars_test.fbs +++ b/test/optional_scalars_test/optional_scalars_test.fbs @@ -68,4 +68,10 @@ table ScalarStuff { maybe_yfactor: OptionalFactor = null; default_yfactor: OptionalFactor = Twice; + nested_table: NestedTable; +} + +table NestedTable { + u64_0: uint64 = null; + u64_1: uint64 = null; }