1 // RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage \ 2 // RUN: -fsafe-buffer-usage-suggestions \ 3 // RUN: -fblocks -include %s -verify %s 4 5 // RUN: %clang -x c++ -fsyntax-only -fblocks -include %s %s 2>&1 | FileCheck --allow-empty %s 6 // RUN: %clang_cc1 -std=c++11 -fblocks -include %s %s 2>&1 | FileCheck --allow-empty %s 7 // RUN: %clang_cc1 -std=c++20 -fblocks -include %s %s 2>&1 | FileCheck --allow-empty %s 8 // CHECK-NOT: [-Wunsafe-buffer-usage] 9 10 #ifndef INCLUDED 11 #define INCLUDED 12 #pragma clang system_header 13 14 // no spanification warnings for system headers 15 void foo(...); // let arguments of `foo` to hold testing expressions 16 void testAsSystemHeader(char *p) { 17 ++p; 18 19 auto ap1 = p; 20 auto ap2 = &p; 21 22 foo(p[1], 23 ap1[1], 24 ap2[2][3]); 25 } 26 27 #else 28 29 void testIncrement(char *p) { // expected-warning{{'p' is an unsafe pointer used for buffer access}} 30 ++p; // expected-note{{used in pointer arithmetic here}} 31 p++; // expected-note{{used in pointer arithmetic here}} 32 --p; // expected-note{{used in pointer arithmetic here}} 33 p--; // expected-note{{used in pointer arithmetic here}} 34 } 35 36 void * voidPtrCall(void); 37 char * charPtrCall(void); 38 39 void testArraySubscripts(int *p, int **pp) { // expected-note{{change type of 'pp' to 'std::span' to preserve bounds information}} 40 // expected-warning@-1{{'p' is an unsafe pointer used for buffer access}} 41 // expected-warning@-2{{'pp' is an unsafe pointer used for buffer access}} 42 foo(p[1], // expected-note{{used in buffer access here}} 43 pp[1][1], // expected-note{{used in buffer access here}} 44 // expected-warning@-1{{unsafe buffer access}} 45 1[1[pp]], // expected-note{{used in buffer access here}} 46 // expected-warning@-1{{unsafe buffer access}} 47 1[pp][1] // expected-note{{used in buffer access here}} 48 // expected-warning@-1{{unsafe buffer access}} 49 ); 50 51 if (p[3]) { // expected-note{{used in buffer access here}} 52 void * q = p; 53 54 foo(((int*)q)[10]); // expected-warning{{unsafe buffer access}} 55 } 56 57 foo(((int*)voidPtrCall())[3], // expected-warning{{unsafe buffer access}} 58 3[(int*)voidPtrCall()], // expected-warning{{unsafe buffer access}} 59 charPtrCall()[3], // expected-warning{{unsafe buffer access}} 60 3[charPtrCall()] // expected-warning{{unsafe buffer access}} 61 ); 62 63 int a[10]; // expected-warning{{'a' is an unsafe buffer that does not perform bounds checks}} 64 int b[10][10]; // expected-warning{{'b' is an unsafe buffer that does not perform bounds checks}} 65 66 foo(a[1], 1[a], // expected-note2{{used in buffer access here}} 67 b[3][4], // expected-warning{{unsafe buffer access}} 68 // expected-note@-1{{used in buffer access here}} 69 4[b][3], // expected-warning{{unsafe buffer access}} 70 // expected-note@-1{{used in buffer access here}} 71 4[3[b]]); // expected-warning{{unsafe buffer access}} 72 // expected-note@-1{{used in buffer access here}} 73 74 // Not to warn when index is zero 75 foo(p[0], pp[0][0], 0[0[pp]], 0[pp][0], 76 ((int*)voidPtrCall())[0], 77 0[(int*)voidPtrCall()], 78 charPtrCall()[0], 79 0[charPtrCall()] 80 ); 81 } 82 83 void testArraySubscriptsWithAuto() { 84 int a[10]; 85 // We do not fix a declaration if the type is `auto`. Because the actual type may change later. 86 auto ap1 = a; // expected-warning{{'ap1' is an unsafe pointer used for buffer access}} 87 foo(ap1[1]); // expected-note{{used in buffer access here}} 88 89 // In case the type is `auto *`, we know it must be a pointer. We can fix it. 90 auto * ap2 = a; // expected-warning{{'ap2' is an unsafe pointer used for buffer access}} \ 91 expected-note{{change type of 'ap2' to 'std::span' to preserve bounds information}} 92 foo(ap2[1]); // expected-note{{used in buffer access here}} 93 } 94 95 void testUnevaluatedContext(int * p) {// no-warning 96 foo(sizeof(p[1]), // no-warning 97 sizeof(decltype(p[1]))); // no-warning 98 } 99 100 // expected-note@+1{{change type of 'a' to 'std::span' to preserve bounds information}} 101 void testQualifiedParameters(const int * p, const int * const q, const int a[10], const int b[10][10]) { 102 // expected-warning@-1{{'p' is an unsafe pointer used for buffer access}} 103 // expected-warning@-2{{'q' is an unsafe pointer used for buffer access}} 104 // expected-warning@-3{{'a' is an unsafe pointer used for buffer access}} 105 // expected-warning@-4{{'b' is an unsafe pointer used for buffer access}} 106 107 foo(p[1], 1[p], p[-1], // expected-note3{{used in buffer access here}} 108 q[1], 1[q], q[-1], // expected-note3{{used in buffer access here}} 109 a[1], // expected-note{{used in buffer access here}} `a` is of pointer type 110 b[1][2] // expected-note{{used in buffer access here}} `b[1]` is of array type 111 // expected-warning@-1{{unsafe buffer access}} 112 ); 113 } 114 115 struct T { 116 int a[10]; 117 int * b; 118 struct { 119 int a[10]; 120 int * b; 121 } c; 122 }; 123 124 typedef struct T T_t; 125 126 T_t funRetT(); 127 T_t * funRetTStar(); 128 129 void testStructMembers(struct T * sp, struct T s, T_t * sp2, T_t s2) { 130 foo(sp->a[1], // expected-warning{{unsafe buffer access}} 131 sp->b[1], // expected-warning{{unsafe buffer access}} 132 sp->c.a[1], // expected-warning{{unsafe buffer access}} 133 sp->c.b[1], // expected-warning{{unsafe buffer access}} 134 s.a[1], // expected-warning{{unsafe buffer access}} 135 s.b[1], // expected-warning{{unsafe buffer access}} 136 s.c.a[1], // expected-warning{{unsafe buffer access}} 137 s.c.b[1], // expected-warning{{unsafe buffer access}} 138 sp2->a[1], // expected-warning{{unsafe buffer access}} 139 sp2->b[1], // expected-warning{{unsafe buffer access}} 140 sp2->c.a[1], // expected-warning{{unsafe buffer access}} 141 sp2->c.b[1], // expected-warning{{unsafe buffer access}} 142 s2.a[1], // expected-warning{{unsafe buffer access}} 143 s2.b[1], // expected-warning{{unsafe buffer access}} 144 s2.c.a[1], // expected-warning{{unsafe buffer access}} 145 s2.c.b[1], // expected-warning{{unsafe buffer access}} 146 funRetT().a[1], // expected-warning{{unsafe buffer access}} 147 funRetT().b[1], // expected-warning{{unsafe buffer access}} 148 funRetTStar()->a[1], // expected-warning{{unsafe buffer access}} 149 funRetTStar()->b[1] // expected-warning{{unsafe buffer access}} 150 ); 151 } 152 153 int garray[10]; // expected-warning{{'garray' is an unsafe buffer that does not perform bounds checks}} 154 int * gp = garray; // expected-warning{{'gp' is an unsafe pointer used for buffer access}} 155 int gvar = gp[1]; // FIXME: file scope unsafe buffer access is not warned 156 157 void testLambdaCaptureAndGlobal(int * p) { 158 // expected-warning@-1{{'p' is an unsafe pointer used for buffer access}} 159 int a[10]; // expected-warning{{'a' is an unsafe buffer that does not perform bounds checks}} 160 161 auto Lam = [p, a]() { 162 return p[1] // expected-note{{used in buffer access here}} 163 + a[1] + garray[1] // expected-note2{{used in buffer access here}} 164 + gp[1]; // expected-note{{used in buffer access here}} 165 166 }; 167 } 168 169 auto file_scope_lambda = [](int *ptr) { 170 // expected-warning@-1{{'ptr' is an unsafe pointer used for buffer access}} 171 172 ptr[5] = 10; // expected-note{{used in buffer access here}} 173 }; 174 175 void testLambdaCapture() { 176 int a[10]; // expected-warning{{'a' is an unsafe buffer that does not perform bounds checks}} 177 int b[10]; // expected-warning{{'b' is an unsafe buffer that does not perform bounds checks}} 178 int c[10]; 179 180 auto Lam1 = [a]() { 181 return a[1]; // expected-note{{used in buffer access here}} 182 }; 183 184 auto Lam2 = [x = b[3]]() { // expected-note{{used in buffer access here}} 185 return x; 186 }; 187 188 auto Lam = [x = c]() { // expected-warning{{'x' is an unsafe pointer used for buffer access}} 189 return x[3]; // expected-note{{used in buffer access here}} 190 }; 191 } 192 193 void testLambdaImplicitCapture() { 194 int a[10]; // expected-warning{{'a' is an unsafe buffer that does not perform bounds checks}} 195 int b[10]; // expected-warning{{'b' is an unsafe buffer that does not perform bounds checks}} 196 197 auto Lam1 = [=]() { 198 return a[1]; // expected-note{{used in buffer access here}} 199 }; 200 201 auto Lam2 = [&]() { 202 return b[1]; // expected-note{{used in buffer access here}} 203 }; 204 } 205 206 typedef T_t * T_ptr_t; 207 208 void testTypedefs(T_ptr_t p) { 209 // expected-warning@-1{{'p' is an unsafe pointer used for buffer access}} 210 foo(p[1], // expected-note{{used in buffer access here}} 211 p[1].a[1], // expected-note{{used in buffer access here}} 212 // expected-warning@-1{{unsafe buffer access}} 213 p[1].b[1] // expected-note{{used in buffer access here}} 214 // expected-warning@-1{{unsafe buffer access}} 215 ); 216 } 217 218 template<typename T, int N> T f(T t, T * pt, T a[N], T (&b)[N]) { 219 // expected-warning@-1{{'t' is an unsafe pointer used for buffer access}} 220 // expected-warning@-2{{'pt' is an unsafe pointer used for buffer access}} 221 // expected-warning@-3{{'a' is an unsafe pointer used for buffer access}} 222 // expected-warning@-4{{'b' is an unsafe buffer that does not perform bounds checks}} 223 foo(pt[1], // expected-note{{used in buffer access here}} 224 a[1], // expected-note{{used in buffer access here}} 225 b[1]); // expected-note{{used in buffer access here}} 226 return &t[1]; // expected-note{{used in buffer access here}} 227 } 228 229 // Testing pointer arithmetic for pointer-to-int, qualified multi-level 230 // pointer, pointer to a template type, and auto type 231 T_ptr_t getPtr(); 232 233 template<typename T> 234 void testPointerArithmetic(int * p, const int **q, T * x) { 235 // expected-warning@-1{{'p' is an unsafe pointer used for buffer access}} 236 // expected-warning@-2{{'x' is an unsafe pointer used for buffer access}} 237 int a[10]; 238 auto y = &a[0]; // expected-warning{{'y' is an unsafe pointer used for buffer access}} 239 240 foo(p + 1, 1 + p, p - 1, // expected-note3{{used in pointer arithmetic here}} 241 *q + 1, 1 + *q, *q - 1, // expected-warning3{{unsafe pointer arithmetic}} 242 x + 1, 1 + x, x - 1, // expected-note3{{used in pointer arithmetic here}} 243 y + 1, 1 + y, y - 1, // expected-note3{{used in pointer arithmetic here}} 244 getPtr() + 1, 1 + getPtr(), getPtr() - 1 // expected-warning3{{unsafe pointer arithmetic}} 245 ); 246 247 p += 1; p -= 1; // expected-note2{{used in pointer arithmetic here}} 248 *q += 1; *q -= 1; // expected-warning2{{unsafe pointer arithmetic}} 249 y += 1; y -= 1; // expected-note2{{used in pointer arithmetic here}} 250 x += 1; x -= 1; // expected-note2{{used in pointer arithmetic here}} 251 } 252 253 void testTemplate(int * p) { 254 int *a[10]; 255 foo(f(p, &p, a, a)[1]); // expected-warning{{unsafe buffer access}} 256 // FIXME: expected note@-1{{in instantiation of function template specialization 'f<int *, 10>' requested here}} 257 258 const int **q = const_cast<const int **>(&p); 259 260 testPointerArithmetic(p, q, p); //FIXME: expected note{{in instantiation of}} 261 } 262 263 void testPointerToMember() { 264 struct S_t { 265 int x; 266 int * y; 267 } S; 268 269 int S_t::* p = &S_t::x; 270 int * S_t::* q = &S_t::y; 271 272 foo(S.*p, 273 (S.*q)[1]); // expected-warning{{unsafe buffer access}} 274 } 275 276 // test that nested callable definitions are scanned only once 277 void testNestedCallableDefinition(int * p) { 278 class A { 279 void inner(int * p) { 280 // expected-warning@-1{{'p' is an unsafe pointer used for buffer access}} 281 p++; // expected-note{{used in pointer arithmetic here}} 282 } 283 284 static void innerStatic(int * p) { 285 // expected-warning@-1{{'p' is an unsafe pointer used for buffer access}} 286 p++; // expected-note{{used in pointer arithmetic here}} 287 } 288 289 void innerInner(int * p) { 290 auto Lam = [p]() { 291 int * q = p; // expected-warning{{'q' is an unsafe pointer used for buffer access}} 292 q++; // expected-note{{used in pointer arithmetic here}} 293 return *q; 294 }; 295 } 296 }; 297 298 auto Lam = [p]() { 299 int * q = p; // expected-warning{{'q' is an unsafe pointer used for buffer access}} 300 q++; // expected-note{{used in pointer arithmetic here}} 301 return *q; 302 }; 303 304 auto LamLam = [p]() { 305 auto Lam = [p]() { 306 int * q = p; // expected-warning{{'q' is an unsafe pointer used for buffer access}} 307 q++; // expected-note{{used in pointer arithmetic here}} 308 return *q; 309 }; 310 }; 311 312 void (^Blk)(int*) = ^(int *p) { 313 // expected-warning@-1{{'p' is an unsafe pointer used for buffer access}} 314 p++; // expected-note{{used in pointer arithmetic here}} 315 }; 316 317 void (^BlkBlk)(int*) = ^(int *p) { 318 void (^Blk)(int*) = ^(int *p) { 319 // expected-warning@-1{{'p' is an unsafe pointer used for buffer access}} 320 p++; // expected-note{{used in pointer arithmetic here}} 321 }; 322 Blk(p); 323 }; 324 325 // lambda and block as call arguments... 326 foo( [p]() { int * q = p; // expected-warning{{'q' is an unsafe pointer used for buffer access}} 327 q++; // expected-note{{used in pointer arithmetic here}} 328 return *q; 329 }, 330 ^(int *p) { // expected-warning{{'p' is an unsafe pointer used for buffer access}} 331 p++; // expected-note{{used in pointer arithmetic here}} 332 } 333 ); 334 } 335 336 int testVariableDecls(int * p) { 337 // expected-warning@-1{{'p' is an unsafe pointer used for buffer access}} 338 int * q = p++; // expected-note{{used in pointer arithmetic here}} 339 int a[p[1]]; // expected-note{{used in buffer access here}} 340 int b = p[1]; // expected-note{{used in buffer access here}} 341 return p[1]; // expected-note{{used in buffer access here}} 342 } 343 344 template<typename T> void fArr(T t[]) { 345 // expected-warning@-1{{'t' is an unsafe pointer used for buffer access}} 346 foo(t[1]); // expected-note{{used in buffer access here}} 347 T ar[8]; // expected-warning{{'ar' is an unsafe buffer that does not perform bounds checks}} 348 foo(ar[5]); // expected-note{{used in buffer access here}} 349 } 350 351 template void fArr<int>(int t[]); // FIXME: expected note {{in instantiation of}} 352 353 int testReturn(int t[]) {// expected-note{{change type of 't' to 'std::span' to preserve bounds information}} 354 // expected-warning@-1{{'t' is an unsafe pointer used for buffer access}} 355 return t[1]; // expected-note{{used in buffer access here}} 356 } 357 358 int testArrayAccesses(int n) { 359 // auto deduced array type 360 int cArr[2][3] = {{1, 2, 3}, {4, 5, 6}}; 361 // expected-warning@-1{{'cArr' is an unsafe buffer that does not perform bounds checks}} 362 int d = cArr[0][0]; 363 foo(cArr[0][0]); 364 foo(cArr[1][2]); // expected-note{{used in buffer access here}} 365 // expected-warning@-1{{unsafe buffer access}} 366 auto cPtr = cArr[1][2]; // expected-note{{used in buffer access here}} 367 // expected-warning@-1{{unsafe buffer access}} 368 foo(cPtr); 369 370 // Typdefs 371 typedef int A[3]; 372 const A tArr = {4, 5, 6}; 373 // expected-warning@-1{{'tArr' is an unsafe buffer that does not perform bounds checks}} 374 foo(tArr[0], tArr[1]); // expected-note{{used in buffer access here}} 375 return cArr[0][1]; // expected-warning{{unsafe buffer access}} 376 } 377 378 void testArrayPtrArithmetic(int x[]) { // expected-warning{{'x' is an unsafe pointer used for buffer access}} 379 foo (x + 3); // expected-note{{used in pointer arithmetic here}} 380 381 int y[3] = {0, 1, 2}; // expected-warning{{'y' is an unsafe buffer that does not perform bounds checks}} 382 foo(y + 4); // expected-note{{used in pointer arithmetic here}} 383 } 384 385 void testMultiLineDeclStmt(int * p) { 386 int 387 388 * 389 390 ap1 = p; // expected-warning{{'ap1' is an unsafe pointer used for buffer access}} \ 391 expected-note{{change type of 'ap1' to 'std::span' to preserve bounds information}} 392 393 foo(ap1[1]); // expected-note{{used in buffer access here}} 394 } 395 396 #endif 397