Skip to content

Commit fa97954

Browse files
committed
Do not read past array end in struct return
1 parent 9dffcd2 commit fa97954

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

tccgen.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6706,11 +6706,16 @@ static void gfunc_return(CType *func_type)
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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ typedef struct {
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) {
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
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
0 1 2 3 1234 2345
2+
29

0 commit comments

Comments
 (0)