xref: /llvm-project/clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp (revision b7bdf1996fd195b20b9a2916c9183167650806f1)
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     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(int *p, int **pp) {
84   int a[10];
85   auto ap1 = a;   // expected-warning{{'ap1' is an unsafe pointer used for buffer access}} \
86 		     expected-note{{change type of 'ap1' to 'std::span' to preserve bounds information}}
87 
88   foo(ap1[1]);    // expected-note{{used in buffer access here}}
89 
90   auto ap2 = p;   // 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 
93   foo(ap2[1]);    // expected-note{{used in buffer access here}}
94 
95   auto ap3 = pp;  // expected-warning{{'ap3' is an unsafe pointer used for buffer access}} \
96 		     expected-note{{change type of 'ap3' to 'std::span' to preserve bounds information}}
97 
98   foo(ap3[1][1]); // expected-note{{used in buffer access here}}
99                   // expected-warning@-1{{unsafe buffer access}}
100 
101   auto ap4 = *pp; // expected-warning{{'ap4' is an unsafe pointer used for buffer access}} \
102   		     expected-note{{change type of 'ap4' to 'std::span' to preserve bounds information}}
103 
104   foo(ap4[1]);    // expected-note{{used in buffer access here}}
105 }
106 
107 void testQualifiedParameters(const int * p, const int * const q, const int a[10], const int b[10][10]) {
108   // expected-warning@-1{{'p' is an unsafe pointer used for buffer access}}
109   // expected-warning@-2{{'q' is an unsafe pointer used for buffer access}}
110   // expected-warning@-3{{'a' is an unsafe pointer used for buffer access}}
111   // expected-warning@-4{{'b' is an unsafe pointer used for buffer access}}
112 
113   foo(p[1], 1[p], p[-1],    // expected-note3{{used in buffer access here}}
114       q[1], 1[q], q[-1],    // expected-note3{{used in buffer access here}}
115       a[1],                 // expected-note{{used in buffer access here}}     `a` is of pointer type
116       b[1][2]               // expected-note{{used in buffer access here}}     `b[1]` is of array type
117                             // expected-warning@-1{{unsafe buffer access}}
118       );
119 }
120 
121 struct T {
122   int a[10];
123   int * b;
124   struct {
125     int a[10];
126     int * b;
127   } c;
128 };
129 
130 typedef struct T T_t;
131 
132 T_t funRetT();
133 T_t * funRetTStar();
134 
135 void testStructMembers(struct T * sp, struct T s, T_t * sp2, T_t s2) {
136   foo(sp->a[1],     // expected-warning{{unsafe buffer access}}
137       sp->b[1],     // expected-warning{{unsafe buffer access}}
138       sp->c.a[1],   // expected-warning{{unsafe buffer access}}
139       sp->c.b[1],   // expected-warning{{unsafe buffer access}}
140       s.a[1],       // expected-warning{{unsafe buffer access}}
141       s.b[1],       // expected-warning{{unsafe buffer access}}
142       s.c.a[1],     // expected-warning{{unsafe buffer access}}
143       s.c.b[1],     // expected-warning{{unsafe buffer access}}
144       sp2->a[1],    // expected-warning{{unsafe buffer access}}
145       sp2->b[1],    // expected-warning{{unsafe buffer access}}
146       sp2->c.a[1],  // expected-warning{{unsafe buffer access}}
147       sp2->c.b[1],  // expected-warning{{unsafe buffer access}}
148       s2.a[1],      // expected-warning{{unsafe buffer access}}
149       s2.b[1],      // expected-warning{{unsafe buffer access}}
150       s2.c.a[1],           // expected-warning{{unsafe buffer access}}
151       s2.c.b[1],           // expected-warning{{unsafe buffer access}}
152       funRetT().a[1],      // expected-warning{{unsafe buffer access}}
153       funRetT().b[1],      // expected-warning{{unsafe buffer access}}
154       funRetTStar()->a[1], // expected-warning{{unsafe buffer access}}
155       funRetTStar()->b[1]  // expected-warning{{unsafe buffer access}}
156       );
157 }
158 
159 int garray[10];     // expected-warning{{'garray' is an unsafe buffer that does not perform bounds checks}}
160 int * gp = garray;  // expected-warning{{'gp' is an unsafe pointer used for buffer access}}
161 int gvar = gp[1];   // FIXME: file scope unsafe buffer access is not warned
162 
163 // FIXME: Add test for lambda capture with initializer. E. g. auto Lam = [new_p = p]() {...
164 void testLambdaCaptureAndGlobal(int * p) {
165   // expected-warning@-1{{'p' is an unsafe pointer used for buffer access}}
166   int a[10];              // expected-warning{{'a' is an unsafe buffer that does not perform bounds checks}}
167 
168   auto Lam = [p, a]() {
169     return p[1]           // expected-note{{used in buffer access here}}
170       + a[1] + garray[1]  // expected-note2{{used in buffer access here}}
171       + gp[1];            // expected-note{{used in buffer access here}}
172   };
173 }
174 
175 typedef T_t * T_ptr_t;
176 
177 void testTypedefs(T_ptr_t p) {
178   // expected-warning@-1{{'p' is an unsafe pointer used for buffer access}}
179   foo(p[1],       // expected-note{{used in buffer access here}}
180       p[1].a[1],  // expected-note{{used in buffer access here}}
181                   // expected-warning@-1{{unsafe buffer access}}
182       p[1].b[1]   // expected-note{{used in buffer access here}}
183                   // expected-warning@-1{{unsafe buffer access}}
184       );
185 }
186 
187 template<typename T, int N> T f(T t, T * pt, T a[N], T (&b)[N]) {
188   // expected-warning@-1{{'t' is an unsafe pointer used for buffer access}}
189   // expected-warning@-2{{'pt' is an unsafe pointer used for buffer access}}
190   // expected-warning@-3{{'a' is an unsafe pointer used for buffer access}}
191   // expected-warning@-4{{'b' is an unsafe buffer that does not perform bounds checks}}
192   foo(pt[1],    // expected-note{{used in buffer access here}}
193       a[1],     // expected-note{{used in buffer access here}}
194       b[1]);    // expected-note{{used in buffer access here}}
195   return &t[1]; // expected-note{{used in buffer access here}}
196 }
197 
198 // Testing pointer arithmetic for pointer-to-int, qualified multi-level
199 // pointer, pointer to a template type, and auto type
200 T_ptr_t getPtr();
201 
202 template<typename T>
203 void testPointerArithmetic(int * p, const int **q, T * x) {
204 // expected-warning@-1{{'p' is an unsafe pointer used for buffer access}}
205 // expected-warning@-2{{'x' is an unsafe pointer used for buffer access}}
206   int a[10];
207   auto y = &a[0]; // expected-warning{{'y' is an unsafe pointer used for buffer access}}
208 
209   foo(p + 1, 1 + p, p - 1,      // expected-note3{{used in pointer arithmetic here}}
210       *q + 1, 1 + *q, *q - 1,   // expected-warning3{{unsafe pointer arithmetic}}
211       x + 1, 1 + x, x - 1,      // expected-note3{{used in pointer arithmetic here}}
212       y + 1, 1 + y, y - 1,      // expected-note3{{used in pointer arithmetic here}}
213       getPtr() + 1, 1 + getPtr(), getPtr() - 1 // expected-warning3{{unsafe pointer arithmetic}}
214       );
215 
216   p += 1;  p -= 1;  // expected-note2{{used in pointer arithmetic here}}
217   *q += 1; *q -= 1; // expected-warning2{{unsafe pointer arithmetic}}
218   y += 1; y -= 1;   // expected-note2{{used in pointer arithmetic here}}
219   x += 1; x -= 1;   // expected-note2{{used in pointer arithmetic here}}
220 }
221 
222 void testTemplate(int * p) {
223   int *a[10];
224   foo(f(p, &p, a, a)[1]); // expected-warning{{unsafe buffer access}}
225                           // FIXME: expected note@-1{{in instantiation of function template specialization 'f<int *, 10>' requested here}}
226 
227   const int **q = const_cast<const int **>(&p);
228 
229   testPointerArithmetic(p, q, p); //FIXME: expected note{{in instantiation of}}
230 }
231 
232 void testPointerToMember() {
233   struct S_t {
234     int x;
235     int * y;
236   } S;
237 
238   int S_t::* p = &S_t::x;
239   int * S_t::* q = &S_t::y;
240 
241   foo(S.*p,
242       (S.*q)[1]);  // expected-warning{{unsafe buffer access}}
243 }
244 
245 // test that nested callable definitions are scanned only once
246 void testNestedCallableDefinition(int * p) {
247   class A {
248     void inner(int * p) {
249       // expected-warning@-1{{'p' is an unsafe pointer used for buffer access}}
250       p++; // expected-note{{used in pointer arithmetic here}}
251     }
252 
253     static void innerStatic(int * p) {
254       // expected-warning@-1{{'p' is an unsafe pointer used for buffer access}}
255       p++; // expected-note{{used in pointer arithmetic here}}
256     }
257 
258     void innerInner(int * p) {
259       auto Lam = [p]() {
260         int * q = p;    // expected-warning{{'q' is an unsafe pointer used for buffer access}}
261         q++;            // expected-note{{used in pointer arithmetic here}}
262         return *q;
263       };
264     }
265   };
266 
267   auto Lam = [p]() {
268     int * q = p;  // expected-warning{{'q' is an unsafe pointer used for buffer access}}
269     q++;          // expected-note{{used in pointer arithmetic here}}
270     return *q;
271   };
272 
273   auto LamLam = [p]() {
274     auto Lam = [p]() {
275       int * q = p;  // expected-warning{{'q' is an unsafe pointer used for buffer access}}
276       q++;          // expected-note{{used in pointer arithmetic here}}
277       return *q;
278     };
279   };
280 
281   void (^Blk)(int*) = ^(int *p) {
282     // expected-warning@-1{{'p' is an unsafe pointer used for buffer access}}
283     p++;   // expected-note{{used in pointer arithmetic here}}
284   };
285 
286   void (^BlkBlk)(int*) = ^(int *p) {
287     void (^Blk)(int*) = ^(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     Blk(p);
292   };
293 
294   // lambda and block as call arguments...
295   foo( [p]() { int * q = p; // expected-warning{{'q' is an unsafe pointer used for buffer access}}
296               q++;          // expected-note{{used in pointer arithmetic here}}
297               return *q;
298        },
299        ^(int *p) {  // expected-warning{{'p' is an unsafe pointer used for buffer access}}
300         p++;        // expected-note{{used in pointer arithmetic here}}
301        }
302      );
303 }
304 
305 int testVariableDecls(int * p) {
306   // expected-warning@-1{{'p' is an unsafe pointer used for buffer access}}
307   int * q = p++;      // expected-note{{used in pointer arithmetic here}}
308   int a[p[1]];        // expected-note{{used in buffer access here}}
309   int b = p[1];       // expected-note{{used in buffer access here}}
310   return p[1];        // expected-note{{used in buffer access here}}
311 }
312 
313 template<typename T> void fArr(T t[]) {
314   // expected-warning@-1{{'t' is an unsafe pointer used for buffer access}}
315   foo(t[1]);    // expected-note{{used in buffer access here}}
316   T ar[8];      // expected-warning{{'ar' is an unsafe buffer that does not perform bounds checks}}
317   foo(ar[5]);   // expected-note{{used in buffer access here}}
318 }
319 
320 template void fArr<int>(int t[]); // FIXME: expected note {{in instantiation of}}
321 
322 int testReturn(int t[]) {
323   // expected-warning@-1{{'t' is an unsafe pointer used for buffer access}}
324   return t[1]; // expected-note{{used in buffer access here}}
325 }
326 
327 int testArrayAccesses(int n) {
328     // auto deduced array type
329     int cArr[2][3] = {{1, 2, 3}, {4, 5, 6}};
330     // expected-warning@-1{{'cArr' is an unsafe buffer that does not perform bounds checks}}
331     int d = cArr[0][0];
332     foo(cArr[0][0]);
333     foo(cArr[1][2]);        // expected-note{{used in buffer access here}}
334                             // expected-warning@-1{{unsafe buffer access}}
335     auto cPtr = cArr[1][2]; // expected-note{{used in buffer access here}}
336                             // expected-warning@-1{{unsafe buffer access}}
337     foo(cPtr);
338 
339     // Typdefs
340     typedef int A[3];
341     const A tArr = {4, 5, 6};
342     // expected-warning@-1{{'tArr' is an unsafe buffer that does not perform bounds checks}}
343     foo(tArr[0], tArr[1]);  // expected-note{{used in buffer access here}}
344     return cArr[0][1];      // expected-warning{{unsafe buffer access}}
345 }
346 
347 void testArrayPtrArithmetic(int x[]) { // expected-warning{{'x' is an unsafe pointer used for buffer access}}
348   foo (x + 3); // expected-note{{used in pointer arithmetic here}}
349 
350   int y[3] = {0, 1, 2}; // expected-warning{{'y' is an unsafe buffer that does not perform bounds checks}}
351   foo(y + 4); // expected-note{{used in pointer arithmetic here}}
352 }
353 
354 void testMultiLineDeclStmt(int * p) {
355   auto
356 
357 
358   ap1 = p;      // expected-warning{{'ap1' is an unsafe pointer used for buffer access}} \
359          	   expected-note{{change type of 'ap1' to 'std::span' to preserve bounds information}}
360 
361   foo(ap1[1]);  // expected-note{{used in buffer access here}}
362 }
363 
364 #endif
365