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