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