Debian Codegraph @

clang/test/Sema/warn-alloc-size.c

raw
1 // RUN: %clang_cc1 -triple x86_64-linux -fsyntax-only -verify -Walloc-size %s
2 struct Foo { int x[10]; };
3
4 typedef __typeof__(sizeof(int)) size_t;
5 void *my_malloc(size_t) __attribute__((alloc_size(1)));
6 void *my_calloc(size_t, size_t) __attribute__((alloc_size(2, 1)));
7
8 void foo_consumer(struct Foo* p);
9
10 void alloc_foo(void) {
11 struct Foo *ptr1 = my_malloc(sizeof(struct Foo));
12 struct Foo *ptr2 = my_malloc(sizeof(*ptr2));
13 struct Foo *ptr3 = my_calloc(1, sizeof(*ptr3));
14 struct Foo *ptr4 = my_calloc(sizeof(*ptr4), 1);
15 struct Foo (*ptr5)[5] = my_malloc(sizeof(*ptr5));
16 void *ptr6 = my_malloc(4);
17
18 // Test insufficient size with different allocation functions.
19 struct Foo *ptr7 = my_malloc(sizeof(ptr7)); // expected-warning {{allocation of insufficient size '8' for type 'struct Foo' with size '40'}}
20 struct Foo *ptr8 = my_calloc(1, sizeof(ptr8)); // expected-warning {{allocation of insufficient size '8' for type 'struct Foo' with size '40'}}
21 struct Foo *ptr9 = my_calloc(sizeof(ptr9), 1); // expected-warning {{allocation of insufficient size '8' for type 'struct Foo' with size '40'}}
22
23 // Test function arguments.
24 foo_consumer(my_malloc(4)); // expected-warning {{allocation of insufficient size '4' for type 'struct Foo' with size '40'}}
25
26 // Test explicit cast.
27 struct Foo *ptr10 = (struct Foo *)my_malloc(sizeof(*ptr10));
28 struct Foo *ptr11 = (struct Foo *)my_malloc(sizeof(ptr11)); // expected-warning {{allocation of insufficient size '8' for type 'struct Foo' with size '40'}}
29 struct Foo *ptr12 = (struct Foo *)my_calloc(1, sizeof(ptr12)); // expected-warning {{allocation of insufficient size '8' for type 'struct Foo' with size '40'}}
30 struct Foo *ptr13 = (struct Foo *)my_malloc(4); // expected-warning {{allocation of insufficient size '4' for type 'struct Foo' with size '40'}}
31 void *ptr14 = (struct Foo *)my_malloc(4); // expected-warning {{allocation of insufficient size '4' for type 'struct Foo' with size '40'}}
32
33 struct Foo *ptr15 = (void *)(struct Foo *)my_malloc(4); // expected-warning 2 {{allocation of insufficient size '4' for type 'struct Foo' with size '40'}}
34 int *ptr16 = (unsigned *)(void *)(int *)my_malloc(1); // expected-warning {{initializing 'int *' with an expression of type 'unsigned int *' converts between pointers to integer types with different sign}}
35 // expected-warning@-1 {{allocation of insufficient size '1' for type 'int' with size '4'}}
36 // expected-warning@-2 {{allocation of insufficient size '1' for type 'unsigned int' with size '4'}}
37 int *ptr17 = (void *)(int *)my_malloc(1); // expected-warning {{allocation of insufficient size '1' for type 'int' with size '4'}}
38 // expected-warning@-1 {{allocation of insufficient size '1' for type 'int' with size '4'}}
39 (void)(int *)my_malloc(1); // expected-warning {{allocation of insufficient size '1' for type 'int' with size '4'}}
40
41 void *funcptr_1 = (void (*)(int))my_malloc(1);
42
43 // Zero size allocations are assumed to be intentional.
44 int *zero_alloc1 = my_malloc(0);
45 int *zero_alloc2 = (int *)my_malloc(0);
46 }