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
6783 c += index * elem_size;6783 c += index * elem_size;
6784 nb_elems = index_last - index + 1;6784 nb_elems = index_last - index + 1;
6785 } else {6785 } else {
6786 int cumofs = 0;6786 int cumofs;
6787 next();6787 next();
6788 l = tok;6788 l = tok;
6789 struct_field:6789 struct_field:
6790 next();6790 next();
6791 if ((type->t & VT_BTYPE) != VT_STRUCT)6791 if ((type->t & VT_BTYPE) != VT_STRUCT)
6792 expect("struct/union type");6792 expect("struct/union type");
6793 cumofs = 0;
6793 f = find_field(type, l, &cumofs);6794 f = find_field(type, l, &cumofs);
6794 if (!f)6795 if (!f)
6795 expect("field");6796 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
175 struct T lt2 = { { [1 ... 5] = 9, [6 ... 10] = elt, [4 ... 7] = elt+1 }, 1 };175 struct T lt2 = { { [1 ... 5] = 9, [6 ... 10] = elt, [4 ... 7] = elt+1 }, 1 };
176 struct SSU lssu1 = { 5, 3 };176 struct SSU lssu1 = { 5, 3 };
177 struct SSU lssu2 = { .y = 5, .x = 3 };177 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
178 print(ls);184 print(ls);
179 print(ls2);185 print(ls2);
180 print(lt);186 print(lt);
@@ -194,6 +200,7 @@ void foo (struct W *w, struct pkthdr *phdr_) · 13 unmodified lines
194 print(lssu1);200 print(lssu1);
195 print(lssu2);201 print(lssu2);
196 print(flow);202 print(flow);
203 print(ls4);
197}204}
198#endif205#endif
199 206
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
39lssu1: 5 0 0 0 3 0 0 039lssu1: 5 0 0 0 3 0 0 0
40lssu2: 5 0 0 0 3 0 0 040lssu2: 5 0 0 0 3 0 0 0
41flow: 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 041flow: 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
42ls4: 1 2 3 4
42one43one
43two44two
44three45three

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