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