Skip to content

Commit 50f28a0

Browse files
authored
Merge pull request #344 from Nordix/ubsan-null-ptr
Correcting an UBSan error in builder
2 parents d8ddb2c + 4d980b1 commit 50f28a0

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

src/runtime/builder.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,10 @@ static int enter_frame(flatcc_builder_t *B, uint16_t align)
608608

609609
static inline void exit_frame(flatcc_builder_t *B)
610610
{
611-
memset(B->ds, 0, B->ds_offset);
611+
/* Clear the ds stack (if any struct frames have been allocated). */
612+
if (B->ds) {
613+
memset(B->ds, 0, B->ds_offset);
614+
}
612615
B->ds_offset = frame(ds_offset);
613616
B->ds_first = frame(ds_first);
614617
refresh_ds(B, frame(type_limit));

test/optional_scalars_test/optional_scalars_test.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ int create_scalar_stuff(flatcc_builder_t *builder)
2121
{
2222
ns(ScalarStuff_start_as_root(builder));
2323

24+
/* Test of creating a table before any fields are added. */
25+
ns(NestedTable_ref_t) nested_table;
26+
ns(NestedTable_start(builder));
27+
nested_table = ns(NestedTable_end(builder));
28+
2429
ns(ScalarStuff_just_i8_add(builder, 10));
2530
ns(ScalarStuff_maybe_i8_add(builder, 11));
2631
ns(ScalarStuff_default_i8_add(builder, 12));
@@ -49,6 +54,8 @@ int create_scalar_stuff(flatcc_builder_t *builder)
4954
ns(ScalarStuff_maybe_xfactor_add)(builder, ns(OptionalFactor_Twice));
5055
ns(ScalarStuff_default_xfactor_add)(builder, ns(OptionalFactor_Twice));
5156

57+
ns(ScalarStuff_nested_table_add)(builder,nested_table);
58+
5259
ns(ScalarStuff_end_as_root(builder));
5360

5461
return 0;
@@ -176,7 +183,7 @@ int test(void)
176183
}
177184

178185
const char *expected_json =
179-
"{\"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\"}";
186+
"{\"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\":{}}";
180187

181188
#if 0
182189
int print_buffer(const void *buf, size_t size)

test/optional_scalars_test/optional_scalars_test.fbs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,10 @@ table ScalarStuff {
6868
maybe_yfactor: OptionalFactor = null;
6969
default_yfactor: OptionalFactor = Twice;
7070

71+
nested_table: NestedTable;
72+
}
73+
74+
table NestedTable {
75+
u64_0: uint64 = null;
76+
u64_1: uint64 = null;
7177
}

0 commit comments

Comments
 (0)