Debian Codegraph @

gcc/testsuite/gcc.dg/Walloc-size-4.c

raw
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 (struct 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 struct S *p = (struct S *) __builtin_malloc (sizeof *p);
16 __builtin_free (p);
17 p = (struct S *) __builtin_malloc (sizeof p); /* { dg-warning "allocation of insufficient size" } */
18 __builtin_free (p);
19 p = (struct S *) __builtin_alloca (sizeof p); /* { dg-warning "allocation of insufficient size" } */
20 bar (p);
21 p = (struct S *) __builtin_calloc (1, sizeof p); /* { dg-warning "allocation of insufficient size" } */
22 __builtin_free (p);
23 bar ((struct S *) __builtin_malloc (4)); /* { dg-warning "allocation of insufficient size" } */
24 __builtin_free (p);
25 p = (struct S *) __builtin_calloc (sizeof *p, 1);
26 __builtin_free (p);
27 p = __builtin_calloc (sizeof *p, 1);
28 __builtin_free (p);
29 }
30
31 void
32 baz (void)
33 {
34 struct S *p = (struct S *) mymalloc (42, 42, sizeof *p);
35 myfree (p, 42, 42);
36 p = (struct S *) mymalloc (42, 42, sizeof p); /* { dg-warning "allocation of insufficient size" } */
37 myfree (p, 42, 42);
38 p = (struct S *) mycalloc (42, 42, 1, sizeof p); /* { dg-warning "allocation of insufficient size" } */
39 myfree (p, 42, 42);
40 bar ((struct S *) mymalloc (42, 42, 4)); /* { dg-warning "allocation of insufficient size" } */
41 myfree (p, 42, 42);
42 p = (struct S *) mycalloc (42, 42, sizeof *p, 1);
43 myfree (p, 42, 42);
44 p = mycalloc (42, 42, sizeof *p, 1);
45 myfree (p, 42, 42);
46 p = mymalloc (42, 42, sizeof *p);
47 myfree (p, 42, 42);
48 p = mymalloc (42, 42, sizeof p); /* { dg-warning "allocation of insufficient size" } */
49 myfree (p, 42, 42);
50 p = mycalloc (42, 42, 1, sizeof p); /* { dg-warning "allocation of insufficient size" } */
51 myfree (p, 42, 42);
52 bar (mymalloc (42, 42, 4)); /* { dg-warning "allocation of insufficient size" } */
53 myfree (p, 42, 42);
54 }