xref: /llvm-project/clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp (revision 644ac2a018c9bf83c9ba256074e552ad7f1fe941)
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) {
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                         // expected-note@-1{{change type of 'a' to 'std::array' to label it for hardening}}
65     int b[10][10];      // expected-warning{{'b' is an unsafe buffer that does not perform bounds checks}}
66 
67   foo(a[1], 1[a],   // expected-note2{{used in buffer access here}}
68       b[3][4],      // expected-warning{{unsafe buffer access}}
69                     // expected-note@-1{{used in buffer access here}}
70       4[b][3],      // expected-warning{{unsafe buffer access}}
71                     // expected-note@-1{{used in buffer access here}}
72       4[3[b]]);     // expected-warning{{unsafe buffer access}}
73                     // expected-note@-1{{used in buffer access here}}
74 
75   // Not to warn when index is zero
76   foo(p[0], pp[0][0], 0[0[pp]], 0[pp][0],
77       ((int*)voidPtrCall())[0],
78       0[(int*)voidPtrCall()],
79       charPtrCall()[0],
80       0[charPtrCall()]
81       );
82 }
83 
84 void testArraySubscriptsWithAuto() {
85   int a[10];
86   // We do not fix a declaration if the type is `auto`. Because the actual type may change later.
87   auto ap1 = a;   // expected-warning{{'ap1' is an unsafe pointer used for buffer access}}
88   foo(ap1[1]);    // expected-note{{used in buffer access here}}
89 
90   // In case the type is `auto *`, we know it must be a pointer. We can fix it.
91   auto * ap2 = a; // expected-warning{{'ap2' is an unsafe pointer used for buffer access}} \
92                      expected-note{{change type of 'ap2' to 'std::span' to preserve bounds information}}
93   foo(ap2[1]);    // expected-note{{used in buffer access here}}
94 }
95 
96 void testUnevaluatedContext(int * p) {// no-warning
97   foo(sizeof(p[1]),             // no-warning
98       sizeof(decltype(p[1])));  // no-warning
99 }
100 
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                           // expected-note@-1{{change type of 'b' to 'std::array' to label it for hardening}}
179   int c[10];
180 
181   auto Lam1 = [a]() {
182     return a[1];           // expected-note{{used in buffer access here}}
183   };
184 
185   auto Lam2 = [x = b[3]]() { // expected-note{{used in buffer access here}}
186     return x;
187   };
188 
189   auto Lam = [x = c]() { // expected-warning{{'x' is an unsafe pointer used for buffer access}}
190     return x[3]; // expected-note{{used in buffer access here}}
191   };
192 }
193 
194 void testLambdaImplicitCapture() {
195   int a[10];              // expected-warning{{'a' is an unsafe buffer that does not perform bounds checks}}
196                           // expected-note@-1{{change type of 'a' to 'std::array' to label it for hardening}}
197   int b[10];              // expected-warning{{'b' is an unsafe buffer that does not perform bounds checks}}
198                           // expected-note@-1{{change type of 'b' to 'std::array' to label it for hardening}}
199 
200   auto Lam1 = [=]() {
201     return a[1];           // expected-note{{used in buffer access here}}
202   };
203 
204   auto Lam2 = [&]() {
205     return b[1];           // expected-note{{used in buffer access here}}
206   };
207 }
208 
209 typedef T_t * T_ptr_t;
210 
211 void testTypedefs(T_ptr_t p) {
212   // expected-warning@-1{{'p' is an unsafe pointer used for buffer access}}
213   foo(p[1],       // expected-note{{used in buffer access here}}
214       p[1].a[1],  // expected-note{{used in buffer access here}}
215                   // expected-warning@-1{{unsafe buffer access}}
216       p[1].b[1]   // expected-note{{used in buffer access here}}
217                   // expected-warning@-1{{unsafe buffer access}}
218       );
219 }
220 
221 template<typename T, int N> T f(T t, T * pt, T a[N], T (&b)[N]) {
222   // expected-warning@-1{{'t' is an unsafe pointer used for buffer access}}
223   // expected-warning@-2{{'pt' is an unsafe pointer used for buffer access}}
224   // expected-warning@-3{{'a' is an unsafe pointer used for buffer access}}
225   // expected-warning@-4{{'b' is an unsafe buffer that does not perform bounds checks}}
226   foo(pt[1],    // expected-note{{used in buffer access here}}
227       a[1],     // expected-note{{used in buffer access here}}
228       b[1]);    // expected-note{{used in buffer access here}}
229   return &t[1]; // expected-note{{used in buffer access here}}
230 }
231 
232 // Testing pointer arithmetic for pointer-to-int, qualified multi-level
233 // pointer, pointer to a template type, and auto type
234 T_ptr_t getPtr();
235 
236 template<typename T>
237 void testPointerArithmetic(int * p, const int **q, T * x) {
238 // expected-warning@-1{{'p' is an unsafe pointer used for buffer access}}
239 // expected-warning@-2{{'x' is an unsafe pointer used for buffer access}}
240   int a[10];
241   auto y = &a[0]; // expected-warning{{'y' is an unsafe pointer used for buffer access}}
242 
243   foo(p + 1, 1 + p, p - 1,      // expected-note3{{used in pointer arithmetic here}}
244       *q + 1, 1 + *q, *q - 1,   // expected-warning3{{unsafe pointer arithmetic}}
245       x + 1, 1 + x, x - 1,      // expected-note3{{used in pointer arithmetic here}}
246       y + 1, 1 + y, y - 1,      // expected-note3{{used in pointer arithmetic here}}
247       getPtr() + 1, 1 + getPtr(), getPtr() - 1 // expected-warning3{{unsafe pointer arithmetic}}
248       );
249 
250   p += 1;  p -= 1;  // expected-note2{{used in pointer arithmetic here}}
251   *q += 1; *q -= 1; // expected-warning2{{unsafe pointer arithmetic}}
252   y += 1; y -= 1;   // expected-note2{{used in pointer arithmetic here}}
253   x += 1; x -= 1;   // expected-note2{{used in pointer arithmetic here}}
254 }
255 
256 void testTemplate(int * p) {
257   int *a[10];
258   foo(f(p, &p, a, a)[1]); // expected-warning{{unsafe buffer access}}
259                           // FIXME: expected note@-1{{in instantiation of function template specialization 'f<int *, 10>' requested here}}
260 
261   const int **q = const_cast<const int **>(&p);
262 
263   testPointerArithmetic(p, q, p); //FIXME: expected note{{in instantiation of}}
264 }
265 
266 void testPointerToMember() {
267   struct S_t {
268     int x;
269     int * y;
270   } S;
271 
272   int S_t::* p = &S_t::x;
273   int * S_t::* q = &S_t::y;
274 
275   foo(S.*p,
276       (S.*q)[1]);  // expected-warning{{unsafe buffer access}}
277 }
278 
279 // test that nested callable definitions are scanned only once
280 void testNestedCallableDefinition(int * p) {
281   class A {
282     void inner(int * p) {
283       // expected-warning@-1{{'p' is an unsafe pointer used for buffer access}}
284       p++; // expected-note{{used in pointer arithmetic here}}
285     }
286 
287     static void innerStatic(int * p) {
288       // expected-warning@-1{{'p' is an unsafe pointer used for buffer access}}
289       p++; // expected-note{{used in pointer arithmetic here}}
290     }
291 
292     void innerInner(int * p) {
293       auto Lam = [p]() {
294         int * q = p;    // expected-warning{{'q' is an unsafe pointer used for buffer access}}
295         q++;            // expected-note{{used in pointer arithmetic here}}
296         return *q;
297       };
298     }
299   };
300 
301   auto Lam = [p]() {
302     int * q = p;  // expected-warning{{'q' is an unsafe pointer used for buffer access}}
303     q++;          // expected-note{{used in pointer arithmetic here}}
304     return *q;
305   };
306 
307   auto LamLam = [p]() {
308     auto Lam = [p]() {
309       int * q = p;  // expected-warning{{'q' is an unsafe pointer used for buffer access}}
310       q++;          // expected-note{{used in pointer arithmetic here}}
311       return *q;
312     };
313   };
314 
315   void (^Blk)(int*) = ^(int *p) {
316     // expected-warning@-1{{'p' is an unsafe pointer used for buffer access}}
317     p++;   // expected-note{{used in pointer arithmetic here}}
318   };
319 
320   void (^BlkBlk)(int*) = ^(int *p) {
321     void (^Blk)(int*) = ^(int *p) {
322       // expected-warning@-1{{'p' is an unsafe pointer used for buffer access}}
323       p++;   // expected-note{{used in pointer arithmetic here}}
324     };
325     Blk(p);
326   };
327 
328   // lambda and block as call arguments...
329   foo( [p]() { int * q = p; // expected-warning{{'q' is an unsafe pointer used for buffer access}}
330               q++;          // expected-note{{used in pointer arithmetic here}}
331               return *q;
332        },
333        ^(int *p) {  // expected-warning{{'p' is an unsafe pointer used for buffer access}}
334         p++;        // expected-note{{used in pointer arithmetic here}}
335        }
336      );
337 }
338 
339 int testVariableDecls(int * p) {
340   // expected-warning@-1{{'p' is an unsafe pointer used for buffer access}}
341   int * q = p++;      // expected-note{{used in pointer arithmetic here}}
342   int a[p[1]];        // expected-note{{used in buffer access here}}
343   int b = p[1];       // expected-note{{used in buffer access here}}
344   return p[1];        // expected-note{{used in buffer access here}}
345 }
346 
347 template<typename T> void fArr(T t[]) {
348   // expected-warning@-1{{'t' is an unsafe pointer used for buffer access}}
349   foo(t[1]);    // expected-note{{used in buffer access here}}
350   T ar[8];      // expected-warning{{'ar' is an unsafe buffer that does not perform bounds checks}}
351                 // expected-note@-1{{change type of 'ar' to 'std::array' to label it for hardening}}
352   foo(ar[5]);   // expected-note{{used in buffer access here}}
353 }
354 
355 template void fArr<int>(int t[]); // FIXME: expected note {{in instantiation of}}
356 
357 int testReturn(int t[]) {// expected-note{{change type of 't' to 'std::span' to preserve bounds information}}
358   // expected-warning@-1{{'t' is an unsafe pointer used for buffer access}}
359   return t[1]; // expected-note{{used in buffer access here}}
360 }
361 
362 int testArrayAccesses(int n) {
363     // auto deduced array type
364     int cArr[2][3] = {{1, 2, 3}, {4, 5, 6}};
365     // expected-warning@-1{{'cArr' is an unsafe buffer that does not perform bounds checks}}
366     int d = cArr[0][0];
367     foo(cArr[0][0]);
368     foo(cArr[1][2]);        // expected-note{{used in buffer access here}}
369                             // expected-warning@-1{{unsafe buffer access}}
370     auto cPtr = cArr[1][2]; // expected-note{{used in buffer access here}}
371                             // expected-warning@-1{{unsafe buffer access}}
372     foo(cPtr);
373 
374     // Typdefs
375     typedef int A[3];
376     const A tArr = {4, 5, 6};
377     // expected-warning@-1{{'tArr' is an unsafe buffer that does not perform bounds checks}}
378     foo(tArr[0], tArr[1]);  // expected-note{{used in buffer access here}}
379     return cArr[0][1];      // expected-warning{{unsafe buffer access}}
380 }
381 
382 void testArrayPtrArithmetic(int x[]) { // expected-warning{{'x' is an unsafe pointer used for buffer access}}
383   foo (x + 3); // expected-note{{used in pointer arithmetic here}}
384 
385   int y[3] = {0, 1, 2}; // expected-warning{{'y' is an unsafe buffer that does not perform bounds checks}}
386   foo(y + 4); // expected-note{{used in pointer arithmetic here}}
387 }
388 
389 void testMultiLineDeclStmt(int * p) {
390   int
391 
392   *
393 
394   ap1 = p;      // expected-warning{{'ap1' is an unsafe pointer used for buffer access}} \
395          	   expected-note{{change type of 'ap1' to 'std::span' to preserve bounds information}}
396 
397   foo(ap1[1]);  // expected-note{{used in buffer access here}}
398 }
399 
400 #endif
401