]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/testsuite/gcc.dg/Warray-bounds-52.c
Correct handling of variable offset minus constant in -Warray-bounds [PR100137]
[thirdparty/gcc.git] / gcc / testsuite / gcc.dg / Warray-bounds-52.c
CommitLineData
ef29b12c
MS
1/* PR middle-end/92341 - missing -Warray-bounds indexing past the end
2 of a compound literal
3 { dg-do compile }
4 { dg-options "-O2 -Wall -ftrack-macro-expansion=0" } */
5
6#include "range.h"
7
8#define INT_MAX __INT_MAX__
9#define INT_MIN (-__INT_MAX__ - 1)
10
11void sink (int, ...);
12
13
14#define T(...) sink (__LINE__, (__VA_ARGS__))
15
16
17void direct_idx_cst (void)
18{
19 T ((int[]){ }[-1]); // { dg-warning "array subscript -1 is outside array bounds of 'int\\\[0]'" }
20 T ((int[]){ }[0]); // { dg-warning "array subscript 0 is outside array bounds of 'int\\\[0]'" }
21 T ((int[]){ }[1]); // { dg-warning "array subscript 1 is outside array bounds of 'int\\\[0]'" }
22
23 T ((int[]){ 1 }[-1]); // { dg-warning "array subscript -1 is below array bounds of 'int\\\[1]'" }
24 T ((int[]){ 1 }[0]);
25 T ((int[]){ 1 }[1]); // { dg-warning "array subscript 1 is above array bounds of 'int\\\[1]'" }
26 T ((int[]){ 1 }[INT_MIN]); // { dg-warning "array subscript -\[0-9\]+ is below array bounds of 'int\\\[1]'" }
27 T ((int[]){ 1 }[INT_MAX]); // { dg-warning "array subscript \[0-9\]+ is above array bounds of 'int\\\[1]'" }
28 T ((int[]){ 1 }[SIZE_MAX]); // { dg-warning "array subscript \[0-9\]+ is above array bounds of 'int\\\[1]'" }
29}
30
31
32void direct_idx_var (int i)
33{
34 T ((char[]){ }[i]); // { dg-warning "array subscript i is outside array bounds of 'char\\\[0]'" }
35 T ((int[]){ }[i]); // { dg-warning "array subscript i is outside array bounds of 'int\\\[0]'" }
36}
37
38
39void direct_idx_range (void)
40{
41 ptrdiff_t i = SR (-2, -1);
42
43 T ((int[]){ 1 }[i]); // { dg-warning "array subscript \[ \n\r]+ is outside array bounds of 'int\\\[0]'" "pr?????" { xfail *-*-* } }
44}
45
46
47#undef T
48#define T(idx, ...) do { \
49 int *p = (__VA_ARGS__); \
50 sink (p[idx]); \
51 } while (0)
52
53void ptr_idx_cst (void)
54{
55 T (-1, (int[]){ }); // { dg-warning "array subscript -1 is outside array bounds of 'int\\\[0]'" }
56 T ( 0, (int[]){ }); // { dg-warning "array subscript 0 is outside array bounds of 'int\\\[0]'" }
57 T (+1, (int[]){ }); // { dg-warning "array subscript 1 is outside array bounds of 'int\\\[0]'" }
58
59 T (-1, (int[]){ 1 }); // { dg-warning "array subscript -1 is outside array bounds of 'int\\\[1]'" }
60 T ( 0, (int[]){ 1 });
61 T (+1, (int[]){ 1 }); // { dg-warning "array subscript 1 is outside array bounds of 'int\\\[1]'" }
62 T (INT_MIN, (int[]){ 1 }); // { dg-warning "array subscript -\[0-9\]+ is outside array bounds of 'int\\\[1]'" "lp64" { xfail ilp32 } }
63 T (INT_MAX, (int[]){ 1 }); // { dg-warning "array subscript \[0-9\]+ is outside array bounds of 'int\\\[1]'" "lp64" { target lp64 } }
64 // { dg-warning "array subscript -1 is outside array bounds of 'int\\\[1]'" "ilp32" { target ilp32 } .-1 }
65 T (SIZE_MAX, (int[]){ 1 }); // { dg-warning "array subscript -?\[0-9\]+ is outside array bounds of 'int\\\[1]'" }
66}
67
68
69void ptr_idx_var (int i)
70{
71 T (i, (int[]){ }); // { dg-warning "array subscript \[^\n\r\]+ is outside array bounds of 'int\\\[0]'" }
72 T (i, (int[]){ 1 });
73 T (i, (int[]){ i, 1 });
74}
75
76void ptr_idx_range (void)
77{
78 ptrdiff_t i = SR (-2, -1);
79
80 T (i, (int[]){ }); // { dg-warning "array subscript \\\[-2, -1] is outside array bounds of 'int\\\[0]'" }
81 T (i, (int[]){ 1 }); // { dg-warning "array subscript \\\[-2, -1] is outside array bounds of 'int\\\[1]'" }
82 T (i, (int[]){ i }); // { dg-warning "array subscript \\\[-2, -1] is outside array bounds of 'int\\\[1]'" }
83
84 i = SR (0, 1);
85
a1108556 86 T (i, (int[]){ }); // { dg-warning "array subscript 0 is outside array bounds of 'int\\\[0]'" }
ef29b12c
MS
87 T (i, (int[]){ 1 });
88
89 i = SR (1, 2);
a1108556 90 T (i, (int[]){ 1 }); // { dg-warning "array subscript 1 is outside array bounds of 'int\\\[1]'" }
ef29b12c
MS
91
92 i = SR (2, 3);
93 T (i, (int[]){ 1, 2, 3 });
94
95 i = SR (3, 4);
a1108556 96 T (i, (int[]){ 2, 3, 4 }); // { dg-warning "array subscript 3 is outside array bounds of 'int\\\[3]'" }
ef29b12c 97}
b825a228
MS
98
99/* Some of the invalid accesses above also trigger -Wuninitialized.
100 { dg-prune-output "\\\[-Wuninitialized" } */