History

Fix segfault with nested flex array structs

when used with string initialization the size of nested struct flex array member was tested too late for < 0. GCC accepts but discards such initializers (the flex array member will become zero sized). TCC supports flex arrays members only in top-level structs and gives an error in this case.

Michael Matz committed 5 years ago

Commit 52013121 parent 3658c293 files · +16 −8Browse files

Unified · Split

3 files
  1. tccgen.c +1 −2
  2. tests/tests2/60_errors_and_warnings.c +6 −0
  3. tests/tests2/60_errors_and_warnings.expect +9 −6
tccgen.c+1−2
@@ -8237,6 +8237,7 @@ static void decl_initializer(init_params *p, CType *type, unsigned long c, int f · 8236 unmodified lines
82378237 goto do_init_array;
82388238 }
82398239
8240+ decl_design_flex(p, s, len);
82408241 if (!(flags & DIF_SIZE_ONLY)) {
82418242 int nb = n;
82428243 if (len < nb)
@@ -8270,8 +8271,6 @@ static void decl_initializer(init_params *p, CType *type, unsigned long c, int f · 27 unmodified lines
82708271 init_putv(p, t1, c + i * size1);
82718272 }
82728273 }
8273− } else {
8274− decl_design_flex(p, s, len);
82758274 }
82768275 } else {
82778276
tests/tests2/60_errors_and_warnings.c+6−0
@@ -354,6 +354,12 @@ struct c1 c1 = { 1, { 2, 3, 4 } }; · 353 unmodified lines
354354 struct c2 { int c; struct c1 c1; };
355355 struct c2 c2 = { 1, { 2, { 3, 4, 5 }}};
356356
357+#elif defined test_var_array3
358+/* similar to test_var_array2 but with string initializers */
359+struct A { int a; char b[]; };
360+struct A a = { 1, "1" };
361+struct B { struct A a; };
362+struct B b = { { 1, "1" } };
357363 /******************************************************************/
358364 #elif defined test_default_int_type
359365 n; // warn
tests/tests2/60_errors_and_warnings.expect+9−6
@@ -166,20 +166,23 @@ bar : 3 ; 3 · 165 unmodified lines
166166 [test_var_array2]
167167 60_errors_and_warnings.c:355: error: flexible array has zero size in this context
168168
169+[test_var_array3]
170+60_errors_and_warnings.c:362: error: flexible array has zero size in this context
171+
169172 [test_default_int_type]
170−60_errors_and_warnings.c:359: warning: type defaults to int
173+60_errors_and_warnings.c:365: warning: type defaults to int
171174
172175 [test_invalid_global_stmtexpr]
173−60_errors_and_warnings.c:363: error: statement expression outside of function
176+60_errors_and_warnings.c:369: error: statement expression outside of function
174177
175178 [test_invalid_tokckill]
176−60_errors_and_warnings.c:366: error: ';' expected (got "3")
179+60_errors_and_warnings.c:372: error: ';' expected (got "3")
177180
178181 [test_duplicate_member]
179−60_errors_and_warnings.c:372: error: duplicate member 'a'
182+60_errors_and_warnings.c:378: error: duplicate member 'a'
180183
181184 [test_duplicate_member_anon]
182−60_errors_and_warnings.c:385: error: duplicate member 'd'
185+60_errors_and_warnings.c:391: error: duplicate member 'd'
183186
184187 [test_conflicting_array_definition]
185−60_errors_and_warnings.c:390: error: incompatible types for redefinition of 'array'
188+60_errors_and_warnings.c:396: error: incompatible types for redefinition of 'array'

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t