Skip to content

Commit 4d980b1

Browse files
committed
Correcting a UBSan error in builder
If a table with optional fields is created before the ds-stack is allocated we get an UBSan error. The ds stack would normally be allocated when a field is added to the table. /xxxx/flatcc/src/runtime/builder.c:613:16: runtime error: null pointer passed as argument 1, which is declared to never be null /usr/include/string.h:61:62: note: nonnull attribute specified here #0 0x600c6b6e in exit_frame /xxxx/flatcc/src/runtime/builder.c:613:9 #1 0x600d4520 in flatcc_builder_end_table /xxxx/flatcc/src/runtime/builder.c:1380:5 #2 0x600a0be8 in optional_scalars_NestedTable_end /xxxx/flatcc/build/Debug/test/optional_scalars_test/generated/optional_scalars_test_builder.h:35:1 #3 0x600a08a7 in create_scalar_stuff /xxxx/flatcc/test/optional_scalars_test/optional_scalars_test.c:27:20 #4 0x600ab4f5 in test /xxxx/flatcc/test/optional_scalars_test/optional_scalars_test.c:176:5 #5 0x600abd1f in main /xxxx/flatcc/test/optional_scalars_test/optional_scalars_test.c:272:9 #6 0xe9186518 (/lib/i386-linux-gnu/libc.so.6+0x21518) (BuildId: 6f8a2d2f90a25e7865aa6fdfd9a7825d62d53f51) #7 0xe91865f2 in __libc_start_main (/lib/i386-linux-gnu/libc.so.6+0x215f2) (BuildId: 6f8a2d2f90a25e7865aa6fdfd9a7825d62d53f51) #8 0x6007053a in _start (/xxxx/flatcc/build/Debug/test/optional_scalars_test/optional_scalars_test_d+0x2653a) (BuildId: d47f8a02ee936bc1bc53a0efd6ad360faa6428d7) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /xxxx/flatcc/src/runtime/builder.c:613:16 Signed-off-by: Björn Svensson <[email protected]>
1 parent e32c6f6 commit 4d980b1

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)