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
67066706 /* returning structure packed into registers */
67076707 int size, addr, align, rc, n;
67086708 size = type_size(func_type,&align);
6709− if ((align & (ret_align - 1))
6710− && ((vtop->r & VT_VALMASK) < VT_CONST /* pointer to struct */
6711− || (vtop->c.i & (ret_align - 1))
6712− )) {
6713− loc = (loc - size) & -ret_align;
6709+ if (ret_nregs * regsize > size ||
6710+ ((align & (ret_align - 1))
6711+ && ((vtop->r & VT_VALMASK) < VT_CONST /* pointer to struct */
6712+ || (vtop->c.i & (ret_align - 1))
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;
67146719 addr = loc;
67156720 type = *func_type;
67166721 vset(&type, VT_LOCAL | VT_LVAL, addr);
tests/tests2/121_struct_return.c+18−0
@@ -6,6 +6,10 @@ typedef struct { · 5 unmodified lines
66 double d2;
77 } Node;
88
9+typedef struct {
10+ int a, b, c;
11+} A;
12+
913 Node init(Node self) {
1014 self.data[0] = 0;
1115 self.data[1] = 1;
@@ -25,11 +29,25 @@ void print_data(Node data) { · 13 unmodified lines
2529 data.d1, data.d2);
2630 }
2731
32+A 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+
2842 int main(void) {
2943 /* This code resulted in a bounds checking error */
3044 Node data;
45+ A a;
3146 dummy (data);
3247 char val;
3348 data = init (data);
3449 print_data(data);
50+ a = test();
51+ printf("%d\n", a.b);
52+ return 0;
3553 }
tests/tests2/121_struct_return.expect+1−0
@@ -1 +1,2 @@
11 0 1 2 3 1234 2345
2+29

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