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}}
44 // Test for radar://11110132.
47 Foo(void* data
) : m_data(data
) {}
53 // Assume that functions which take a function pointer can free memory even if
54 // they are defined in system headers and take the const pointer to the
55 // allocated memory. (radar://11160612)
56 // Test default parameter.
57 int const_ptr_and_callback_def_param(int, const char*, int n
, void(*)(void*) = free
);
59 char *x
= (char*)malloc(12);
60 const_ptr_and_callback_def_param(0, x
, 12);
63 int const_ptr_and_callback_def_param_null(int, const char*, int n
, void(*)(void*) = 0);
64 void r11160612_no_callback() {
65 char *x
= (char*)malloc(12);
66 const_ptr_and_callback_def_param_null(0, x
, 12);
67 } // expected-warning{{leak}}
69 // Test member function pointer.
70 struct CanFreeMemory
{
71 static void myFree(void*);
73 //This is handled because we look at the type of the parameter(not argument).
74 void r11160612_3(CanFreeMemory
* p
) {
75 char *x
= (char*)malloc(12);
76 const_ptr_and_callback_def_param(0, x
, 12, p
->myFree
);
87 void push_back(void *Item
) {
88 storage
[length
++] = Item
;
92 void testDestructors() {
94 v
.push_back(malloc(4));
95 // no leak warning; freed in destructor
99 struct X
{ void *a
; };
103 result
.a
= malloc(4);
104 return result
; // no-warning
107 // Ensure that regions accessible through a LazyCompoundVal trigger region escape.
108 // Malloc checker used to report leaks for the following two test cases.
115 void append(Property x
);
117 void appendWrapper(char *getterName
) {
118 append(Property(getterName
));
120 void foo(const char* name
) {
121 char* getterName
= strdup(name
);
122 appendWrapper(getterName
); // no-warning
125 struct NestedProperty
{
127 NestedProperty(Property p
)
130 void appendNested(NestedProperty x
);
132 void appendWrapperNested(char *getterName
) {
133 appendNested(NestedProperty(Property(getterName
)));
135 void fooNested(const char* name
) {
136 char* getterName
= strdup(name
);
137 appendWrapperNested(getterName
); // no-warning
145 struct b1
: virtual b2
{
154 p
->m(); // no-crash // no-warning
158 // Allow __cxa_demangle to escape.
159 char* test_cxa_demangle(const char* sym
) {
160 size_t funcnamesize
= 256;
161 char* funcname
= (char*)malloc(funcnamesize
);
163 char* ret
= abi::__cxa_demangle(sym
, funcname
, &funcnamesize
, &status
);
167 return funcname
; // no-warning
170 namespace argument_leak
{
177 name
= static_cast<char *>(malloc(10));
191 } // namespace argument_leak
193 #define ZERO_SIZE_PTR ((void *)16)
195 void test_delete_ZERO_SIZE_PTR() {
196 int *Ptr
= (int *)ZERO_SIZE_PTR
;
197 // ZERO_SIZE_PTR is specially handled but only for malloc family
198 delete Ptr
; // expected-warning{{Argument to 'delete' is a constant address (16)}}
201 namespace pr46253_class
{
203 void *realloc(int, bool = false) { realloc(1); } // no-crash
205 } // namespace pr46253_class
207 namespace pr46253_retty
{
208 void realloc(void *ptr
, size_t size
) { realloc(ptr
, size
); } // no-crash
209 } // namespace pr46253_retty
211 namespace pr46253_paramty
{
212 void *realloc(void **ptr
, size_t size
) { realloc(ptr
, size
); } // no-crash
213 } // namespace pr46253_paramty
215 namespace pr46253_paramty2
{
216 void *realloc(void *ptr
, int size
) { realloc(ptr
, size
); } // no-crash
217 } // namespace pr46253_paramty2