| 1 |
// Tests the warnings for insufficient allocation size. |
| 2 |
// { dg-do compile } |
| 3 |
// { dg-options "-Walloc-size -Wno-calloc-transposed-args" } |
| 4 |
|
| 5 |
struct S { int x[10]; }; |
| 6 |
void bar (S *); |
| 7 |
typedef __SIZE_TYPE__ size_t; |
| 8 |
void *myfree (void *, int, int); |
| 9 |
void *mymalloc (int, int, size_t) __attribute__((malloc, malloc (myfree), alloc_size (3))); |
| 10 |
void *mycalloc (int, int, size_t, size_t) __attribute__((malloc, malloc (myfree), alloc_size (3, 4))); |
| 11 |
|
| 12 |
void |
| 13 |
foo (void) |
| 14 |
{ |
| 15 |
S *p = (S *) __builtin_malloc (sizeof *p); |
| 16 |
__builtin_free (p); |
| 17 |
p = (S *) __builtin_malloc (sizeof p); // { dg-warning "allocation of insufficient size" } |
| 18 |
__builtin_free (p); |
| 19 |
p = (S *) __builtin_alloca (sizeof p); // { dg-warning "allocation of insufficient size" } |
| 20 |
bar (p); |
| 21 |
p = (S *) __builtin_calloc (1, sizeof p); // { dg-warning "allocation of insufficient size" } |
| 22 |
__builtin_free (p); |
| 23 |
bar ((S *) __builtin_malloc (4)); // { dg-warning "allocation of insufficient size" } |
| 24 |
__builtin_free (p); |
| 25 |
p = (S *) __builtin_calloc (sizeof *p, 1); |
| 26 |
__builtin_free (p); |
| 27 |
} |
| 28 |
|
| 29 |
void |
| 30 |
baz (void) |
| 31 |
{ |
| 32 |
S *p = (S *) mymalloc (42, 42, sizeof *p); |
| 33 |
myfree (p, 42, 42); |
| 34 |
p = (S *) mymalloc (42, 42, sizeof p); // { dg-warning "allocation of insufficient size" } |
| 35 |
myfree (p, 42, 42); |
| 36 |
p = (S *) mycalloc (42, 42, 1, sizeof p); // { dg-warning "allocation of insufficient size" } |
| 37 |
myfree (p, 42, 42); |
| 38 |
bar ((S *) mymalloc (42, 42, 4)); // { dg-warning "allocation of insufficient size" } |
| 39 |
myfree (p, 42, 42); |
| 40 |
p = (S *) mycalloc (42, 42, sizeof *p, 1); |
| 41 |
myfree (p, 42, 42); |
| 42 |
p = static_cast <S *> (mycalloc (42, 42, sizeof *p, 1)); |
| 43 |
myfree (p, 42, 42); |
| 44 |
p = static_cast <S *> (mymalloc (42, 42, sizeof *p)); |
| 45 |
myfree (p, 42, 42); |
| 46 |
p = static_cast <S *> (mymalloc (42, 42, sizeof p)); // { dg-warning "allocation of insufficient size" } |
| 47 |
myfree (p, 42, 42); |
| 48 |
p = static_cast <S *> (mycalloc (42, 42, 1, sizeof p)); // { dg-warning "allocation of insufficient size" } |
| 49 |
myfree (p, 42, 42); |
| 50 |
bar (static_cast <S *> (mymalloc (42, 42, 4))); // { dg-warning "allocation of insufficient size" } |
| 51 |
myfree (p, 42, 42); |
| 52 |
} |