Debian Codegraph @

gcc/testsuite/gcc.dg/attr-alloc_size-6.c

raw
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-options "-O0 -Wall -Walloc-size-larger-than=12345 -Wno-use-after-free" } */
8
9 #define MAXOBJSZ 12345
10
11 typedef __SIZE_TYPE__ size_t;
12
13 void sink (void*);
14
15
16 void test_lit (char *p, char *q)
17 {
18 sink (__builtin_aligned_alloc (1, MAXOBJSZ));
19 sink (__builtin_aligned_alloc (1, MAXOBJSZ + 1)); /* { dg-warning "argument 2 value .12346\[lu\]*. exceeds maximum object size 12345" } */
20
21 sink (__builtin_alloca (MAXOBJSZ));
22 sink (__builtin_alloca (MAXOBJSZ + 2)); /* { dg-warning "argument 1 value .12347\[lu\]*. exceeds maximum object size 12345" } */
23
24 sink (__builtin_calloc (MAXOBJSZ, 1));
25 sink (__builtin_calloc (1, MAXOBJSZ));
26
27 /* Verify that the signed to unsigned conversion below doesn't cause
28 a warning. */
29 sink (__builtin_calloc (p - q, 1));
30 sink (__builtin_calloc (1, q - p));
31 sink (__builtin_calloc (p - q, MAXOBJSZ));
32 sink (__builtin_calloc (MAXOBJSZ, q - p));
33
34 sink (__builtin_calloc (MAXOBJSZ / 2, 3)); /* { dg-warning "product .6172\[lu\]* \\* 3\[lu\]*. of arguments 1 and 2 exceeds maximum object size 12345" } */
35 sink (__builtin_calloc (4, MAXOBJSZ / 3)); /* { dg-warning "product .4\[lu\]* \\* 4115\[lu\]*. of arguments 1 and 2 exceeds maximum object size 12345" } */
36
37 sink (__builtin_malloc (MAXOBJSZ));
38 sink (__builtin_malloc (MAXOBJSZ + 3)); /* { dg-warning "argument 1 value .12348\[lu\]*. exceeds maximum object size 12345" } */
39
40 sink (__builtin_realloc (p, MAXOBJSZ));
41 sink (__builtin_realloc (p, MAXOBJSZ + 4)); /* { dg-warning "argument 2 value .12349\[lu\]*. exceeds maximum object size 12345" } */
42 }
43
44
45 enum { max = MAXOBJSZ };
46
47 void test_cst (char *p, char *q)
48 {
49 sink (__builtin_aligned_alloc (1, max));
50 sink (__builtin_aligned_alloc (1, max + 1)); /* { dg-warning "argument 2 value .12346\[lu\]*. exceeds maximum object size 12345" } */
51
52 sink (__builtin_alloca (max));
53 sink (__builtin_alloca (max + 2)); /* { dg-warning "argument 1 value .12347\[lu\]*. exceeds maximum object size 12345" } */
54
55 sink (__builtin_calloc (max, 1));
56 sink (__builtin_calloc (1, max));
57
58 /* Verify that the signed to unsigned conversion below doesn't cause
59 a warning. */
60 sink (__builtin_calloc (p - q, 1));
61 sink (__builtin_calloc (1, q - p));
62 sink (__builtin_calloc (p - q, max));
63 sink (__builtin_calloc (max, q - p));
64
65 sink (__builtin_calloc (max / 2, 3)); /* { dg-warning "product .6172\[lu\]* \\* 3\[lu\]*. of arguments 1 and 2 exceeds maximum object size 12345" } */
66 sink (__builtin_calloc (4, max / 3)); /* { dg-warning "product .4\[lu\]* \\* 4115\[lu\]*. of arguments 1 and 2 exceeds maximum object size 12345" } */
67
68 sink (__builtin_malloc (max));
69 sink (__builtin_malloc (max + 3)); /* { dg-warning "argument 1 value .12348\[lu\]*. exceeds maximum object size 12345" } */
70
71 sink (__builtin_realloc (p, max));
72 sink (__builtin_realloc (p, max + 4)); /* { dg-warning "argument 2 value .12349\[lu\]*. exceeds maximum object size 12345" } */
73 }