blob: f24ccf58dc3b4bfaaa199f1669edab120be2cc71 [file] [log] [blame]
Artem Dergachevd69e0122016-12-07 16:12:26 +00001// 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
Anna Zaks7e3e06a2012-03-29 23:26:54 +00003
4typedef __typeof(sizeof(int)) size_t;
5void *malloc(size_t);
6void free(void *);
7void *realloc(void *ptr, size_t size);
8void *calloc(size_t nmemb, size_t size);
Anna Zaks60bf5f42013-04-02 01:28:24 +00009char *strdup(const char *s);
Jordan Rose6d671cc2012-09-05 22:55:23 +000010
11void checkThatMallocCheckerIsRunning() {
Jordan Rosee37ab502012-11-15 19:11:43 +000012 malloc(4);
13} // expected-warning{{leak}}
Jordan Rose6d671cc2012-09-05 22:55:23 +000014
Anna Zaks7e3e06a2012-03-29 23:26:54 +000015// Test for radar://11110132.
16struct Foo {
17 mutable void* m_data;
18 Foo(void* data) : m_data(data) {}
19};
20Foo aFunction() {
21 return malloc(10);
22}
Anna Zaks228f9c72012-05-03 23:50:28 +000023
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.
Jordan Rose89bbd1f2013-05-01 23:10:44 +000028int const_ptr_and_callback_def_param(int, const char*, int n, void(*)(void*) = free);
Anna Zaks228f9c72012-05-03 23:50:28 +000029void r11160612_3() {
30 char *x = (char*)malloc(12);
31 const_ptr_and_callback_def_param(0, x, 12);
32}
Anna Zaks75776862012-05-04 22:18:36 +000033
Jordan Rose89bbd1f2013-05-01 23:10:44 +000034int const_ptr_and_callback_def_param_null(int, const char*, int n, void(*)(void*) = 0);
35void 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
Anna Zaks75776862012-05-04 22:18:36 +000040// Test member function pointer.
41struct CanFreeMemory {
42 static void myFree(void*);
43};
44//This is handled because we look at the type of the parameter(not argument).
45void r11160612_3(CanFreeMemory* p) {
46 char *x = (char*)malloc(12);
47 const_ptr_and_callback_def_param(0, x, 12, p->myFree);
48}
49
Jordan Rose6d671cc2012-09-05 22:55:23 +000050
51namespace 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
Anna Zaks258f9352013-02-06 00:01:14 +000070struct X { void *a; };
71
72struct X get() {
73 struct X result;
74 result.a = malloc(4);
75 return result; // no-warning
76}
Anna Zaks60bf5f42013-04-02 01:28:24 +000077
78// Ensure that regions accessible through a LazyCompoundVal trigger region escape.
79// Malloc checker used to report leaks for the following two test cases.
80struct Property {
81 char* getterName;
82 Property(char* n)
83 : getterName(n) {}
84
85};
86void append(Property x);
87
88void appendWrapper(char *getterName) {
89 append(Property(getterName));
90}
91void foo(const char* name) {
92 char* getterName = strdup(name);
93 appendWrapper(getterName); // no-warning
94}
95
96struct NestedProperty {
97 Property prop;
98 NestedProperty(Property p)
99 : prop(p) {}
100};
101void appendNested(NestedProperty x);
102
103void appendWrapperNested(char *getterName) {
104 appendNested(NestedProperty(Property(getterName)));
105}
106void fooNested(const char* name) {
107 char* getterName = strdup(name);
108 appendWrapperNested(getterName); // no-warning
Artem Dergachevd69e0122016-12-07 16:12:26 +0000109}
110
111namespace 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}