History

Do not read past array end in struct return

herman ten brugge committed 1 year ago

Commit fa979541 parent 9dffcd23 files · +29 −5Browse files

Unified · Split

3 files
  1. tccgen.c +10 −5
  2. tests/tests2/121_struct_return.c +18 −0
  3. tests/tests2/121_struct_return.expect +1 −0
tccgen.c+10−5
@@ -6706,11 +6706,16 @@ static void gfunc_return(CType *func_type) · 6705 unmodified lines
6706 /* returning structure packed into registers */6706 /* returning structure packed into registers */
6707 int size, addr, align, rc, n;6707 int size, addr, align, rc, n;
6708 size = type_size(func_type,&align);6708 size = type_size(func_type,&align);
6709 if ((align & (ret_align - 1))6709 if (ret_nregs * regsize > size ||
6710 && ((vtop->r & VT_VALMASK) < VT_CONST /* pointer to struct */6710 ((align & (ret_align - 1))
6711 || (vtop->c.i & (ret_align - 1))6711 && ((vtop->r & VT_VALMASK) < VT_CONST /* pointer to struct */
6712 )) {6712 || (vtop->c.i & (ret_align - 1))
6713 loc = (loc - size) & -ret_align;6713 ))) {
6714 if (ret_nregs * regsize > size)
6715 size = ret_nregs * regsize;
6716 if (ret_align > align)
6717 align = ret_align;
6718 loc = (loc - size) & -align;
6714 addr = loc;6719 addr = loc;
6715 type = *func_type;6720 type = *func_type;
6716 vset(&type, VT_LOCAL | VT_LVAL, addr);6721 vset(&type, VT_LOCAL | VT_LVAL, addr);
tests/tests2/121_struct_return.c+18−0
@@ -6,6 +6,10 @@ typedef struct { · 5 unmodified lines
6 double d2;6 double d2;
7} Node;7} Node;
8 8
9typedef struct {
10 int a, b, c;
11} A;
12
9Node init(Node self) {13Node init(Node self) {
10 self.data[0] = 0;14 self.data[0] = 0;
11 self.data[1] = 1;15 self.data[1] = 1;
@@ -25,11 +29,25 @@ void print_data(Node data) { · 13 unmodified lines
25 data.d1, data.d2);29 data.d1, data.d2);
26}30}
27 31
32A test(void)
33{
34 int i;
35 A arr[30];
36
37 for (i = 0; i < 30; i++)
38 arr[i].b = i;
39 return arr[29];
40}
41
28int main(void) {42int main(void) {
29 /* This code resulted in a bounds checking error */43 /* This code resulted in a bounds checking error */
30 Node data;44 Node data;
45 A a;
31 dummy (data);46 dummy (data);
32 char val;47 char val;
33 data = init (data);48 data = init (data);
34 print_data(data);49 print_data(data);
50 a = test();
51 printf("%d\n", a.b);
52 return 0;
35}53}
tests/tests2/121_struct_return.expect+1−0
@@ -1 +1,2 @@
10 1 2 3 1234 234510 1 2 3 1234 2345
229

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