[RISCV][FMV] Support target_clones (#85786)
[llvm-project.git] / clang / test / Analysis / malloc.cpp
blob7af1b59e04a5a24a8cd1ff5abf0204611b27e28f
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;
42 void *malloc(size_t);
43 void free(void *);
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, ... );
49 void taintAlloc() {
50 size_t size = 0;
51 scanf("%zu", &size);
52 int *ptr = new int[size];// expected-warning{{Memory allocation function is called with a tainted (potentially attacker controlled) value}}
53 delete[] ptr;
56 void checkThatMallocCheckerIsRunning() {
57 malloc(4);
58 } // expected-warning{{leak}}
60 struct Foo {
61 mutable void* m_data;
62 Foo(void* data) : m_data(data) {}
64 Foo aFunction() {
65 return malloc(10);
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
70 // allocated memory.
71 // Test default parameter.
72 int const_ptr_and_callback_def_param(int, const char*, int n, void(*)(void*) = free);
73 void r11160612_3() {
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);
95 namespace PR13751 {
96 class OwningVector {
97 void **storage;
98 size_t length;
99 public:
100 OwningVector();
101 ~OwningVector();
102 void push_back(void *Item) {
103 storage[length++] = Item;
107 void testDestructors() {
108 OwningVector v;
109 v.push_back(malloc(4));
110 // no leak warning; freed in destructor
114 struct X { void *a; };
116 struct X get() {
117 struct X result;
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.
124 struct Property {
125 char* getterName;
126 Property(char* n)
127 : getterName(n) {}
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 {
141 Property prop;
142 NestedProperty(Property p)
143 : prop(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
155 namespace PR31226 {
156 struct b2 {
157 int f;
160 struct b1 : virtual b2 {
161 void m();
164 struct d : b1, b2 {
167 void f() {
168 d *p = new d();
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);
177 int status;
178 char* ret = abi::__cxa_demangle(sym, funcname, &funcnamesize, &status);
179 if (status == 0) {
180 funcname = ret;
182 return funcname; // no-warning
185 namespace argument_leak {
186 class A {
187 char *name;
189 public:
190 char *getName() {
191 if (!name) {
192 name = static_cast<char *>(malloc(10));
194 return name;
196 ~A() {
197 if (name) {
198 delete[] name;
203 void test(A a) {
204 (void)a.getName();
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 {
217 class a {
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
234 namespace pr81597 {
235 struct S {};
236 struct T {
237 void free(const S& s);
239 void f(T& t) {
240 S s;
241 t.free(s); // no-warning: This is not the free you are looking for...
243 } // namespace pr81597