| 1 |
/* PR c/78284 - warn on malloc with very large arguments |
| 2 |
Test to exercise the interaction of the -Walloca-larger-than, |
| 3 |
-Wvla-larger-than, and -Walloc-size-larger-than options. The former |
| 4 |
two more specific options override the more general latter option. */ |
| 5 |
/* { dg-do compile } */ |
| 6 |
/* { dg-options "-O -Walloc-size-larger-than=123 -Walloca-larger-than=234 -Wvla-larger-than=345" } */ |
| 7 |
|
| 8 |
typedef __SIZE_TYPE__ size_t; |
| 9 |
|
| 10 |
void sink (void*); |
| 11 |
|
| 12 |
static size_t alloc_size_limit (void) |
| 13 |
{ |
| 14 |
return 123; |
| 15 |
} |
| 16 |
|
| 17 |
static size_t alloca_limit (void) |
| 18 |
{ |
| 19 |
return 234; |
| 20 |
} |
| 21 |
|
| 22 |
static size_t vla_limit (void) |
| 23 |
{ |
| 24 |
return 345; |
| 25 |
} |
| 26 |
|
| 27 |
void test_alloca (void) |
| 28 |
{ |
| 29 |
void *p; |
| 30 |
|
| 31 |
/* No warning should be issued for the following call because the more |
| 32 |
permissive alloca limit overrides the stricter alloc_size limit. */ |
| 33 |
p = __builtin_alloca (alloca_limit ()); |
| 34 |
sink (p); |
| 35 |
|
| 36 |
p = __builtin_alloca (alloca_limit () + 1); /* { dg-warning "argument to .alloca. is too large" } */ |
| 37 |
sink (p); |
| 38 |
} |
| 39 |
|
| 40 |
void test_vla (void) |
| 41 |
{ |
| 42 |
/* Same as above, no warning should be issued here because the more |
| 43 |
permissive VLA limit overrides the stricter alloc_size limit. */ |
| 44 |
char vla1 [vla_limit ()]; |
| 45 |
sink (vla1); |
| 46 |
|
| 47 |
char vla2 [vla_limit () + 1]; /* { dg-warning "argument to variable-length array is too large" } */ |
| 48 |
sink (vla2); |
| 49 |
} |
| 50 |
|
| 51 |
void test_malloc (void) |
| 52 |
{ |
| 53 |
void *p; |
| 54 |
p = __builtin_malloc (alloc_size_limit ()); |
| 55 |
sink (p); |
| 56 |
|
| 57 |
p = __builtin_malloc (alloc_size_limit () + 1); /* { dg-warning "argument 1 value .124\[lu\]*. exceeds maximum object size 123" } */ |
| 58 |
sink (p); |
| 59 |
} |