[C++ PATCH] Deprecate -ffriend-injection
[official-gcc.git] / gcc / testsuite / gcc.dg / attr-alloc_size-3.c
blob3be888672020b7fa5ab170e0f0807b7c99025af8
1 /* PR c/77531 - __attribute__((alloc_size(1,2))) could also warn on
2 multiplication overflow
3 PR c/78284 - warn on malloc with very large arguments
4 Test exercising the ability to detect and diagnose calls to allocation
5 functions decorated with attribute alloc_size that either overflow or
6 exceed the default maximum object size (with -Walloc-size-larger-than
7 not explicitly specified). */
8 /* { dg-do compile { target size32plus } } */
9 /* { dg-options "-O2 -Wall" } */
11 #define SCHAR_MAX __SCHAR_MAX__
12 #define SCHAR_MIN (-SCHAR_MAX - 1)
13 #define UCHAR_MAX (SCHAR_MAX * 2 + 1)
15 #define SHRT_MAX __SHRT_MAX__
16 #define SHRT_MIN (-SHRT_MAX - 1)
17 #define USHRT_MAX (SHRT_MAX * 2 + 1)
19 #define INT_MAX __INT_MAX__
20 #define INT_MIN (-INT_MAX - 1)
21 #define UINT_MAX (INT_MAX * 2U + 1)
23 #define LONG_MAX __LONG_MAX__
24 #define LONG_MIN (-LONG_MAX - 1L)
25 #define ULONG_MAX (LONG_MAX * 2LU + 1)
27 #define LLONG_MAX __LLONG_MAX__
28 #define LLONG_MIN (-LLONG_MAX - 1LL)
29 #define ULLONG_MAX (ULLONG_MAX * 2LLU + 1)
31 #define PTRDIFF_MAX __PTRDIFF_MAX__
32 #define PTRDIFF_MIN (-PTRDIFF_MAX - 1)
33 #define SIZE_MAX __SIZE_MAX__
35 typedef __PTRDIFF_TYPE__ ptrdiff_t;
36 typedef __SIZE_TYPE__ size_t;
38 #define ALLOC_SIZE(...) __attribute__ ((alloc_size (__VA_ARGS__)))
40 void* f_uchar_1 (unsigned char) ALLOC_SIZE (1);
41 void* f_uchar_2 (unsigned char, unsigned char) ALLOC_SIZE (1, 2);
42 void* f_schar_1 (signed char) ALLOC_SIZE (1);
43 void* f_schar_2 (signed char, signed char) ALLOC_SIZE (1, 2);
45 void* f_ushrt_1 (unsigned short) ALLOC_SIZE (1);
46 void* f_ushrt_2 (unsigned short, unsigned short) ALLOC_SIZE (1, 2);
47 void* f_shrt_1 (signed short) ALLOC_SIZE (1);
48 void* f_shrt_2 (signed short, signed short) ALLOC_SIZE (1, 2);
50 void* f_uint_1 (unsigned) ALLOC_SIZE (1);
51 void* f_uint_2 (unsigned, unsigned) ALLOC_SIZE (1, 2);
52 void* f_int_1 (int) ALLOC_SIZE (1);
53 void* f_int_2 (int, int) ALLOC_SIZE (1, 2);
55 void* f_ulong_1 (unsigned long) ALLOC_SIZE (1);
56 void* f_ulong_2 (unsigned long, unsigned long) ALLOC_SIZE (1, 2);
57 void* f_long_1 (long) ALLOC_SIZE (1);
58 void* f_long_2 (long, long) ALLOC_SIZE (1, 2);
60 void* f_ullong_1 (unsigned long long) ALLOC_SIZE (1);
61 void* f_ullong_2 (unsigned long long, unsigned long long) ALLOC_SIZE (1, 2);
62 void* f_llong_1 (long long) ALLOC_SIZE (1);
63 void* f_llong_2 (long long, long long) ALLOC_SIZE (1, 2);
65 void* f_size_1 (size_t) ALLOC_SIZE (1);
66 void* f_size_2 (size_t, size_t) ALLOC_SIZE (1, 2);
68 size_t
69 unsigned_range (size_t min, size_t max)
71 extern size_t random_unsigned_value (void);
72 size_t val = random_unsigned_value ();
73 if (val < min || max < val) val = min;
74 return val;
77 long long
78 signed_range (long long min, long long max)
80 extern long long random_signed_value (void);
81 long long val = random_signed_value ();
82 if (val < min || max < val) val = min;
83 return val;
86 size_t
87 unsigned_anti_range (size_t min, size_t max)
89 extern size_t random_unsigned_value (void);
90 size_t val = random_unsigned_value ();
91 if (min <= val && val <= max)
92 val = min - 1;
93 return val;
96 long long
97 signed_anti_range (long long min, long long max)
99 extern long long random_signed_value (void);
100 long long val = random_signed_value ();
101 if (min <= val && val <= max)
102 val = min - 1;
103 return val;
106 #define UR(min, max) unsigned_range (min, max)
107 #define SR(min, max) signed_range (min, max)
109 #define UAR(min, max) unsigned_anti_range (min, max)
110 #define SAR(min, max) signed_anti_range (min, max)
113 void sink (void*);
115 void
116 test_uchar_cst (void)
118 const unsigned char max = UCHAR_MAX;
120 sink (f_uchar_1 (0));
121 sink (f_uchar_1 (1));
122 sink (f_uchar_1 (max));
124 sink (f_uchar_2 (0, 0));
125 sink (f_uchar_2 (0, 1));
126 sink (f_uchar_2 (1, 0));
127 sink (f_uchar_2 (1, 1));
128 sink (f_uchar_2 (0, max));
129 sink (f_uchar_2 (max, 0));
130 sink (f_uchar_2 (max, max));
133 void
134 test_uchar_range (unsigned char n, int i)
136 const unsigned char max = UCHAR_MAX;
138 sink (f_uchar_1 (n));
140 sink (f_uchar_1 (UR (0, 1)));
141 sink (f_uchar_1 (UR (1, max)));
142 sink (f_uchar_1 (UR (0, max - 1)));
144 sink (f_uchar_1 (UAR (1, 1)));
145 sink (f_uchar_1 (UAR (1, max - 1)));
146 sink (f_uchar_1 (UAR (max - 2, max - 1)));
148 sink (f_uchar_2 (0, n));
149 sink (f_uchar_2 (0, i));
150 sink (f_uchar_2 (n, 0));
151 sink (f_uchar_2 (i, 0));
152 sink (f_uchar_2 (1, n));
153 sink (f_uchar_2 (1, i));
154 sink (f_uchar_2 (n, 1));
155 sink (f_uchar_2 (i, 1));
156 sink (f_uchar_2 (max, n));
157 sink (f_uchar_2 (max, i));
158 sink (f_uchar_2 (n, max));
159 sink (f_uchar_2 (i, max));
160 sink (f_uchar_2 (n, n));
161 sink (f_uchar_2 (i, i));
163 sink (f_uchar_2 (UR (0, 1), UR (0, 1)));
164 sink (f_uchar_2 (UR (1, 2), UR (1, 2)));
165 sink (f_uchar_2 (UR (1, max), UR (0, 1)));
166 sink (f_uchar_2 (UR (0, 1), UR (1, max)));
169 void
170 test_schar_cst (void)
172 const signed char min = SCHAR_MIN;
173 const signed char max = SCHAR_MAX;
175 sink (f_schar_1 (min)); /* { dg-warning "argument 1 value .-\[0-9\]+. is negative" } */
176 sink (f_schar_1 (-1)); /* { dg-warning "argument 1 value .-1. is negative" } */
177 sink (f_schar_1 (0));
178 sink (f_schar_1 (1));
179 sink (f_schar_1 (max));
181 sink (f_schar_2 (0, min)); /* { dg-warning "argument 2 value .-\[0-9\]+. is negative" } */
182 sink (f_schar_2 (min, 0)); /* { dg-warning "argument 1 value .-\[0-9\]+. is negative" } */
183 sink (f_schar_2 (0, -1)); /* { dg-warning "argument 2 value .-1. is negative" } */
184 sink (f_schar_2 (-1, 0)); /* { dg-warning "argument 1 value .-1. is negative" } */
188 void
189 test_schar_range (signed char n)
191 const signed char min = SCHAR_MIN;
192 const signed char max = SCHAR_MAX;
194 sink (f_schar_1 (n));
196 sink (f_schar_1 (SR (min, min + 1))); /* { dg-warning "argument 1 range \\\[-\[0-9\]+, -\[0-9\]+\\\] is negative" } */
197 sink (f_schar_1 (SR (min, 0))); /* { dg-warning "argument 1 range \\\[-\[0-9\]+, 0\\\] is negative" } */
198 sink (f_schar_1 (SR (-1, 0))); /* { dg-warning "argument 1 range \\\[-1, 0\\\] is negative" } */
199 sink (f_schar_1 (SR (-1, 1)));
200 sink (f_schar_1 (SR (0, 1)));
201 sink (f_schar_1 (SR (0, max - 1)));
202 sink (f_schar_1 (SR (1, max)));
203 sink (f_schar_1 (SR (max - 1, max)));
205 sink (f_schar_2 (n, n));
207 sink (f_schar_2 (SR (min, min + 1), n)); /* { dg-warning "argument 1 range \\\[-\[0-9\]+, -\[0-9\]+\\\] is negative" } */
208 sink (f_schar_2 (n, SR (min, min + 1))); /* { dg-warning "argument 2 range \\\[-\[0-9\]+, -\[0-9\]+\\\] is negative" } */
209 sink (f_schar_2 (SR (min, min + 1), 0)); /* { dg-warning "argument 1 range \\\[-\[0-9\]+, -\[0-9\]+\\\] is negative" } */
210 sink (f_schar_2 (0, SR (min, min + 1))); /* { dg-warning "argument 2 range \\\[-\[0-9\]+, -\[0-9\]+\\\] is negative" } */
211 sink (f_schar_2 (SR (min, min + 1), min)); /* { dg-warning "argument 1 range \\\[-\[0-9\]+, -\[0-9\]+\\\] is negative" } */
212 /* { dg-warning "argument 2 value .-\[0-9\]+. is negative" "argument 2" { target *-*-* } .-1 } */
213 sink (f_schar_2 (min, SR (min, min + 1))); /* { dg-warning "argument 2 range \\\[-\[0-9\]+, -\[0-9\]+\\\] is negative" } */
214 /* { dg-warning "argument 1 value .-\[0-9\]+. is negative" "argument 1" { target *-*-* } .-1 } */
216 sink (f_schar_2 (SR (-1, 0), 0)); /* { dg-warning "argument 1 range \\\[-\[0-9\]+, 0\\\] is negative" } */
217 sink (f_schar_2 (0, SR (-1, 0))); /* { dg-warning "argument 2 range \\\[-\[0-9\]+, 0\\\] is negative" } */
218 sink (f_schar_2 (SR (-1, 0), 1)); /* { dg-warning "argument 1 range \\\[-\[0-9\]+, 0\\\] is negative" } */
219 sink (f_schar_2 (1, SR (-1, 0))); /* { dg-warning "argument 2 range \\\[-\[0-9\]+, 0\\\] is negative" } */
220 sink (f_schar_2 (SR (-1, 0), n)); /* { dg-warning "argument 1 range \\\[-\[0-9\]+, 0\\\] is negative" } */
221 sink (f_schar_2 (n, SR (-1, 0))); /* { dg-warning "argument 2 range \\\[-\[0-9\]+, 0\\\] is negative" } */
223 sink (f_schar_2 (max, SR (1, max)));
224 sink (f_schar_2 (SR (1, max), max));
227 void
228 test_ushrt_cst (void)
230 const unsigned short max = USHRT_MAX;
232 sink (f_ushrt_1 (0));
233 sink (f_ushrt_1 (1));
234 sink (f_ushrt_1 (max));
236 sink (f_ushrt_2 (0, 0));
237 sink (f_ushrt_2 (0, 1));
238 sink (f_ushrt_2 (1, 0));
239 sink (f_ushrt_2 (1, 1));
240 sink (f_ushrt_2 (0, max));
241 sink (f_ushrt_2 (max, 0));
243 if (max < SIZE_MAX && (size_t)max * max < SIZE_MAX / 2)
244 sink (f_ushrt_2 (max, max));
247 void
248 test_ushrt_range (unsigned short n)
250 const unsigned short max = USHRT_MAX;
252 sink (f_ushrt_1 (n));
253 sink (f_ushrt_1 (UR (0, 1)));
254 sink (f_ushrt_1 (UR (1, max - 1)));
255 sink (f_ushrt_1 (UR (1, max)));
256 sink (f_ushrt_1 (UR (0, max - 1)));
259 void
260 test_shrt_cst (void)
262 const short min = SHRT_MIN;
263 const short max = SHRT_MAX;
265 sink (f_shrt_1 (min)); /* { dg-warning "argument 1 value .-\[0-9\]+. is negative" } */
266 sink (f_shrt_1 (-1)); /* { dg-warning "argument 1 value .-1. is negative" } */
267 sink (f_shrt_1 (0));
268 sink (f_shrt_1 (1));
269 sink (f_shrt_1 (max));
272 void
273 test_shrt_range (short n)
275 const short min = SHRT_MIN;
276 const short max = SHRT_MAX;
278 sink (f_shrt_1 (n));
280 sink (f_shrt_1 (SR (min, min + 1))); /* { dg-warning "argument 1 range \\\[-\[0-9\]+, -\[0-9\]+\\\] is negative" } */
281 sink (f_shrt_1 (SR (min, 0))); /* { dg-warning "argument 1 range \\\[-\[0-9\]+, 0\\\] is negative" } */
282 sink (f_shrt_1 (SR (-1, 0))); /* { dg-warning "argument 1 range \\\[-1, 0\\\] is negative" } */
283 sink (f_shrt_1 (SR (-1, 1)));
284 sink (f_shrt_1 (SR (0, 1)));
285 sink (f_shrt_1 (SR (0, max - 1)));
286 sink (f_shrt_1 (SR (1, max)));
287 sink (f_shrt_1 (SR (max - 1, max)));
290 void
291 test_uint_cst (void)
293 const unsigned max = UINT_MAX;
295 sink (f_uint_1 (0));
296 sink (f_uint_1 (1));
298 if (max < SIZE_MAX)
300 sink (f_uint_1 (max - 1));
301 sink (f_uint_1 (max));
305 void
306 test_uint_range (unsigned n)
308 const unsigned max = UINT_MAX;
310 sink (f_uint_1 (n));
311 sink (f_uint_1 (UR (0, 1)));
312 sink (f_uint_1 (UR (0, max - 1)));
313 sink (f_uint_1 (UR (1, max - 1)));
314 sink (f_uint_1 (UR (1, max)));
317 void
318 test_int_cst (void)
320 const int min = INT_MIN;
321 const int max = INT_MAX;
323 sink (f_int_1 (min)); /* { dg-warning "argument 1 value .-\[0-9\]+. is negative" } */
324 sink (f_int_1 (-1)); /* { dg-warning "argument 1 value .-1. is negative" } */
325 sink (f_int_1 (0));
326 sink (f_int_1 (1));
327 sink (f_int_1 (max));
330 void
331 test_int_range (int n)
333 const int min = INT_MIN;
334 const int max = INT_MAX;
336 sink (f_int_1 (n));
338 sink (f_int_1 (SR (min, min + 1))); /* { dg-warning "argument 1 range \\\[-\[0-9\]+, -\[0-9\]+\\\] is negative" } */
339 sink (f_int_1 (SR (min, 0))); /* { dg-warning "argument 1 range \\\[-\[0-9\]+, 0\\\] is negative" } */
340 sink (f_int_1 (SR (-1, 0))); /* { dg-warning "argument 1 range \\\[-1, 0\\\] is negative" } */
341 sink (f_int_1 (SR (-1, 1)));
342 sink (f_int_1 (SR (0, 1)));
343 sink (f_int_1 (SR (0, max - 1)));
344 sink (f_int_1 (SR (1, max)));
345 sink (f_int_1 (SR (max - 1, max)));
348 void
349 test_ulong_cst (void)
351 const unsigned long max = ULONG_MAX;
353 sink (f_ulong_1 (0));
354 sink (f_ulong_1 (1));
355 #if ULONG_MAX < SIZE_MAX
356 sink (f_ulong_1 (max - 1));
357 sink (f_ulong_1 (max));
358 #else
359 (void)&max;
360 #endif
363 void
364 test_ulong_range (unsigned long n)
366 const unsigned long max = ULONG_MAX;
368 sink (f_ulong_1 (n));
369 sink (f_ulong_1 (UR (0, 1)));
370 sink (f_ulong_1 (UR (0, max - 1)));
371 sink (f_ulong_1 (UR (1, max - 1)));
372 sink (f_ulong_1 (UR (1, max)));
375 void
376 test_long_cst (void)
378 const long min = LONG_MIN;
379 const long max = LONG_MAX;
381 sink (f_long_1 (min)); /* { dg-warning "argument 1 value .-\[0-9\]+l*. is negative" } */
382 sink (f_long_1 (-1)); /* { dg-warning "argument 1 value .-1l*. is negative" } */
383 sink (f_long_1 (0));
384 sink (f_long_1 (1));
385 sink (f_long_1 (max));
388 void
389 test_long_range (long n)
391 const long min = LONG_MIN;
392 const long max = LONG_MAX;
394 sink (f_long_1 (n));
396 sink (f_long_1 (SR (min, min + 1))); /* { dg-warning "argument 1 range \\\[-\[0-9\]+l*, -\[0-9\]+l*\\\] is negative" } */
397 sink (f_long_1 (SR (min, 0))); /* { dg-warning "argument 1 range \\\[-\[0-9\]+l*, 0l*\\\] is negative" } */
398 sink (f_long_1 (SR (-1, 0))); /* { dg-warning "argument 1 range \\\[-1l*, 0l*\\\] is negative" } */
399 sink (f_long_1 (SR (-1, 1)));
400 sink (f_long_1 (SR (0, 1)));
401 sink (f_long_1 (SR (0, max - 1)));
402 sink (f_long_1 (SR (1, max)));
403 sink (f_long_1 (SR (max - 1, max)));
406 void
407 test_size_cst (void)
409 const size_t max = __SIZE_MAX__;
411 sink (f_size_1 (0));
412 sink (f_size_1 (1));
413 sink (f_size_1 (max - 1)); /* { dg-warning "argument 1 value .\[0-9\]+. exceeds maximum object size \[0-9\]+" } */
414 sink (f_size_1 (max)); /* { dg-warning "argument 1 value .\[0-9\]+. exceeds maximum object size \[0-9\]+" } */
416 sink (f_size_2 (0, max - 1)); /* { dg-warning "argument 2 value .\[0-9\]+. exceeds maximum object size \[0-9\]+" } */
417 sink (f_size_2 (max - 1, 0)); /* { dg-warning "argument 1 value .\[0-9\]+. exceeds maximum object size \[0-9\]+" } */
418 sink (f_size_2 (1, max - 1)); /* { dg-warning "argument 2 value .\[0-9\]+. exceeds maximum object size \[0-9\]+" } */
419 sink (f_size_2 (max - 1, 1)); /* { dg-warning "argument 1 value .\[0-9\]+. exceeds maximum object size \[0-9\]+" } */
420 sink (f_size_2 (max - 1, max - 1)); /* { dg-warning "argument 1 value .\[0-9\]+. exceeds maximum object size \[0-9\]+" } */
421 /* { dg-warning "argument 2 value .\[0-9\]+. exceeds maximum object size \[0-9\]+" "argument 2" { target *-*-* } .-1 } */
423 sink (f_size_2 (0, max)); /* { dg-warning "argument 2 value .\[0-9\]+. exceeds maximum object size \[0-9\]+" } */
424 sink (f_size_2 (max, 0)); /* { dg-warning "argument 1 value .\[0-9\]+. exceeds maximum object size \[0-9\]+" } */
426 sink (f_size_2 (max / 2, 2)); /* { dg-warning "product .\[0-9\]+ \\* \[0-9\]+. of arguments 1 and 2 exceeds maximum object size \[0-9\]+" } */
427 sink (f_size_2 (max / 2, 3)); /* { dg-warning "product .\[0-9\]+ \\* \[0-9\]+. of arguments 1 and 2 exceeds .SIZE_MAX." } */
430 void
431 test_size_range (size_t ui, ptrdiff_t si)
433 const ptrdiff_t smin = PTRDIFF_MIN;
434 const ptrdiff_t smax = PTRDIFF_MAX;
435 const size_t umax = SIZE_MAX;
437 sink (f_size_1 (ui));
438 sink (f_size_1 (si));
440 sink (f_size_1 (UR (0, 1)));
441 sink (f_size_1 (UR (0, umax - 1)));
442 sink (f_size_1 (UR (1, umax - 1)));
443 sink (f_size_1 (UR (1, umax)));
445 sink (f_size_1 (UAR (1, 1)));
446 /* Since the only valid argument in the anti-range below is zero
447 a warning is expected even though -Walloc-zero is not specified. */
448 sink (f_size_1 (UAR (1, umax / 2))); /* { dg-warning "argument 1 range \\\[\[0-9\]+, \[0-9\]+\\\] exceeds maximum object size " } */
449 /* The only valid argument in this range is 1. */
450 sink (f_size_1 (UAR (2, umax / 2)));
452 sink (f_size_2 (ui, ui));
453 sink (f_size_2 (si, si));
454 sink (f_size_2 (ui, umax / 2));
455 sink (f_size_2 (si, umax / 2));
456 sink (f_size_2 (umax / 2, ui));
457 sink (f_size_2 (umax / 2, si));
459 sink (f_size_2 (UR (0, 1), umax)); /* { dg-warning "argument 2 value .\[0-9\]+. exceeds maximum object size " } */
460 sink (f_size_2 (UR (0, 1), umax / 2));
461 sink (f_size_2 (UR (0, umax / 2), umax / 2));
463 sink (f_size_2 (UR (umax / 2 + 1, umax / 2 + 2), ui)); /* { dg-warning "argument 1 range \\\[\[0-9\]+, \[0-9\]+\\\] exceeds maximum object size " } */
464 sink (f_size_2 (ui, UR (umax / 2 + 1, umax / 2 + 2))); /* { dg-warning "argument 2 range \\\[\[0-9\]+, \[0-9\]+\\\] exceeds maximum object size " } */
465 sink (f_size_2 (UR (umax / 2 + 1, umax), UR (umax / 2 + 1, umax))); /* { dg-warning "argument 1 range \\\[\[0-9\]+, \[0-9\]+\\\] exceeds maximum object size " } */
466 /* { dg-warning "argument 2 range \\\[\[0-9\]+, \[0-9\]+\\\] exceeds maximum object size " "argument 2" { target *-*-* } .-1 } */
468 sink (f_size_2 (SR (smin, 1), 1));
469 sink (f_size_2 (SR (smin, 1), umax / 2));
470 sink (f_size_2 (SR (-1, smax), 1));
471 sink (f_size_2 (SR (-1, smax), umax / 2));
472 sink (f_size_2 (SR (-1, 1), 1));
473 sink (f_size_2 (SR (-1, 1), umax / 2));
474 sink (f_size_2 (SR (-9, 9), 1));
475 sink (f_size_2 (SR (-9, 9), umax / 2));