Corrected date in changelog
[official-gcc.git] / gcc / testsuite / gcc.dg / attr-alloc_size-6.c
blobbf010c536074ece0af8f3dcb0b849d0fd5d12ec0
1 /* PR c/78284 - warn on malloc with very large arguments
2 Test exercising the ability of the built-in allocation functions
3 to detect and diagnose, without optimization, calls that attemnpt
4 to allocate objects in excess of the number of bytes specified by
5 -Walloc-larger-than=maximum. */
6 /* { dg-do compile } */
7 /* { dg-require-effective-target alloca } */
8 /* { dg-options "-O0 -Wall -Walloc-size-larger-than=12345" } */
10 #define MAXOBJSZ 12345
12 typedef __SIZE_TYPE__ size_t;
14 void sink (void*);
17 void test_lit (char *p, char *q)
19 sink (__builtin_aligned_alloc (1, MAXOBJSZ));
20 sink (__builtin_aligned_alloc (1, MAXOBJSZ + 1)); /* { dg-warning "argument 2 value .12346\[lu\]*. exceeds maximum object size 12345" } */
22 sink (__builtin_alloca (MAXOBJSZ));
23 sink (__builtin_alloca (MAXOBJSZ + 2)); /* { dg-warning "argument 1 value .12347\[lu\]*. exceeds maximum object size 12345" } */
25 sink (__builtin_calloc (MAXOBJSZ, 1));
26 sink (__builtin_calloc (1, MAXOBJSZ));
28 /* Verify that the signed to unsigned conversion below doesn't cause
29 a warning. */
30 sink (__builtin_calloc (p - q, 1));
31 sink (__builtin_calloc (1, q - p));
32 sink (__builtin_calloc (p - q, MAXOBJSZ));
33 sink (__builtin_calloc (MAXOBJSZ, q - p));
35 sink (__builtin_calloc (MAXOBJSZ / 2, 3)); /* { dg-warning "product .6172\[lu\]* \\* 3\[lu\]*. of arguments 1 and 2 exceeds maximum object size 12345" } */
36 sink (__builtin_calloc (4, MAXOBJSZ / 3)); /* { dg-warning "product .4\[lu\]* \\* 4115\[lu\]*. of arguments 1 and 2 exceeds maximum object size 12345" } */
38 sink (__builtin_malloc (MAXOBJSZ));
39 sink (__builtin_malloc (MAXOBJSZ + 3)); /* { dg-warning "argument 1 value .12348\[lu\]*. exceeds maximum object size 12345" } */
41 sink (__builtin_realloc (p, MAXOBJSZ));
42 sink (__builtin_realloc (p, MAXOBJSZ + 4)); /* { dg-warning "argument 2 value .12349\[lu\]*. exceeds maximum object size 12345" } */
46 enum { max = MAXOBJSZ };
48 void test_cst (char *p, char *q)
50 sink (__builtin_aligned_alloc (1, max));
51 sink (__builtin_aligned_alloc (1, max + 1)); /* { dg-warning "argument 2 value .12346\[lu\]*. exceeds maximum object size 12345" } */
53 sink (__builtin_alloca (max));
54 sink (__builtin_alloca (max + 2)); /* { dg-warning "argument 1 value .12347\[lu\]*. exceeds maximum object size 12345" } */
56 sink (__builtin_calloc (max, 1));
57 sink (__builtin_calloc (1, max));
59 /* Verify that the signed to unsigned conversion below doesn't cause
60 a warning. */
61 sink (__builtin_calloc (p - q, 1));
62 sink (__builtin_calloc (1, q - p));
63 sink (__builtin_calloc (p - q, max));
64 sink (__builtin_calloc (max, q - p));
66 sink (__builtin_calloc (max / 2, 3)); /* { dg-warning "product .6172\[lu\]* \\* 3\[lu\]*. of arguments 1 and 2 exceeds maximum object size 12345" } */
67 sink (__builtin_calloc (4, max / 3)); /* { dg-warning "product .4\[lu\]* \\* 4115\[lu\]*. of arguments 1 and 2 exceeds maximum object size 12345" } */
69 sink (__builtin_malloc (max));
70 sink (__builtin_malloc (max + 3)); /* { dg-warning "argument 1 value .12348\[lu\]*. exceeds maximum object size 12345" } */
72 sink (__builtin_realloc (p, max));
73 sink (__builtin_realloc (p, max + 4)); /* { dg-warning "argument 2 value .12349\[lu\]*. exceeds maximum object size 12345" } */