[clang][NFC] simplify the unset check in `ParseLabeledStatement` (#117430)
[llvm-project.git] / clang / test / Analysis / kmalloc-linux.c
blob0114c4ef000e2806dd1d0760b089bcb9b08f8a09
1 // RUN: %clang_analyze_cc1 -triple x86_64-unknown-linux %s -verify \
2 // RUN: -Wno-incompatible-library-redeclaration \
3 // RUN: -analyzer-checker=core \
4 // RUN: -analyzer-checker=unix.Malloc
6 #define __GFP_ZERO 0x8000
7 #define NULL ((void *)0)
9 typedef __typeof(sizeof(int)) size_t;
11 void *kmalloc(size_t, int);
12 void kfree(void *);
14 struct test {
17 void foo(struct test *);
19 void test_zeroed(void) {
20 struct test **list, *t;
21 int i;
23 list = kmalloc(sizeof(*list) * 10, __GFP_ZERO);
24 if (list == NULL)
25 return;
27 for (i = 0; i < 10; i++) {
28 t = list[i];
29 foo(t);
31 kfree(list); // no-warning
34 void test_nonzero(void) {
35 struct test **list, *t;
36 int i;
38 list = kmalloc(sizeof(*list) * 10, 0);
39 if (list == NULL)
40 return;
42 for (i = 0; i < 10; i++) {
43 t = list[i]; // expected-warning{{undefined}}
44 foo(t);
46 kfree(list);
49 void test_indeterminate(int flags) {
50 struct test **list, *t;
51 int i;
53 list = kmalloc(sizeof(*list) * 10, flags);
54 if (list == NULL)
55 return;
57 for (i = 0; i < 10; i++) {
58 t = list[i]; // expected-warning{{undefined}}
59 foo(t);
61 kfree(list);
64 typedef unsigned long long uint64_t;
66 struct malloc_type;
68 // 3 parameter malloc:
69 // https://www.freebsd.org/cgi/man.cgi?query=malloc&sektion=9
70 void *malloc(unsigned long size, struct malloc_type *mtp, int flags);
72 void test_3arg_malloc(struct malloc_type *mtp) {
73 struct test **list, *t;
74 int i;
76 list = malloc(sizeof(*list) * 10, mtp, __GFP_ZERO);
77 if (list == NULL)
78 return;
80 for (i = 0; i < 10; i++) {
81 t = list[i];
82 foo(t);
84 kfree(list); // no-warning
87 void test_3arg_malloc_nonzero(struct malloc_type *mtp) {
88 struct test **list, *t;
89 int i;
91 list = malloc(sizeof(*list) * 10, mtp, 0);
92 if (list == NULL)
93 return;
95 for (i = 0; i < 10; i++) {
96 t = list[i]; // expected-warning{{undefined}}
97 foo(t);
99 kfree(list);
102 void test_3arg_malloc_indeterminate(struct malloc_type *mtp, int flags) {
103 struct test **list, *t;
104 int i;
106 list = malloc(sizeof(*list) * 10, mtp, flags);
107 if (list == NULL)
108 return;
110 for (i = 0; i < 10; i++) {
111 t = list[i]; // expected-warning{{undefined}}
112 foo(t);
114 kfree(list);
117 void test_3arg_malloc_leak(struct malloc_type *mtp, int flags) {
118 struct test **list;
120 list = malloc(sizeof(*list) * 10, mtp, flags);
121 if (list == NULL)
122 return;
123 } // expected-warning{{Potential leak of memory pointed to by 'list'}}
125 // kmalloc can return a constant value defined in ZERO_SIZE_PTR
126 // if a block of size 0 is requested
127 #define ZERO_SIZE_PTR ((void *)16)
129 void test_kfree_ZERO_SIZE_PTR(void) {
130 void *ptr = ZERO_SIZE_PTR;
131 kfree(ptr); // no warning about freeing this value
134 void test_kfree_other_constant_value(void) {
135 void *ptr = (void *)1;
136 kfree(ptr); // expected-warning{{Argument to 'kfree()' is a constant address (1)}}