1 // RUN: %clang_analyze_cc1 -w -verify %s \
2 // RUN: -analyzer-checker=core \
3 // RUN: -analyzer-checker=alpha.deadcode.UnreachableCode \
4 // RUN: -analyzer-checker=alpha.core.CastSize \
5 // RUN: -analyzer-checker=unix.Malloc \
6 // RUN: -analyzer-checker=cplusplus.NewDelete
8 // RUN: %clang_analyze_cc1 -w -verify %s \
9 // RUN: -triple i386-unknown-linux-gnu \
10 // RUN: -analyzer-checker=core \
11 // RUN: -analyzer-checker=alpha.deadcode.UnreachableCode \
12 // RUN: -analyzer-checker=alpha.core.CastSize \
13 // RUN: -analyzer-checker=unix.Malloc \
14 // RUN: -analyzer-checker=cplusplus.NewDelete
16 // RUN: %clang_analyze_cc1 -w -verify %s -DTEST_INLINABLE_ALLOCATORS \
17 // RUN: -analyzer-checker=core \
18 // RUN: -analyzer-checker=alpha.deadcode.UnreachableCode \
19 // RUN: -analyzer-checker=alpha.core.CastSize \
20 // RUN: -analyzer-checker=unix.Malloc \
21 // RUN: -analyzer-checker=cplusplus.NewDelete
23 // RUN: %clang_analyze_cc1 -w -verify %s -DTEST_INLINABLE_ALLOCATORS \
24 // RUN: -triple i386-unknown-linux-gnu \
25 // RUN: -analyzer-checker=core \
26 // RUN: -analyzer-checker=alpha.deadcode.UnreachableCode \
27 // RUN: -analyzer-checker=alpha.core.CastSize \
28 // RUN: -analyzer-checker=unix.Malloc \
29 // RUN: -analyzer-checker=cplusplus.NewDelete
31 #include "Inputs/system-header-simulator-cxx.h"
33 typedef __typeof(sizeof(int)) size_t;
36 void *realloc(void *ptr
, size_t size
);
37 void *calloc(size_t nmemb
, size_t size
);
38 char *strdup(const char *s
);
40 void checkThatMallocCheckerIsRunning() {
42 } // expected-warning{{leak}}
46 Foo(void* data
) : m_data(data
) {}
52 // Assume that functions which take a function pointer can free memory even if
53 // they are defined in system headers and take the const pointer to the
55 // Test default parameter.
56 int const_ptr_and_callback_def_param(int, const char*, int n
, void(*)(void*) = free
);
58 char *x
= (char*)malloc(12);
59 const_ptr_and_callback_def_param(0, x
, 12);
62 int const_ptr_and_callback_def_param_null(int, const char*, int n
, void(*)(void*) = 0);
63 void r11160612_no_callback() {
64 char *x
= (char*)malloc(12);
65 const_ptr_and_callback_def_param_null(0, x
, 12);
66 } // expected-warning{{leak}}
68 // Test member function pointer.
69 struct CanFreeMemory
{
70 static void myFree(void*);
72 //This is handled because we look at the type of the parameter(not argument).
73 void r11160612_3(CanFreeMemory
* p
) {
74 char *x
= (char*)malloc(12);
75 const_ptr_and_callback_def_param(0, x
, 12, p
->myFree
);
86 void push_back(void *Item
) {
87 storage
[length
++] = Item
;
91 void testDestructors() {
93 v
.push_back(malloc(4));
94 // no leak warning; freed in destructor
98 struct X
{ void *a
; };
102 result
.a
= malloc(4);
103 return result
; // no-warning
106 // Ensure that regions accessible through a LazyCompoundVal trigger region escape.
107 // Malloc checker used to report leaks for the following two test cases.
114 void append(Property x
);
116 void appendWrapper(char *getterName
) {
117 append(Property(getterName
));
119 void foo(const char* name
) {
120 char* getterName
= strdup(name
);
121 appendWrapper(getterName
); // no-warning
124 struct NestedProperty
{
126 NestedProperty(Property p
)
129 void appendNested(NestedProperty x
);
131 void appendWrapperNested(char *getterName
) {
132 appendNested(NestedProperty(Property(getterName
)));
134 void fooNested(const char* name
) {
135 char* getterName
= strdup(name
);
136 appendWrapperNested(getterName
); // no-warning
144 struct b1
: virtual b2
{
153 p
->m(); // no-crash // no-warning
157 // Allow __cxa_demangle to escape.
158 char* test_cxa_demangle(const char* sym
) {
159 size_t funcnamesize
= 256;
160 char* funcname
= (char*)malloc(funcnamesize
);
162 char* ret
= abi::__cxa_demangle(sym
, funcname
, &funcnamesize
, &status
);
166 return funcname
; // no-warning
169 namespace argument_leak
{
176 name
= static_cast<char *>(malloc(10));
190 } // namespace argument_leak
192 #define ZERO_SIZE_PTR ((void *)16)
194 void test_delete_ZERO_SIZE_PTR() {
195 int *Ptr
= (int *)ZERO_SIZE_PTR
;
196 // ZERO_SIZE_PTR is specially handled but only for malloc family
197 delete Ptr
; // expected-warning{{Argument to 'delete' is a constant address (16)}}
200 namespace pr46253_class
{
202 void *realloc(int, bool = false) { realloc(1); } // no-crash
204 } // namespace pr46253_class
206 namespace pr46253_retty
{
207 void realloc(void *ptr
, size_t size
) { realloc(ptr
, size
); } // no-crash
208 } // namespace pr46253_retty
210 namespace pr46253_paramty
{
211 void *realloc(void **ptr
, size_t size
) { realloc(ptr
, size
); } // no-crash
212 } // namespace pr46253_paramty
214 namespace pr46253_paramty2
{
215 void *realloc(void *ptr
, int size
) { realloc(ptr
, size
); } // no-crash
216 } // namespace pr46253_paramty2