xref: /llvm-project/clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp (revision 237ca436adf48c72821afd9fa6e031ec1d4a0420)
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 
85   foo(ap1[1]);    // expected-note{{used in buffer access here}}
86 
87   auto ap2 = p;   // expected-warning{{'ap2' is an unsafe pointer used for buffer access}}
88 
89   foo(ap2[1]);    // expected-note{{used in buffer access here}}
90 
91   auto ap3 = pp;  // expected-warning{{'ap3' is an unsafe pointer used for buffer access}}
92 
93   foo(ap3[1][1]); // expected-note{{used in buffer access here}}
94                   // expected-warning@-1{{unsafe buffer access}}
95 
96   auto ap4 = *pp; // expected-warning{{'ap4' is an unsafe pointer used for buffer access}}
97 
98   foo(ap4[1]);    // expected-note{{used in buffer access here}}
99 }
100 
101 //TODO: do not warn for unevaluated context
102 void testUnevaluatedContext(int * p) {// expected-warning{{'p' is an unsafe pointer used for buffer access}}
103   foo(sizeof(p[1]),             // expected-note{{used in buffer access here}}
104       sizeof(decltype(p[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                           // 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); //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[]); // 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 
360   foo(ap1[1]);  // expected-note{{used in buffer access here}}
361 }
362 
363 #endif
364