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 | %normalize_plist | diff -ub %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(void) {
21 A
= malloc(2*sizeof(int*));
23 }//expected-warning{{Potential leak}}
25 void reallocDiagnostics(void) {
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.
44 void test_wrapper(void) {
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(void) {
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);
75 void reallocIntra(void) {
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(void) {
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 void myfree_takingblock(void (^ignored
)(void), int *p
) {
96 void call_myfree_takingblock(void) {
97 void (^some_block
)(void) = ^void(void) { };
99 int *p
= malloc(sizeof(int));
100 myfree_takingblock(some_block
, p
);
101 *p
= 3;//expected-warning{{Use of memory after it is freed}}
104 // Test that we refer to the last symbol used in the leak diagnostic.
105 void LeakedSymbol(int in
) {
108 p
= (int*)malloc(12);
114 in
++;//expected-warning{{Potential leak}}
117 // Tests that exercise running remove dead bindings at Call exit.
118 static void function_with_leak1(void) {
119 char *x
= (char*)malloc(12);
120 } //expected-warning{{Potential leak}}
121 void use_function_with_leak1(void) {
122 function_with_leak1();
126 static void function_with_leak2(void) {
127 char *x
= (char*)malloc(12);
128 int m
= 0; //expected-warning{{Potential leak}}
130 void use_function_with_leak2(void) {
131 function_with_leak2();
134 static void function_with_leak3(int y
) {
135 char *x
= (char*)malloc(12);
138 }//expected-warning{{Potential leak}}
139 void use_function_with_leak3(int y
) {
140 function_with_leak3(y
);
143 static void function_with_leak4(int y
) {
144 char *x
= (char*)malloc(12);
148 y
--;//expected-warning{{Potential leak}}
150 void use_function_with_leak4(int y
) {
151 function_with_leak4(y
);
154 int anotherFunction5(void) {
157 static int function_with_leak5(void) {
158 char *x
= (char*)malloc(12);
159 return anotherFunction5();//expected-warning{{Potential leak}}
161 void use_function_with_leak5(void) {
162 function_with_leak5();
165 void anotherFunction6(int m
) {
168 static void function_with_leak6(void) {
169 char *x
= (char*)malloc(12);
170 anotherFunction6(3);//expected-warning{{Potential leak}}
172 void use_function_with_leak6(void) {
173 function_with_leak6();
176 static void empty_function(void){
178 void use_empty_function(void) {
181 static char *function_with_leak7(void) {
182 return (char*)malloc(12);
184 void use_function_with_leak7(void) {
185 function_with_leak7();
186 }//expected-warning{{Potential memory leak}}
188 // Test that we do not print the name of a variable not visible from where
189 // the issue is reported.
190 int *my_malloc(void) {
194 void testOnlyRefferToVisibleVariables(void) {
196 } // expected-warning{{Potential memory leak}}
198 struct PointerWrapper
{
201 int *my_malloc_into_struct(void) {
202 struct PointerWrapper w
;
206 void testMyMalloc(void) {
207 my_malloc_into_struct();
208 } // expected-warning{{Potential memory leak}}