xref: /aosp_15_r20/external/clang/test/SemaCXX/array-bounds.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -verify %s
2*67e74705SXin Li 
foo()3*67e74705SXin Li int foo() {
4*67e74705SXin Li   int x[2]; // expected-note 4 {{array 'x' declared here}}
5*67e74705SXin Li   int y[2]; // expected-note 2 {{array 'y' declared here}}
6*67e74705SXin Li   int z[1]; // expected-note {{array 'z' declared here}}
7*67e74705SXin Li   int w[1][1]; // expected-note {{array 'w' declared here}}
8*67e74705SXin Li   int v[1][1][1]; // expected-note {{array 'v' declared here}}
9*67e74705SXin Li   int *p = &y[2]; // no-warning
10*67e74705SXin Li   (void) sizeof(x[2]); // no-warning
11*67e74705SXin Li   y[2] = 2; // expected-warning {{array index 2 is past the end of the array (which contains 2 elements)}}
12*67e74705SXin Li   z[1] = 'x'; // expected-warning {{array index 1 is past the end of the array (which contains 1 element)}}
13*67e74705SXin Li   w[0][2] = 0; // expected-warning {{array index 2 is past the end of the array (which contains 1 element)}}
14*67e74705SXin Li   v[0][0][2] = 0; // expected-warning {{array index 2 is past the end of the array (which contains 1 element)}}
15*67e74705SXin Li   return x[2] +  // expected-warning {{array index 2 is past the end of the array (which contains 2 elements)}}
16*67e74705SXin Li          y[-1] + // expected-warning {{array index -1 is before the beginning of the array}}
17*67e74705SXin Li          x[sizeof(x)] +  // expected-warning {{array index 8 is past the end of the array (which contains 2 elements)}}
18*67e74705SXin Li          x[sizeof(x) / sizeof(x[0])] +  // expected-warning {{array index 2 is past the end of the array (which contains 2 elements)}}
19*67e74705SXin Li          x[sizeof(x) / sizeof(x[0]) - 1] + // no-warning
20*67e74705SXin Li          x[sizeof(x[2])]; // expected-warning {{array index 4 is past the end of the array (which contains 2 elements)}}
21*67e74705SXin Li }
22*67e74705SXin Li 
23*67e74705SXin Li // This code example tests that -Warray-bounds works with arrays that
24*67e74705SXin Li // are template parameters.
25*67e74705SXin Li template <char *sz> class Qux {
test()26*67e74705SXin Li   bool test() { return sz[0] == 'a'; }
27*67e74705SXin Li };
28*67e74705SXin Li 
f1(int a[1])29*67e74705SXin Li void f1(int a[1]) {
30*67e74705SXin Li   int val = a[3]; // no warning for function argumnet
31*67e74705SXin Li }
32*67e74705SXin Li 
f2(const int (& a)[2])33*67e74705SXin Li void f2(const int (&a)[2]) { // expected-note {{declared here}}
34*67e74705SXin Li   int val = a[3];  // expected-warning {{array index 3 is past the end of the array (which contains 2 elements)}}
35*67e74705SXin Li }
36*67e74705SXin Li 
test()37*67e74705SXin Li void test() {
38*67e74705SXin Li   struct {
39*67e74705SXin Li     int a[0];
40*67e74705SXin Li   } s2;
41*67e74705SXin Li   s2.a[3] = 0; // no warning for 0-sized array
42*67e74705SXin Li 
43*67e74705SXin Li   union {
44*67e74705SXin Li     short a[2]; // expected-note 4 {{declared here}}
45*67e74705SXin Li     char c[4];
46*67e74705SXin Li   } u;
47*67e74705SXin Li   u.a[3] = 1; // expected-warning {{array index 3 is past the end of the array (which contains 2 elements)}}
48*67e74705SXin Li   u.c[3] = 1; // no warning
49*67e74705SXin Li   short *p = &u.a[2]; // no warning
50*67e74705SXin Li   p = &u.a[3]; // expected-warning {{array index 3 is past the end of the array (which contains 2 elements)}}
51*67e74705SXin Li   *(&u.a[2]) = 1; // expected-warning {{array index 2 is past the end of the array (which contains 2 elements)}}
52*67e74705SXin Li   *(&u.a[3]) = 1; // expected-warning {{array index 3 is past the end of the array (which contains 2 elements)}}
53*67e74705SXin Li   *(&u.c[3]) = 1; // no warning
54*67e74705SXin Li 
55*67e74705SXin Li   const int const_subscript = 3;
56*67e74705SXin Li   int array[2]; // expected-note {{declared here}}
57*67e74705SXin Li   array[const_subscript] = 0;  // expected-warning {{array index 3 is past the end of the array (which contains 2 elements)}}
58*67e74705SXin Li 
59*67e74705SXin Li   int *ptr;
60*67e74705SXin Li   ptr[3] = 0; // no warning for pointer references
61*67e74705SXin Li   int array2[] = { 0, 1, 2 }; // expected-note 2 {{declared here}}
62*67e74705SXin Li 
63*67e74705SXin Li   array2[3] = 0; // expected-warning {{array index 3 is past the end of the array (which contains 3 elements)}}
64*67e74705SXin Li   array2[2+2] = 0; // expected-warning {{array index 4 is past the end of the array (which contains 3 elements)}}
65*67e74705SXin Li 
66*67e74705SXin Li   const char *str1 = "foo";
67*67e74705SXin Li   char c1 = str1[5]; // no warning for pointers
68*67e74705SXin Li 
69*67e74705SXin Li   const char str2[] = "foo"; // expected-note {{declared here}}
70*67e74705SXin Li   char c2 = str2[5]; // expected-warning {{array index 5 is past the end of the array (which contains 4 elements)}}
71*67e74705SXin Li 
72*67e74705SXin Li   int (*array_ptr)[2];
73*67e74705SXin Li   (*array_ptr)[3] = 1; // expected-warning {{array index 3 is past the end of the array (which contains 2 elements)}}
74*67e74705SXin Li }
75*67e74705SXin Li 
76*67e74705SXin Li template <int I> struct S {
77*67e74705SXin Li   char arr[I]; // expected-note 3 {{declared here}}
78*67e74705SXin Li };
f()79*67e74705SXin Li template <int I> void f() {
80*67e74705SXin Li   S<3> s;
81*67e74705SXin Li   s.arr[4] = 0; // expected-warning 2 {{array index 4 is past the end of the array (which contains 3 elements)}}
82*67e74705SXin Li   s.arr[I] = 0; // expected-warning {{array index 5 is past the end of the array (which contains 3 elements)}}
83*67e74705SXin Li }
84*67e74705SXin Li 
test_templates()85*67e74705SXin Li void test_templates() {
86*67e74705SXin Li   f<5>(); // expected-note {{in instantiation}}
87*67e74705SXin Li }
88*67e74705SXin Li 
89*67e74705SXin Li #define SIZE 10
90*67e74705SXin Li #define ARR_IN_MACRO(flag, arr, idx) flag ? arr[idx] : 1
91*67e74705SXin Li 
test_no_warn_macro_unreachable()92*67e74705SXin Li int test_no_warn_macro_unreachable() {
93*67e74705SXin Li   int arr[SIZE]; // expected-note {{array 'arr' declared here}}
94*67e74705SXin Li   return ARR_IN_MACRO(0, arr, SIZE) + // no-warning
95*67e74705SXin Li          ARR_IN_MACRO(1, arr, SIZE); // expected-warning{{array index 10 is past the end of the array (which contains 10 elements)}}
96*67e74705SXin Li }
97*67e74705SXin Li 
98*67e74705SXin Li // This exhibited an assertion failure for a 32-bit build of Clang.
test_pr9240()99*67e74705SXin Li int test_pr9240() {
100*67e74705SXin Li   short array[100]; // expected-note {{array 'array' declared here}}
101*67e74705SXin Li   return array[(unsigned long long) 100]; // expected-warning {{array index 100 is past the end of the array (which contains 100 elements)}}
102*67e74705SXin Li }
103*67e74705SXin Li 
104*67e74705SXin Li // PR 9284 - a template parameter can cause an array bounds access to be
105*67e74705SXin Li // infeasible.
106*67e74705SXin Li template <bool extendArray>
pr9284()107*67e74705SXin Li void pr9284() {
108*67e74705SXin Li     int arr[3 + (extendArray ? 1 : 0)];
109*67e74705SXin Li 
110*67e74705SXin Li     if (extendArray)
111*67e74705SXin Li         arr[3] = 42; // no-warning
112*67e74705SXin Li }
113*67e74705SXin Li 
114*67e74705SXin Li template <bool extendArray>
pr9284b()115*67e74705SXin Li void pr9284b() {
116*67e74705SXin Li     int arr[3 + (extendArray ? 1 : 0)]; // expected-note {{array 'arr' declared here}}
117*67e74705SXin Li 
118*67e74705SXin Li     if (!extendArray)
119*67e74705SXin Li         arr[3] = 42; // expected-warning{{array index 3 is past the end of the array (which contains 3 elements)}}
120*67e74705SXin Li }
121*67e74705SXin Li 
test_pr9284()122*67e74705SXin Li void test_pr9284() {
123*67e74705SXin Li     pr9284<true>();
124*67e74705SXin Li     pr9284<false>();
125*67e74705SXin Li     pr9284b<true>();
126*67e74705SXin Li     pr9284b<false>(); // expected-note{{in instantiation of function template specialization 'pr9284b<false>' requested here}}
127*67e74705SXin Li }
128*67e74705SXin Li 
test_pr9296()129*67e74705SXin Li int test_pr9296() {
130*67e74705SXin Li     int array[2];
131*67e74705SXin Li     return array[true]; // no-warning
132*67e74705SXin Li }
133*67e74705SXin Li 
test_sizeof_as_condition(int flag)134*67e74705SXin Li int test_sizeof_as_condition(int flag) {
135*67e74705SXin Li   int arr[2] = { 0, 0 }; // expected-note {{array 'arr' declared here}}
136*67e74705SXin Li   if (flag)
137*67e74705SXin Li     return sizeof(char) != sizeof(char) ? arr[2] : arr[1];
138*67e74705SXin Li   return sizeof(char) == sizeof(char) ? arr[2] : arr[1]; // expected-warning {{array index 2 is past the end of the array (which contains 2 elements)}}
139*67e74705SXin Li }
140*67e74705SXin Li 
test_switch()141*67e74705SXin Li void test_switch() {
142*67e74705SXin Li   switch (4) {
143*67e74705SXin Li     case 1: {
144*67e74705SXin Li       int arr[2];
145*67e74705SXin Li       arr[2] = 1; // no-warning
146*67e74705SXin Li       break;
147*67e74705SXin Li     }
148*67e74705SXin Li     case 4: {
149*67e74705SXin Li       int arr[2]; // expected-note {{array 'arr' declared here}}
150*67e74705SXin Li       arr[2] = 1; // expected-warning {{array index 2 is past the end of the array (which contains 2 elements)}}
151*67e74705SXin Li       break;
152*67e74705SXin Li     }
153*67e74705SXin Li     default: {
154*67e74705SXin Li       int arr[2];
155*67e74705SXin Li       arr[2] = 1; // no-warning
156*67e74705SXin Li       break;
157*67e74705SXin Li     }
158*67e74705SXin Li   }
159*67e74705SXin Li }
160*67e74705SXin Li 
161*67e74705SXin Li // Test nested switch statements.
162*67e74705SXin Li enum enumA { enumA_A, enumA_B, enumA_C, enumA_D, enumA_E };
163*67e74705SXin Li enum enumB { enumB_X, enumB_Y, enumB_Z };
164*67e74705SXin Li static enum enumB myVal = enumB_X;
test_nested_switch()165*67e74705SXin Li void test_nested_switch() {
166*67e74705SXin Li   switch (enumA_E) { // expected-warning {{no case matching constant}}
167*67e74705SXin Li     switch (myVal) { // expected-warning {{enumeration values 'enumB_X' and 'enumB_Z' not handled in switch}}
168*67e74705SXin Li       case enumB_Y: ;
169*67e74705SXin Li     }
170*67e74705SXin Li   }
171*67e74705SXin Li }
172*67e74705SXin Li 
173*67e74705SXin Li // Test that if all the values of an enum covered, that the 'default' branch
174*67e74705SXin Li // is unreachable.
175*67e74705SXin Li enum Values { A, B, C, D };
test_all_enums_covered(enum Values v)176*67e74705SXin Li void test_all_enums_covered(enum Values v) {
177*67e74705SXin Li   int x[2];
178*67e74705SXin Li   switch (v) {
179*67e74705SXin Li   case A: return;
180*67e74705SXin Li   case B: return;
181*67e74705SXin Li   case C: return;
182*67e74705SXin Li   case D: return;
183*67e74705SXin Li   }
184*67e74705SXin Li   x[2] = 0; // no-warning
185*67e74705SXin Li }
186*67e74705SXin Li 
187*67e74705SXin Li namespace tailpad {
188*67e74705SXin Li   struct foo {
189*67e74705SXin Li     char c1[1]; // expected-note {{declared here}}
190*67e74705SXin Li     int x;
191*67e74705SXin Li     char c2[1];
192*67e74705SXin Li   };
193*67e74705SXin Li 
194*67e74705SXin Li   class baz {
195*67e74705SXin Li    public:
196*67e74705SXin Li     char c1[1]; // expected-note {{declared here}}
197*67e74705SXin Li     int x;
198*67e74705SXin Li     char c2[1];
199*67e74705SXin Li   };
200*67e74705SXin Li 
bar(struct foo * F,baz * B)201*67e74705SXin Li   char bar(struct foo *F, baz *B) {
202*67e74705SXin Li     return F->c1[3] + // expected-warning {{array index 3 is past the end of the array (which contains 1 element)}}
203*67e74705SXin Li            F->c2[3] + // no warning, foo could have tail padding allocated.
204*67e74705SXin Li            B->c1[3] + // expected-warning {{array index 3 is past the end of the array (which contains 1 element)}}
205*67e74705SXin Li            B->c2[3]; // no warning, baz could have tail padding allocated.
206*67e74705SXin Li   }
207*67e74705SXin Li }
208*67e74705SXin Li 
209*67e74705SXin Li namespace metaprogramming {
210*67e74705SXin Li #define ONE 1
211*67e74705SXin Li   struct foo { char c[ONE]; }; // expected-note {{declared here}}
212*67e74705SXin Li   template <int N> struct bar { char c[N]; }; // expected-note {{declared here}}
213*67e74705SXin Li 
test(foo * F,bar<1> * B)214*67e74705SXin Li   char test(foo *F, bar<1> *B) {
215*67e74705SXin Li     return F->c[3] + // expected-warning {{array index 3 is past the end of the array (which contains 1 element)}}
216*67e74705SXin Li            B->c[3]; // expected-warning {{array index 3 is past the end of the array (which contains 1 element)}}
217*67e74705SXin Li   }
218*67e74705SXin Li }
219*67e74705SXin Li 
bar(int x)220*67e74705SXin Li void bar(int x) {}
test_more()221*67e74705SXin Li int test_more() {
222*67e74705SXin Li   int foo[5]; // expected-note 5 {{array 'foo' declared here}}
223*67e74705SXin Li   bar(foo[5]); // expected-warning {{array index 5 is past the end of the array (which contains 5 elements)}}
224*67e74705SXin Li   ++foo[5]; // expected-warning {{array index 5 is past the end of the array (which contains 5 elements)}}
225*67e74705SXin Li   if (foo[6]) // expected-warning {{array index 6 is past the end of the array (which contains 5 elements)}}
226*67e74705SXin Li     return --foo[6]; // expected-warning {{array index 6 is past the end of the array (which contains 5 elements)}}
227*67e74705SXin Li   else
228*67e74705SXin Li     return foo[5]; // expected-warning {{array index 5 is past the end of the array (which contains 5 elements)}}
229*67e74705SXin Li }
230*67e74705SXin Li 
test_pr10771()231*67e74705SXin Li void test_pr10771() {
232*67e74705SXin Li     double foo[4096];  // expected-note {{array 'foo' declared here}}
233*67e74705SXin Li 
234*67e74705SXin Li     ((char*)foo)[sizeof(foo) - 1] = '\0';  // no-warning
235*67e74705SXin Li     *(((char*)foo) + sizeof(foo) - 1) = '\0';  // no-warning
236*67e74705SXin Li 
237*67e74705SXin Li     ((char*)foo)[sizeof(foo)] = '\0';  // expected-warning {{array index 32768 is past the end of the array (which contains 32768 elements)}}
238*67e74705SXin Li 
239*67e74705SXin Li     // TODO: This should probably warn, too.
240*67e74705SXin Li     *(((char*)foo) + sizeof(foo)) = '\0';  // no-warning
241*67e74705SXin Li }
242*67e74705SXin Li 
243*67e74705SXin Li int test_pr11007_aux(const char * restrict, ...);
244*67e74705SXin Li 
245*67e74705SXin Li // Test checking with varargs.
test_pr11007()246*67e74705SXin Li void test_pr11007() {
247*67e74705SXin Li   double a[5]; // expected-note {{array 'a' declared here}}
248*67e74705SXin Li   test_pr11007_aux("foo", a[1000]); // expected-warning {{array index 1000 is past the end of the array}}
249*67e74705SXin Li }
250*67e74705SXin Li 
test_rdar10916006(void)251*67e74705SXin Li void test_rdar10916006(void)
252*67e74705SXin Li {
253*67e74705SXin Li 	int a[128]; // expected-note {{array 'a' declared here}}
254*67e74705SXin Li 	a[(unsigned char)'\xA1'] = 1; // expected-warning {{array index 161 is past the end of the array}}
255*67e74705SXin Li }
256