History

Fix invalid size with GNU designated initializers

the uninitialized cumofs was leading to random sizes for the memset when initializing local structures, potentially leading to segfaults from it. Only a problem with GNU designated initializers, which we didn't test very well. See testcase.

Michael Matz committed 7 years ago

Commit 9e429db1 parent ce1ef5b3 files · +10 −1Browse files

Unified · Split

3 files
  1. tccgen.c +2 −1
  2. tests/tests2/90_struct-init.c +7 −0
  3. tests/tests2/90_struct-init.expect +1 −0
tccgen.c+2−1
@@ -6783,13 +6783,14 @@ static int decl_designator(CType *type, Section *sec, unsigned long c, · 6782 unmodified lines
67836783 c += index * elem_size;
67846784 nb_elems = index_last - index + 1;
67856785 } else {
6786− int cumofs = 0;
6786+ int cumofs;
67876787 next();
67886788 l = tok;
67896789 struct_field:
67906790 next();
67916791 if ((type->t & VT_BTYPE) != VT_STRUCT)
67926792 expect("struct/union type");
6793+ cumofs = 0;
67936794 f = find_field(type, l, &cumofs);
67946795 if (!f)
67956796 expect("field");
tests/tests2/90_struct-init.c+7−0
@@ -175,6 +175,12 @@ void foo (struct W *w, struct pkthdr *phdr_) · 174 unmodified lines
175175 struct T lt2 = { { [1 ... 5] = 9, [6 ... 10] = elt, [4 ... 7] = elt+1 }, 1 };
176176 struct SSU lssu1 = { 5, 3 };
177177 struct SSU lssu2 = { .y = 5, .x = 3 };
178+ /* designated initializers in GNU form */
179+#if defined(__GNUC__) || defined(__TINYC__)
180+ struct S ls4 = {a: 1, b: 2, c: {3, 4}};
181+#else
182+ struct S ls4 = {.a = 1, .b = 2, .c = {3, 4}};
183+#endif
178184 print(ls);
179185 print(ls2);
180186 print(lt);
@@ -194,6 +200,7 @@ void foo (struct W *w, struct pkthdr *phdr_) · 13 unmodified lines
194200 print(lssu1);
195201 print(lssu2);
196202 print(flow);
203+ print(ls4);
197204 }
198205 #endif
199206
tests/tests2/90_struct-init.expect+1−0
@@ -39,6 +39,7 @@ lt2: 0 9 9 9 43 43 43 43 42 42 42 0 0 0 0 0 1 · 38 unmodified lines
3939 lssu1: 5 0 0 0 3 0 0 0
4040 lssu2: 5 0 0 0 3 0 0 0
4141 flow: 9 8 7 6 0 0 0 0 0 0 0 0 0 0 0 0 6 5 4 3 0 0 0 0 0 0 0 0 0 0 0 0
42+ls4: 1 2 3 4
4243 one
4344 two
4445 three

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