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
8237 goto do_init_array;8237 goto do_init_array;
8238 }8238 }
8239 8239
8240 decl_design_flex(p, s, len);
8240 if (!(flags & DIF_SIZE_ONLY)) {8241 if (!(flags & DIF_SIZE_ONLY)) {
8241 int nb = n;8242 int nb = n;
8242 if (len < nb)8243 if (len < nb)
@@ -8270,8 +8271,6 @@ static void decl_initializer(init_params *p, CType *type, unsigned long c, int f · 27 unmodified lines
8270 init_putv(p, t1, c + i * size1);8271 init_putv(p, t1, c + i * size1);
8271 }8272 }
8272 }8273 }
8273 } else {
8274 decl_design_flex(p, s, len);
8275 }8274 }
8276 } else {8275 } else {
8277 8276
tests/tests2/60_errors_and_warnings.c+6−0
@@ -354,6 +354,12 @@ struct c1 c1 = { 1, { 2, 3, 4 } }; · 353 unmodified lines
354struct c2 { int c; struct c1 c1; };354struct c2 { int c; struct c1 c1; };
355struct c2 c2 = { 1, { 2, { 3, 4, 5 }}};355struct c2 c2 = { 1, { 2, { 3, 4, 5 }}};
356 356
357#elif defined test_var_array3
358/* similar to test_var_array2 but with string initializers */
359struct A { int a; char b[]; };
360struct A a = { 1, "1" };
361struct B { struct A a; };
362struct B b = { { 1, "1" } };
357/******************************************************************/363/******************************************************************/
358#elif defined test_default_int_type364#elif defined test_default_int_type
359n; // warn365n; // warn
tests/tests2/60_errors_and_warnings.expect+9−6
@@ -166,20 +166,23 @@ bar : 3 ; 3 · 165 unmodified lines
166[test_var_array2]166[test_var_array2]
16760_errors_and_warnings.c:355: error: flexible array has zero size in this context16760_errors_and_warnings.c:355: error: flexible array has zero size in this context
168 168
169[test_var_array3]
17060_errors_and_warnings.c:362: error: flexible array has zero size in this context
171
169[test_default_int_type]172[test_default_int_type]
17060_errors_and_warnings.c:359: warning: type defaults to int17360_errors_and_warnings.c:365: warning: type defaults to int
171 174
172[test_invalid_global_stmtexpr]175[test_invalid_global_stmtexpr]
17360_errors_and_warnings.c:363: error: statement expression outside of function17660_errors_and_warnings.c:369: error: statement expression outside of function
174 177
175[test_invalid_tokckill]178[test_invalid_tokckill]
17660_errors_and_warnings.c:366: error: ';' expected (got "3")17960_errors_and_warnings.c:372: error: ';' expected (got "3")
177 180
178[test_duplicate_member]181[test_duplicate_member]
17960_errors_and_warnings.c:372: error: duplicate member 'a'18260_errors_and_warnings.c:378: error: duplicate member 'a'
180 183
181[test_duplicate_member_anon]184[test_duplicate_member_anon]
18260_errors_and_warnings.c:385: error: duplicate member 'd'18560_errors_and_warnings.c:391: error: duplicate member 'd'
183 186
184[test_conflicting_array_definition]187[test_conflicting_array_definition]
18560_errors_and_warnings.c:390: error: incompatible types for redefinition of 'array'18860_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