2 // RUN: %clang_analyze_cc1 -fblocks -analyzer-checker=core,unix.Malloc -analyzer-output=plist -verify -o %t -analyzer-config eagerly-assume=false %s
3 // RUN: tail -n +11 %t | diff -u -w -I "<string>/" -I "<string>.:" -I "version" - %S/Inputs/expected-plists/malloc-plist.c.plist
5 typedef __typeof(sizeof(int)) size_t;
8 void *realloc(void *ptr, size_t size);
10 void diagnosticTest(int in) {
16 in++; // expected-warning {{leak}}
19 void myArrayAllocation() {
21 A = malloc(2*sizeof(int*));
23 }//expected-warning{{Potential leak}}
25 void reallocDiagnostics() {
26 char * buf = malloc(100);
28 tmp = (char*)realloc(buf, 0x1000000);
30 return;// expected-warning {{leak}}
37 void *x = malloc(100);
38 // This is intentionally done to test diagnostic emission.
45 void *buf = wrapper();
47 }//expected-warning{{Potential leak}}
49 // Test what happens when the same call frees and allocated memory.
50 // Also tests the stack hint for parameters, when they are passed directly or via pointer.
51 void my_free(void *x) {
54 void my_malloc_and_free(void **x) {
60 void *test_double_action_call() {
62 my_malloc_and_free(&buf);
63 return buf; //expected-warning{{Use of memory after it is freed}}
66 // Test stack hint for 'reallocation failed'.
67 char *my_realloc(char *buf) {
69 tmp = (char*)realloc(buf, 0x1000000);
76 char *buf = (char *)malloc(100);
77 buf = my_realloc(buf);
78 free(buf);//expected-warning{{Potential leak}}
81 // Test stack hint when returning a result.
82 static char *malloc_wrapper_ret() {
83 return (char*)malloc(12);
87 v = malloc_wrapper_ret();
88 }//expected-warning{{Potential leak}}
90 // Passing a block as a parameter to an inlined call for which we generate
91 // a stack hint message caused crashes.
92 // rdar://problem/21291971
93 void myfree_takingblock(void (^ignored)(void), int *p) {
97 void call_myfree_takingblock() {
98 void (^some_block)(void) = ^void(void) { };
100 int *p = malloc(sizeof(int));
101 myfree_takingblock(some_block, p);
102 *p = 3;//expected-warning{{Use of memory after it is freed}}
105 // Test that we refer to the last symbol used in the leak diagnostic.
106 void LeakedSymbol(int in) {
109 p = (int*)malloc(12);
115 in++;//expected-warning{{Potential leak}}
118 // Tests that exercise running remove dead bindings at Call exit.
119 static void function_with_leak1() {
120 char *x = (char*)malloc(12);
121 } //expected-warning{{Potential leak}}
122 void use_function_with_leak1() {
123 function_with_leak1();
127 static void function_with_leak2() {
128 char *x = (char*)malloc(12);
129 int m = 0; //expected-warning{{Potential leak}}
131 void use_function_with_leak2() {
132 function_with_leak2();
135 static void function_with_leak3(int y) {
136 char *x = (char*)malloc(12);
139 }//expected-warning{{Potential leak}}
140 void use_function_with_leak3(int y) {
141 function_with_leak3(y);
144 static void function_with_leak4(int y) {
145 char *x = (char*)malloc(12);
149 y--;//expected-warning{{Potential leak}}
151 void use_function_with_leak4(int y) {
152 function_with_leak4(y);
155 int anotherFunction5() {
158 static int function_with_leak5() {
159 char *x = (char*)malloc(12);
160 return anotherFunction5();//expected-warning{{Potential leak}}
162 void use_function_with_leak5() {
163 function_with_leak5();
166 void anotherFunction6(int m) {
169 static void function_with_leak6() {
170 char *x = (char*)malloc(12);
171 anotherFunction6(3);//expected-warning{{Potential leak}}
173 void use_function_with_leak6() {
174 function_with_leak6();
177 static void empty_function(){
179 void use_empty_function() {
182 static char *function_with_leak7() {
183 return (char*)malloc(12);
185 void use_function_with_leak7() {
186 function_with_leak7();
187 }//expected-warning{{Potential memory leak}}
189 // Test that we do not print the name of a variable not visible from where
190 // the issue is reported.
195 void testOnlyRefferToVisibleVariables() {
197 } // expected-warning{{Potential memory leak}}
199 struct PointerWrapper{
202 int *my_malloc_into_struct() {
203 struct PointerWrapper w;
207 void testMyMalloc() {
208 my_malloc_into_struct();
209 } // expected-warning{{Potential memory leak}}