1 // RUN: %clang_analyze_cc1 -w -analyzer-checker=core,alpha.deadcode.UnreachableCode,alpha.core.CastSize,unix.Malloc,cplusplus.NewDelete -analyzer-store=region -verify %s
2 // RUN: %clang_analyze_cc1 -triple i386-unknown-linux-gnu -w -analyzer-checker=core,alpha.deadcode.UnreachableCode,alpha.core.CastSize,unix.Malloc,cplusplus.NewDelete -analyzer-store=region -verify %s
3 // RUN: %clang_analyze_cc1 -w -analyzer-checker=core,alpha.deadcode.UnreachableCode,alpha.core.CastSize,unix.Malloc,cplusplus.NewDelete -analyzer-store=region -DTEST_INLINABLE_ALLOCATORS -verify %s
4 // RUN: %clang_analyze_cc1 -triple i386-unknown-linux-gnu -w -analyzer-checker=core,alpha.deadcode.UnreachableCode,alpha.core.CastSize,unix.Malloc,cplusplus.NewDelete -analyzer-store=region -DTEST_INLINABLE_ALLOCATORS -verify %s
5
6 #include "Inputs/system-header-simulator-cxx.h"
7
8 typedef __typeof(sizeof(int)) size_t;
9 void *malloc(size_t);
10 void free(void *);
11 void *realloc(void *ptr, size_t size);
12 void *calloc(size_t nmemb, size_t size);
13 char *strdup(const char *s);
14
checkThatMallocCheckerIsRunning()15 void checkThatMallocCheckerIsRunning() {
16 malloc(4);
17 } // expected-warning{{leak}}
18
19 // Test for radar://11110132.
20 struct Foo {
21 mutable void* m_data;
FooFoo22 Foo(void* data) : m_data(data) {}
23 };
aFunction()24 Foo aFunction() {
25 return malloc(10);
26 }
27
28 // Assume that functions which take a function pointer can free memory even if
29 // they are defined in system headers and take the const pointer to the
30 // allocated memory. (radar://11160612)
31 // Test default parameter.
32 int const_ptr_and_callback_def_param(int, const char*, int n, void(*)(void*) = free);
r11160612_3()33 void r11160612_3() {
34 char *x = (char*)malloc(12);
35 const_ptr_and_callback_def_param(0, x, 12);
36 }
37
38 int const_ptr_and_callback_def_param_null(int, const char*, int n, void(*)(void*) = 0);
r11160612_no_callback()39 void r11160612_no_callback() {
40 char *x = (char*)malloc(12);
41 const_ptr_and_callback_def_param_null(0, x, 12);
42 } // expected-warning{{leak}}
43
44 // Test member function pointer.
45 struct CanFreeMemory {
46 static void myFree(void*);
47 };
48 //This is handled because we look at the type of the parameter(not argument).
r11160612_3(CanFreeMemory * p)49 void r11160612_3(CanFreeMemory* p) {
50 char *x = (char*)malloc(12);
51 const_ptr_and_callback_def_param(0, x, 12, p->myFree);
52 }
53
54
55 namespace PR13751 {
56 class OwningVector {
57 void **storage;
58 size_t length;
59 public:
60 OwningVector();
61 ~OwningVector();
push_back(void * Item)62 void push_back(void *Item) {
63 storage[length++] = Item;
64 }
65 };
66
testDestructors()67 void testDestructors() {
68 OwningVector v;
69 v.push_back(malloc(4));
70 // no leak warning; freed in destructor
71 }
72 }
73
74 struct X { void *a; };
75
get()76 struct X get() {
77 struct X result;
78 result.a = malloc(4);
79 return result; // no-warning
80 }
81
82 // Ensure that regions accessible through a LazyCompoundVal trigger region escape.
83 // Malloc checker used to report leaks for the following two test cases.
84 struct Property {
85 char* getterName;
PropertyProperty86 Property(char* n)
87 : getterName(n) {}
88
89 };
90 void append(Property x);
91
appendWrapper(char * getterName)92 void appendWrapper(char *getterName) {
93 append(Property(getterName));
94 }
foo(const char * name)95 void foo(const char* name) {
96 char* getterName = strdup(name);
97 appendWrapper(getterName); // no-warning
98 }
99
100 struct NestedProperty {
101 Property prop;
NestedPropertyNestedProperty102 NestedProperty(Property p)
103 : prop(p) {}
104 };
105 void appendNested(NestedProperty x);
106
appendWrapperNested(char * getterName)107 void appendWrapperNested(char *getterName) {
108 appendNested(NestedProperty(Property(getterName)));
109 }
fooNested(const char * name)110 void fooNested(const char* name) {
111 char* getterName = strdup(name);
112 appendWrapperNested(getterName); // no-warning
113 }
114
115 namespace PR31226 {
116 struct b2 {
117 int f;
118 };
119
120 struct b1 : virtual b2 {
121 void m();
122 };
123
124 struct d : b1, b2 {
125 };
126
f()127 void f() {
128 d *p = new d();
129 p->m(); // no-crash // no-warning
130 }
131 }
132
133 // Allow __cxa_demangle to escape.
test_cxa_demangle(const char * sym)134 char* test_cxa_demangle(const char* sym) {
135 size_t funcnamesize = 256;
136 char* funcname = (char*)malloc(funcnamesize);
137 int status;
138 char* ret = abi::__cxa_demangle(sym, funcname, &funcnamesize, &status);
139 if (status == 0) {
140 funcname = ret;
141 }
142 return funcname; // no-warning
143 }
144
145 namespace argument_leak {
146 class A {
147 char *name;
148
149 public:
getName()150 char *getName() {
151 if (!name) {
152 name = static_cast<char *>(malloc(10));
153 }
154 return name;
155 }
~A()156 ~A() {
157 if (name) {
158 delete[] name;
159 }
160 }
161 };
162
test(A a)163 void test(A a) {
164 (void)a.getName();
165 }
166 } // namespace argument_leak
167