File: Warray-bounds-11.c

package info (click to toggle)
gcc-riscv64-unknown-elf 8.3.0.2019.08%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 680,956 kB
  • sloc: ansic: 3,237,715; cpp: 896,882; ada: 772,854; f90: 144,254; asm: 68,788; makefile: 67,456; sh: 29,743; exp: 28,045; objc: 15,273; fortran: 11,885; python: 7,369; pascal: 5,375; awk: 3,725; perl: 2,872; yacc: 316; xml: 311; ml: 285; lex: 198; haskell: 122
file content (103 lines) | stat: -rw-r--r-- 1,873 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* { dg-do compile } */
/* { dg-options "-O3 -Warray-bounds=2" } */

typedef __SIZE_TYPE__ size_t;
extern void* malloc(size_t x);

int e[3];

struct f { int f[3]; };

extern void bar(int v[]);

struct h {

	int i;
	int j[];
};

struct h0 {

	int i;
	int j[0];
};

struct h0b {

	int i;
	int j[0];
	int k;
};

struct h1 {

	int i;
	int j[1];
};

struct h1b {

	int i;
	int j[1];
	int k;
};

struct h3 {

	int i;
	int j[3];
};

struct h3b {

	int i;
	int j[3];
	int k;
};

void foo(int (*a)[3])
{
	(*a)[4] = 1;	/* { dg-warning "subscript 4 is above array bound" } */
	a[0][0] = 1;	// ok
	a[1][0] = 1;	// ok
	a[1][4] = 1;	/* { dg-warning "subscript 4 is above array bound" } */

	int c[3] = { 0 };

	c[4] = 1;	/* { dg-warning "subscript 4 is above array bound" } */

	e[4] = 1;	/* { dg-warning "subscript 4 is above array bound" } */

	struct f f;
	f.f[4] = 1;	/* { dg-warning "subscript 4 is above array bound" } */

	struct h* h = malloc(sizeof(struct h) + 3 * sizeof(int));
	struct h0* h0 = malloc(sizeof(struct h0) + 3 * sizeof(int));
	struct h1* h1 = malloc(sizeof(struct h1) + 3 * sizeof(int));
	struct h3* h3 = malloc(sizeof(struct h3));

	h->j[4] = 1;	// flexible array member
	h0->j[4] = 1;	// zero-sized array extension
	h1->j[4] = 1;	/* { dg-warning "subscript 4 is above array bound" } */
	h3->j[4] = 1;	/* { dg-warning "subscript 4 is above array bound" } */

	struct h0b* h0b = malloc(sizeof(struct h) + 3 * sizeof(int));
	struct h1b* h1b = malloc(sizeof(struct h1b) + 3 * sizeof(int));
	struct h3b* h3b = malloc(sizeof(struct h3b));
//	h0b->j[4] = 1;
	h1b->j[4] = 1;;	/* { dg-warning "subscript 4 is above array bound" } */
	h3b->j[4] = 1;;	/* { dg-warning "subscript 4 is above array bound" } */

	// make sure nothing gets optimized away
	bar(*a);
	bar(c);
	bar(e);
	bar(f.f);
	bar(h1->j);
	bar(h3->j);
	bar(h3b->j);
	bar(h1b->j);
	bar(h->j);
	bar(h0->j);
}