Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / Analysis / malloc-plist.c
blob6a3ba5b5f25501237cc0d5e3d2771351d6d02cf4
1 // RUN: rm -f %t
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;
6 void *malloc(size_t);
7 void free(void *);
8 void *realloc(void *ptr, size_t size);
10 void diagnosticTest(int in) {
11 if (in > 5) {
12 int *p = malloc(12);
13 *p = 0;
14 (*p)++;
16 in++; // expected-warning {{leak}}
19 void myArrayAllocation(void) {
20 int **A;
21 A = malloc(2*sizeof(int*));
22 A[0] = 0;
23 }//expected-warning{{Potential leak}}
25 void reallocDiagnostics(void) {
26 char * buf = malloc(100);
27 char * tmp;
28 tmp = (char*)realloc(buf, 0x1000000);
29 if (!tmp) {
30 return;// expected-warning {{leak}}
32 buf = tmp;
33 free(buf);
36 void *wrapper(void) {
37 void *x = malloc(100);
38 // This is intentionally done to test diagnostic emission.
39 if (x)
40 return x;
41 return 0;
44 void test_wrapper(void) {
45 void *buf = wrapper();
46 (void) buf;
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) {
52 free(x);
54 void my_malloc_and_free(void **x) {
55 *x = malloc(100);
56 if (*x)
57 my_free(*x);
58 return;
60 void *test_double_action_call(void) {
61 void *buf;
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) {
68 char *tmp;
69 tmp = (char*)realloc(buf, 0x1000000);
70 if (!tmp) {
71 return tmp;
73 return tmp;
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);
85 void use_ret(void) {
86 char *v;
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) {
93 free(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) {
106 int *m = 0;
107 int *p;
108 p = (int*)malloc(12);
109 *p = 0;
110 (*p)++;
111 m = p;
112 p = 0;
113 (*m)++;
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();
123 int y = 0;
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);
136 if (y)
137 y++;
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);
145 if (y)
146 y++;
147 else
148 y--;//expected-warning{{Potential leak}}
150 void use_function_with_leak4(int y) {
151 function_with_leak4(y);
154 int anotherFunction5(void) {
155 return 5;
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) {
166 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) {
179 empty_function();
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) {
191 int *p = malloc(12);
192 return p;
194 void testOnlyRefferToVisibleVariables(void) {
195 my_malloc();
196 } // expected-warning{{Potential memory leak}}
198 struct PointerWrapper{
199 int*p;
201 int *my_malloc_into_struct(void) {
202 struct PointerWrapper w;
203 w.p = malloc(12);
204 return w.p;
206 void testMyMalloc(void) {
207 my_malloc_into_struct();
208 } // expected-warning{{Potential memory leak}}