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 \
7 // RUN: -analyzer-checker=alpha.security.taint.TaintPropagation \
8 // RUN: -analyzer-checker=optin.taint.TaintedAlloc
10 // RUN: %clang_analyze_cc1 -w -verify %s \
11 // RUN: -triple i386-unknown-linux-gnu \
12 // RUN: -analyzer-checker=core \
13 // RUN: -analyzer-checker=alpha.deadcode.UnreachableCode \
14 // RUN: -analyzer-checker=alpha.core.CastSize \
15 // RUN: -analyzer-checker=unix.Malloc \
16 // RUN: -analyzer-checker=cplusplus.NewDelete \
17 // RUN: -analyzer-checker=alpha.security.taint.TaintPropagation \
18 // RUN: -analyzer-checker=optin.taint.TaintedAlloc
20 // RUN: %clang_analyze_cc1 -w -verify %s -DTEST_INLINABLE_ALLOCATORS \
21 // RUN: -analyzer-checker=core \
22 // RUN: -analyzer-checker=alpha.deadcode.UnreachableCode \
23 // RUN: -analyzer-checker=alpha.core.CastSize \
24 // RUN: -analyzer-checker=unix.Malloc \
25 // RUN: -analyzer-checker=cplusplus.NewDelete \
26 // RUN: -analyzer-checker=alpha.security.taint.TaintPropagation \
27 // RUN: -analyzer-checker=optin.taint.TaintedAlloc
29 // RUN: %clang_analyze_cc1 -w -verify %s -DTEST_INLINABLE_ALLOCATORS \
30 // RUN: -triple i386-unknown-linux-gnu \
31 // RUN: -analyzer-checker=core \
32 // RUN: -analyzer-checker=alpha.deadcode.UnreachableCode \
33 // RUN: -analyzer-checker=alpha.core.CastSize \
34 // RUN: -analyzer-checker=unix.Malloc \
35 // RUN: -analyzer-checker=cplusplus.NewDelete \
36 // RUN: -analyzer-checker=alpha.security.taint.TaintPropagation \
37 // RUN: -analyzer-checker=optin.taint.TaintedAlloc
39 #include "Inputs/system-header-simulator-cxx.h"
41 typedef __typeof(sizeof(int)) size_t;
44 void *realloc(void *ptr
, size_t size
);
45 void *calloc(size_t nmemb
, size_t size
);
46 char *strdup(const char *s
);
47 int scanf( const char* format
, ... );
52 int *ptr
= new int[size
];// expected-warning{{Memory allocation function is called with a tainted (potentially attacker controlled) value}}
56 void checkThatMallocCheckerIsRunning() {
58 } // expected-warning{{leak}}
62 Foo(void* data
) : m_data(data
) {}
68 // Assume that functions which take a function pointer can free memory even if
69 // they are defined in system headers and take the const pointer to the
71 // Test default parameter.
72 int const_ptr_and_callback_def_param(int, const char*, int n
, void(*)(void*) = free
);
74 char *x
= (char*)malloc(12);
75 const_ptr_and_callback_def_param(0, x
, 12);
78 int const_ptr_and_callback_def_param_null(int, const char*, int n
, void(*)(void*) = 0);
79 void r11160612_no_callback() {
80 char *x
= (char*)malloc(12);
81 const_ptr_and_callback_def_param_null(0, x
, 12);
82 } // expected-warning{{leak}}
84 // Test member function pointer.
85 struct CanFreeMemory
{
86 static void myFree(void*);
88 //This is handled because we look at the type of the parameter(not argument).
89 void r11160612_3(CanFreeMemory
* p
) {
90 char *x
= (char*)malloc(12);
91 const_ptr_and_callback_def_param(0, x
, 12, p
->myFree
);
102 void push_back(void *Item
) {
103 storage
[length
++] = Item
;
107 void testDestructors() {
109 v
.push_back(malloc(4));
110 // no leak warning; freed in destructor
114 struct X
{ void *a
; };
118 result
.a
= malloc(4);
119 return result
; // no-warning
122 // Ensure that regions accessible through a LazyCompoundVal trigger region escape.
123 // Malloc checker used to report leaks for the following two test cases.
130 void append(Property x
);
132 void appendWrapper(char *getterName
) {
133 append(Property(getterName
));
135 void foo(const char* name
) {
136 char* getterName
= strdup(name
);
137 appendWrapper(getterName
); // no-warning
140 struct NestedProperty
{
142 NestedProperty(Property p
)
145 void appendNested(NestedProperty x
);
147 void appendWrapperNested(char *getterName
) {
148 appendNested(NestedProperty(Property(getterName
)));
150 void fooNested(const char* name
) {
151 char* getterName
= strdup(name
);
152 appendWrapperNested(getterName
); // no-warning
160 struct b1
: virtual b2
{
169 p
->m(); // no-crash // no-warning
173 // Allow __cxa_demangle to escape.
174 char* test_cxa_demangle(const char* sym
) {
175 size_t funcnamesize
= 256;
176 char* funcname
= (char*)malloc(funcnamesize
);
178 char* ret
= abi::__cxa_demangle(sym
, funcname
, &funcnamesize
, &status
);
182 return funcname
; // no-warning
185 namespace argument_leak
{
192 name
= static_cast<char *>(malloc(10));
206 } // namespace argument_leak
208 #define ZERO_SIZE_PTR ((void *)16)
210 void test_delete_ZERO_SIZE_PTR() {
211 int *Ptr
= (int *)ZERO_SIZE_PTR
;
212 // ZERO_SIZE_PTR is specially handled but only for malloc family
213 delete Ptr
; // expected-warning{{Argument to 'delete' is a constant address (16)}}
216 namespace pr46253_class
{
218 void *realloc(int, bool = false) { realloc(1); } // no-crash
220 } // namespace pr46253_class
222 namespace pr46253_retty
{
223 void realloc(void *ptr
, size_t size
) { realloc(ptr
, size
); } // no-crash
224 } // namespace pr46253_retty
226 namespace pr46253_paramty
{
227 void *realloc(void **ptr
, size_t size
) { realloc(ptr
, size
); } // no-crash
228 } // namespace pr46253_paramty
230 namespace pr46253_paramty2
{
231 void *realloc(void *ptr
, int size
) { realloc(ptr
, size
); } // no-crash
232 } // namespace pr46253_paramty2
237 void free(const S
& s
);
241 t
.free(s
); // no-warning: This is not the free you are looking for...
243 } // namespace pr81597