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