xref: /llvm-project/clang/test/Analysis/malloc-plist.c (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
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 -
4 
5 typedef __typeof(sizeof(int)) size_t;
6 void *malloc(size_t);
7 void free(void *);
8 void *realloc(void *ptr, size_t size);
9 
diagnosticTest(int in)10 void diagnosticTest(int in) {
11     if (in > 5) {
12         int *p = malloc(12);
13         *p = 0;
14         (*p)++;
15     }
16     in++; // expected-warning {{leak}}
17 }
18 
myArrayAllocation(void)19 void myArrayAllocation(void) {
20     int **A;
21     A = malloc(2*sizeof(int*));
22     A[0] = 0;
23 }//expected-warning{{Potential leak}}
24 
reallocDiagnostics(void)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}}
31     }
32     buf = tmp;
33     free(buf);
34 }
35 
wrapper(void)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;
42 }
43 
test_wrapper(void)44 void test_wrapper(void) {
45   void *buf = wrapper();
46   (void) buf;
47 }//expected-warning{{Potential leak}}
48 
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.
my_free(void * x)51 void my_free(void *x) {
52     free(x);
53 }
my_malloc_and_free(void ** x)54 void my_malloc_and_free(void **x) {
55     *x = malloc(100);
56     if (*x)
57       my_free(*x);
58     return;
59 }
test_double_action_call(void)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}}
64 }
65 
66 // Test stack hint for 'reallocation failed'.
my_realloc(char * buf)67 char *my_realloc(char *buf) {
68     char *tmp;
69     tmp = (char*)realloc(buf, 0x1000000);
70     if (!tmp) {
71         return tmp;
72     }
73     return tmp;
74 }
reallocIntra(void)75 void reallocIntra(void) {
76     char *buf = (char *)malloc(100);
77     buf = my_realloc(buf);
78     free(buf);//expected-warning{{Potential leak}}
79 }
80 
81 // Test stack hint when returning a result.
malloc_wrapper_ret(void)82 static char *malloc_wrapper_ret(void) {
83     return (char*)malloc(12);
84 }
use_ret(void)85 void use_ret(void) {
86     char *v;
87     v = malloc_wrapper_ret();
88 }//expected-warning{{Potential leak}}
89 
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);
94 }
95 
call_myfree_takingblock(void)96 void call_myfree_takingblock(void) {
97   void (^some_block)(void) = ^void(void) { };
98 
99   int *p = malloc(sizeof(int));
100   myfree_takingblock(some_block, p);
101   *p = 3;//expected-warning{{Use of memory after it is freed}}
102 }
103 
104 // Test that we refer to the last symbol used in the leak diagnostic.
LeakedSymbol(int in)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}}
115 }
116 
117 // Tests that exercise running remove dead bindings at Call exit.
function_with_leak1(void)118 static void function_with_leak1(void) {
119     char *x = (char*)malloc(12);
120 } //expected-warning{{Potential leak}}
use_function_with_leak1(void)121 void use_function_with_leak1(void) {
122     function_with_leak1();
123     int y = 0;
124 }
125 
function_with_leak2(void)126 static void function_with_leak2(void) {
127     char *x = (char*)malloc(12);
128     int m = 0; //expected-warning{{Potential leak}}
129 }
use_function_with_leak2(void)130 void use_function_with_leak2(void) {
131     function_with_leak2();
132 }
133 
function_with_leak3(int y)134 static void function_with_leak3(int y) {
135     char *x = (char*)malloc(12);
136     if (y)
137         y++;
138 }//expected-warning{{Potential leak}}
use_function_with_leak3(int y)139 void use_function_with_leak3(int y) {
140     function_with_leak3(y);
141 }
142 
function_with_leak4(int 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}}
149 }
use_function_with_leak4(int y)150 void use_function_with_leak4(int y) {
151     function_with_leak4(y);
152 }
153 
anotherFunction5(void)154 int anotherFunction5(void) {
155     return 5;
156 }
function_with_leak5(void)157 static int function_with_leak5(void) {
158     char *x = (char*)malloc(12);
159     return anotherFunction5();//expected-warning{{Potential leak}}
160 }
use_function_with_leak5(void)161 void use_function_with_leak5(void) {
162     function_with_leak5();
163 }
164 
anotherFunction6(int m)165 void anotherFunction6(int m) {
166     m++;
167 }
function_with_leak6(void)168 static void function_with_leak6(void) {
169     char *x = (char*)malloc(12);
170     anotherFunction6(3);//expected-warning{{Potential leak}}
171 }
use_function_with_leak6(void)172 void use_function_with_leak6(void) {
173     function_with_leak6();
174 }
175 
empty_function(void)176 static void empty_function(void){
177 }
use_empty_function(void)178 void use_empty_function(void) {
179     empty_function();
180 }
function_with_leak7(void)181 static char *function_with_leak7(void) {
182     return (char*)malloc(12);
183 }
use_function_with_leak7(void)184 void use_function_with_leak7(void) {
185     function_with_leak7();
186 }//expected-warning{{Potential memory leak}}
187 
188 // Test that we do not print the name of a variable not visible from where
189 // the issue is reported.
my_malloc(void)190 int *my_malloc(void) {
191   int *p = malloc(12);
192   return p;
193 }
testOnlyRefferToVisibleVariables(void)194 void testOnlyRefferToVisibleVariables(void) {
195   my_malloc();
196 } // expected-warning{{Potential memory leak}}
197 
198 struct PointerWrapper{
199   int*p;
200 };
my_malloc_into_struct(void)201 int *my_malloc_into_struct(void) {
202   struct PointerWrapper w;
203   w.p = malloc(12);
204   return w.p;
205 }
testMyMalloc(void)206 void testMyMalloc(void) {
207   my_malloc_into_struct();
208 } // expected-warning{{Potential memory leak}}
209