1 // RUN: %clang_analyze_cc1 -fblocks -verify %s \
2 // RUN: -analyzer-checker=core \
3 // RUN: -analyzer-checker=unix.Malloc
5 // RUN: %clang_analyze_cc1 -fblocks -verify %s \
6 // RUN: -analyzer-checker=core \
7 // RUN: -analyzer-checker=unix.Malloc \
8 // RUN: -analyzer-config unix.DynamicMemoryModeling:Optimistic=true
10 using size_t = decltype(sizeof(int));
14 extern "C" void free(void *);
15 extern "C" void *alloca(std::size_t);
20 // expected-warning@-1{{Argument to free() is the address of the local variable 'a', which is not memory allocated by malloc()}}
21 // expected-warning@-2{{attempt to call free on non-heap object 'a'}}
27 // expected-warning@-1{{Argument to free() is the address of the local variable 'a', which is not memory allocated by malloc()}}
28 // expected-warning@-2{{attempt to call std::free on non-heap object 'a'}}
34 // expected-warning@-1{{Argument to free() is the address of the local variable 'a', which is not memory allocated by malloc()}}
35 // expected-warning@-2{{attempt to call free on non-heap object 'a'}}
41 // expected-warning@-1{{Argument to free() is the address of the local variable 'a', which is not memory allocated by malloc()}}
42 // expected-warning@-2{{attempt to call std::free on non-heap object 'a'}}
46 static int a
[] = { 1 };
48 // expected-warning@-1{{Argument to free() is the address of the static variable 'a', which is not memory allocated by malloc()}}
49 // expected-warning@-2{{attempt to call free on non-heap object 'a'}}
53 static int a
[] = { 1 };
55 // expected-warning@-1{{Argument to free() is the address of the static variable 'a', which is not memory allocated by malloc()}}
56 // expected-warning@-2{{attempt to call std::free on non-heap object 'a'}}
60 free(x
); // no-warning
64 std::free(x
); // no-warning
69 free(ptr()); // no-warning
74 std::free(ptr()); // no-warning
79 // expected-warning@-1{{Argument to free() is a constant address (1000), which is not memory allocated by malloc()}}
80 // expected-warning@-2{{attempt to call free on non-heap object '(void *)1000'}}
84 std::free((void*)1000);
85 // expected-warning@-1{{Argument to free() is a constant address (1000), which is not memory allocated by malloc()}}
86 // expected-warning@-2{{attempt to call std::free on non-heap object '(void *)1000'}}
90 free(*x
); // no-warning
94 std::free(*x
); // no-warning
99 free((*x
)+8); // no-warning
102 void t8b (char **x
) {
104 std::free((*x
)+8); // no-warning
110 // expected-warning@-1{{Argument to free() is the address of the label 'label', which is not memory allocated by malloc()}}
111 // expected-warning@-2{{attempt to call free on non-heap object 'label'}}
117 // expected-warning@-1{{Argument to free() is the address of the label 'label', which is not memory allocated by malloc()}}
118 // expected-warning@-2{{attempt to call std::free on non-heap object 'label'}}
123 // expected-warning@-1{{Argument to free() is the address of the function 't10a', which is not memory allocated by malloc()}}
124 // expected-warning@-2{{attempt to call free on non-heap object 't10a'}}
128 std::free((void*)&t10b
);
129 // expected-warning@-1{{Argument to free() is the address of the function 't10b', which is not memory allocated by malloc()}}
130 // expected-warning@-2{{attempt to call std::free on non-heap object 't10b'}}
134 char *p
= (char*)alloca(2);
135 free(p
); // expected-warning {{Memory allocated by alloca() should not be deallocated}}
139 char *p
= (char*)alloca(2);
140 std::free(p
); // expected-warning {{Memory allocated by alloca() should not be deallocated}}
144 char *p
= (char*)__builtin_alloca(2);
145 free(p
); // expected-warning {{Memory allocated by alloca() should not be deallocated}}
149 char *p
= (char*)__builtin_alloca(2);
150 std::free(p
); // expected-warning {{Memory allocated by alloca() should not be deallocated}}
155 // expected-warning@-1{{Argument to free() is a block, which is not memory allocated by malloc()}}
156 // expected-warning@-2{{attempt to call free on non-heap object: block expression}}
160 std::free(^{return;});
161 // expected-warning@-1{{Argument to free() is a block, which is not memory allocated by malloc()}}
162 // expected-warning@-2{{attempt to call std::free on non-heap object: block expression}}
166 free((void *)+[]{ return; });
167 // expected-warning@-1{{Argument to free() is the address of the function '__invoke', which is not memory allocated by malloc()}}
168 // expected-warning@-2{{attempt to call free on non-heap object: lambda-to-function-pointer conversion}}
172 std::free((void *)+[]{ return; });
173 // expected-warning@-1{{Argument to free() is the address of the function '__invoke', which is not memory allocated by malloc()}}
174 // expected-warning@-2{{attempt to call std::free on non-heap object: lambda-to-function-pointer conversion}}
179 // expected-warning@-1{{Argument to free() is the address of the parameter 'a', which is not memory allocated by malloc()}}
180 // expected-warning@-2{{attempt to call free on non-heap object 'a'}}
185 // expected-warning@-1{{Argument to free() is the address of the parameter 'a', which is not memory allocated by malloc()}}
186 // expected-warning@-2{{attempt to call std::free on non-heap object 'a'}}
189 static int someGlobal
[2];
192 // expected-warning@-1{{Argument to free() is the address of the global variable 'someGlobal', which is not memory allocated by malloc()}}
193 // expected-warning@-2{{attempt to call free on non-heap object 'someGlobal'}}
197 std::free(someGlobal
);
198 // expected-warning@-1{{Argument to free() is the address of the global variable 'someGlobal', which is not memory allocated by malloc()}}
199 // expected-warning@-2{{attempt to call std::free on non-heap object 'someGlobal'}}
202 void t17a (char **x
, int offset
) {
204 free(x
[offset
]); // no-warning
207 void t17b (char **x
, int offset
) {
209 std::free(x
[offset
]); // no-warning
216 void t18_C_style_C_style_free (S s
) {
217 free((void*)(unsigned long long)s
.p
); // no warning
220 void t18_C_style_C_style_std_free (S s
) {
221 std::free((void*)(unsigned long long)s
.p
); // no warning
224 void t18_C_style_reinterpret_free (S s
) {
225 free((void*)reinterpret_cast<unsigned long long>(s
.p
)); // no warning
228 void t18_C_style_reinterpret_std_free (S s
) {
229 std::free((void*)reinterpret_cast<unsigned long long>(s
.p
)); // no warning
232 void t18_reinterpret_C_style_free (S s
) {
233 free(reinterpret_cast<void*>((unsigned long long)(s
.p
))); // no warning
236 void t18_reinterpret_C_style_std_free (S s
) {
237 std::free(reinterpret_cast<void*>((unsigned long long)(s
.p
))); // no warning
240 void t18_reinterpret_reinterpret_free (S s
) {
241 free(reinterpret_cast<void*>(reinterpret_cast<unsigned long long>(s
.p
))); // no warning
244 void t18_reinterpret_reinterpret_std_free (S s
) {
245 std::free(reinterpret_cast<void*>(reinterpret_cast<unsigned long long>(s
.p
))); // no warning