blob: 89299e6637d55b4b518baf83fcd28a1023b5951a [file] [log] [blame]
Ted Kremenek3f955e62011-08-03 23:14:55 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=core,experimental.deadcode.UnreachableCode,experimental.core.CastSize,experimental.unix.Malloc -analyzer-store=region -verify %s
Eli Friedmanb7746852009-11-14 04:23:25 +00002typedef __typeof(sizeof(int)) size_t;
Ted Kremenek9430bf22009-11-13 20:03:22 +00003void *malloc(size_t);
4void free(void *);
Zhongxing Xuc0484fa2009-12-12 12:29:38 +00005void *realloc(void *ptr, size_t size);
6void *calloc(size_t nmemb, size_t size);
Ted Kremenekd21139a2010-07-31 01:52:11 +00007
Anna Zaksa1b227b2012-02-08 23:16:56 +00008void myfoo(int *p);
9void myfooint(int p);
Zhongxing Xuc7460962009-11-13 07:48:11 +000010
11void f1() {
Zhongxing Xu658dd8b2010-05-25 04:59:19 +000012 int *p = malloc(12);
Zhongxing Xuc7460962009-11-13 07:48:11 +000013 return; // expected-warning{{Allocated memory never released. Potential memory leak.}}
14}
15
Zhongxing Xuc7460962009-11-13 07:48:11 +000016void f2() {
Zhongxing Xu658dd8b2010-05-25 04:59:19 +000017 int *p = malloc(12);
Zhongxing Xuc7460962009-11-13 07:48:11 +000018 free(p);
19 free(p); // expected-warning{{Try to free a memory block that has been released}}
20}
Ted Kremeneke5e977012009-11-13 20:00:28 +000021
Lenny Maiorani005b5c12011-04-27 14:49:29 +000022void f2_realloc_0() {
23 int *p = malloc(12);
24 realloc(p,0);
25 realloc(p,0); // expected-warning{{Try to free a memory block that has been released}}
26}
27
28void f2_realloc_1() {
29 int *p = malloc(12);
Zhongxing Xubfb8e2f2011-09-01 04:53:59 +000030 int *q = realloc(p,0); // no-warning
Lenny Maiorani005b5c12011-04-27 14:49:29 +000031}
32
Zhongxing Xu4668c7e2009-11-17 07:54:15 +000033// This case tests that storing malloc'ed memory to a static variable which is
34// then returned is not leaked. In the absence of known contracts for functions
35// or inter-procedural analysis, this is a conservative answer.
Ted Kremeneke5e977012009-11-13 20:00:28 +000036int *f3() {
37 static int *p = 0;
Zhongxing Xu658dd8b2010-05-25 04:59:19 +000038 p = malloc(12);
Zhongxing Xu23baa012009-11-17 08:58:18 +000039 return p; // no-warning
Ted Kremeneke5e977012009-11-13 20:00:28 +000040}
41
Zhongxing Xu4668c7e2009-11-17 07:54:15 +000042// This case tests that storing malloc'ed memory to a static global variable
43// which is then returned is not leaked. In the absence of known contracts for
44// functions or inter-procedural analysis, this is a conservative answer.
Ted Kremeneke5e977012009-11-13 20:00:28 +000045static int *p_f4 = 0;
46int *f4() {
Zhongxing Xu658dd8b2010-05-25 04:59:19 +000047 p_f4 = malloc(12);
Zhongxing Xu23baa012009-11-17 08:58:18 +000048 return p_f4; // no-warning
Ted Kremeneke5e977012009-11-13 20:00:28 +000049}
Zhongxing Xuc0484fa2009-12-12 12:29:38 +000050
51int *f5() {
Zhongxing Xu658dd8b2010-05-25 04:59:19 +000052 int *q = malloc(12);
Zhongxing Xuc0484fa2009-12-12 12:29:38 +000053 q = realloc(q, 20);
54 return q; // no-warning
55}
Zhongxing Xub0e15df2009-12-31 06:13:07 +000056
57void f6() {
Zhongxing Xu658dd8b2010-05-25 04:59:19 +000058 int *p = malloc(12);
Zhongxing Xub0e15df2009-12-31 06:13:07 +000059 if (!p)
60 return; // no-warning
61 else
62 free(p);
63}
Zhongxing Xu5fcd99b2010-01-18 04:01:40 +000064
Lenny Maiorani005b5c12011-04-27 14:49:29 +000065void f6_realloc() {
66 int *p = malloc(12);
67 if (!p)
68 return; // no-warning
69 else
70 realloc(p,0);
71}
72
73
Zhongxing Xu5fcd99b2010-01-18 04:01:40 +000074char *doit2();
75void pr6069() {
76 char *buf = doit2();
77 free(buf);
78}
Zhongxing Xube36ecb2010-02-14 06:49:48 +000079
80void pr6293() {
81 free(0);
82}
Zhongxing Xu1bb6a1a2010-03-10 04:58:55 +000083
84void f7() {
85 char *x = (char*) malloc(4);
86 free(x);
Anna Zaks31886862012-02-10 01:11:00 +000087 x[0] = 'a'; // expected-warning{{Use of dynamically allocated memory after it is freed.}}
Zhongxing Xu1bb6a1a2010-03-10 04:58:55 +000088}
Zhongxing Xu658dd8b2010-05-25 04:59:19 +000089
Lenny Maiorani005b5c12011-04-27 14:49:29 +000090void f7_realloc() {
91 char *x = (char*) malloc(4);
92 realloc(x,0);
Anna Zaks31886862012-02-10 01:11:00 +000093 x[0] = 'a'; // expected-warning{{Use of dynamically allocated memory after it is freed.}}
Lenny Maiorani005b5c12011-04-27 14:49:29 +000094}
95
Zhongxing Xu658dd8b2010-05-25 04:59:19 +000096void PR6123() {
97 int *x = malloc(11); // expected-warning{{Cast a region whose size is not a multiple of the destination type size.}}
98}
99
100void PR7217() {
101 int *buf = malloc(2); // expected-warning{{Cast a region whose size is not a multiple of the destination type size.}}
102 buf[1] = 'c'; // not crash
Zhongxing Xu658dd8b2010-05-25 04:59:19 +0000103}
Jordy Rose2dd9b022010-06-20 04:30:57 +0000104
105void mallocCastToVoid() {
106 void *p = malloc(2);
107 const void *cp = p; // not crash
108 free(p);
109}
110
111void mallocCastToFP() {
112 void *p = malloc(2);
113 void (*fp)() = p; // not crash
114 free(p);
115}
116
Zhongxing Xu527ff6d2010-06-01 03:01:33 +0000117// This tests that malloc() buffers are undefined by default
118char mallocGarbage () {
119 char *buf = malloc(2);
120 char result = buf[1]; // expected-warning{{undefined}}
121 free(buf);
122 return result;
123}
124
125// This tests that calloc() buffers need to be freed
126void callocNoFree () {
127 char *buf = calloc(2,2);
128 return; // expected-warning{{never released}}
129}
130
131// These test that calloc() buffers are zeroed by default
132char callocZeroesGood () {
133 char *buf = calloc(2,2);
134 char result = buf[3]; // no-warning
135 if (buf[1] == 0) {
136 free(buf);
137 }
138 return result; // no-warning
139}
140
141char callocZeroesBad () {
142 char *buf = calloc(2,2);
143 char result = buf[3]; // no-warning
144 if (buf[1] != 0) {
Tom Carecba9f512010-07-23 23:04:53 +0000145 free(buf); // expected-warning{{never executed}}
Zhongxing Xu527ff6d2010-06-01 03:01:33 +0000146 }
147 return result; // expected-warning{{never released}}
148}
Anna Zaksa1b227b2012-02-08 23:16:56 +0000149
150void nullFree() {
151 int *p = 0;
152 free(p); // no warning - a nop
153}
154
155void paramFree(int *p) {
156 myfoo(p);
157 free(p); // no warning
158 myfoo(p); // TODO: This should be a warning.
159}
160
161int* mallocEscapeRet() {
162 int *p = malloc(12);
163 return p; // no warning
164}
165
166void mallocEscapeFoo() {
167 int *p = malloc(12);
168 myfoo(p);
169 return; // no warning
170}
171
172void mallocEscapeFree() {
173 int *p = malloc(12);
174 myfoo(p);
175 free(p);
176}
177
178void mallocEscapeFreeFree() {
179 int *p = malloc(12);
180 myfoo(p);
181 free(p);
182 free(p); // expected-warning{{Try to free a memory block that has been released}}
183}
184
185void mallocEscapeFreeUse() {
186 int *p = malloc(12);
187 myfoo(p);
188 free(p);
Anna Zaks31886862012-02-10 01:11:00 +0000189 myfoo(p); // expected-warning{{Use of dynamically allocated memory after it is freed.}}
Anna Zaksa1b227b2012-02-08 23:16:56 +0000190}
191
192int *myalloc();
193void myalloc2(int **p);
194
195void mallocEscapeFreeCustomAlloc() {
196 int *p = malloc(12);
197 myfoo(p);
198 free(p);
199 p = myalloc();
200 free(p); // no warning
201}
202
203void mallocEscapeFreeCustomAlloc2() {
204 int *p = malloc(12);
205 myfoo(p);
206 free(p);
207 myalloc2(&p);
208 free(p); // no warning
209}
210
211void mallocBindFreeUse() {
212 int *x = malloc(12);
213 int *y = x;
214 free(y);
Anna Zaks31886862012-02-10 01:11:00 +0000215 myfoo(x); // expected-warning{{Use of dynamically allocated memory after it is freed.}}
Anna Zaksa1b227b2012-02-08 23:16:56 +0000216}
217
218void mallocEscapeMalloc() {
219 int *p = malloc(12);
220 myfoo(p);
221 p = malloc(12); // expected-warning{{Allocated memory never released. Potential memory leak.}}
222}
223
224void mallocMalloc() {
225 int *p = malloc(12);
226 p = malloc(12); // expected-warning{{Allocated memory never released. Potential memory leak}}
227}
228
229void mallocFreeMalloc() {
230 int *p = malloc(12);
231 free(p);
232 p = malloc(12);
233 free(p);
234}
235
Anna Zaks12259b42012-02-09 06:25:47 +0000236void mallocFreeUse_params() {
Anna Zaksa1b227b2012-02-08 23:16:56 +0000237 int *p = malloc(12);
238 free(p);
Anna Zaks31886862012-02-10 01:11:00 +0000239 myfoo(p); //expected-warning{{Use of dynamically allocated memory after it is freed}}
240 myfooint(*p); //expected-warning{{Use of dynamically allocated memory after it is freed}}
Anna Zaksa1b227b2012-02-08 23:16:56 +0000241}
242
Anna Zaks2b5bb972012-02-09 06:25:51 +0000243void mallocFailedOrNot() {
244 int *p = malloc(12);
245 if (!p)
246 free(p);
247 else
248 free(p);
249}
250
Anna Zaks31886862012-02-10 01:11:00 +0000251struct StructWithInt {
252 int g;
253};
254void nonSymbolAsFirstArg(int *pp, struct StructWithInt *p);
255
256void mallocEscapeFooNonSymbolArg() {
257 struct StructWithInt *p = malloc(sizeof(struct StructWithInt));
258 nonSymbolAsFirstArg(&p->g, p);
259 return; // no warning
260}
261
Anna Zaksbb1ef902012-02-11 21:02:35 +0000262void mallocFailedOrNotLeak() {
263 int *p = malloc(12);
264 if (p == 0)
265 return; // no warning
266 else
267 return; // expected-warning {{Allocated memory never released. Potential memory leak.}}
268}
Anna Zaks31886862012-02-10 01:11:00 +0000269
Anna Zaks12259b42012-02-09 06:25:47 +0000270int *Gl;
271struct GlStTy {
272 int *x;
273};
274
275struct GlStTy GlS = {0};
276
277void GlobalFree() {
278 free(Gl);
279}
280
281void GlobalMalloc() {
282 Gl = malloc(12);
283}
284
285void GlobalStructMalloc() {
286 int *a = malloc(12);
287 GlS.x = a;
288}
289
290void GlobalStructMallocFree() {
291 int *a = malloc(12);
292 GlS.x = a;
293 free(GlS.x);
294}
Anna Zakse963fd52012-02-10 01:11:03 +0000295
Anna Zaksbb1ef902012-02-11 21:02:35 +0000296// Region escape testing.
297
298unsigned takePtrToPtr(int **p);
299void PassTheAddrOfAllocatedData(int f) {
300 int *p = malloc(12);
301 // We don't know what happens after the call. Should stop tracking here.
302 if (takePtrToPtr(&p))
303 f++;
304 free(p); // no warning
305}
306
307struct X {
308 int *p;
309};
310unsigned takePtrToStruct(struct X *s);
311int ** foo2(int *g, int f) {
312 int *p = malloc(12);
313 struct X *px= malloc(sizeof(struct X));
314 px->p = p;
315 // We don't know what happens after this call. Should not track px nor p.
316 if (takePtrToStruct(px))
317 f++;
318 free(p);
319 return 0;
320}
321
322struct X* RegInvalidationDetect1(struct X *s2) {
323 struct X *px= malloc(sizeof(struct X));
324 px->p = 0;
325 px = s2;
326 return px; // expected-warning {{Allocated memory never released. Potential memory leak.}}
327}
328
329struct X* RegInvalidationGiveUp1() {
330 int *p = malloc(12);
331 struct X *px= malloc(sizeof(struct X));
332 px->p = p;
333 return px;
334}
335
336int **RegInvalidationDetect2(int **pp) {
337 int *p = malloc(12);
338 pp = &p;
339 pp++;
340 return 0;// expected-warning {{Allocated memory never released. Potential memory leak.}}
341}
Anna Zakse963fd52012-02-10 01:11:03 +0000342
Anna Zakse963fd52012-02-10 01:11:03 +0000343extern void exit(int) __attribute__ ((__noreturn__));
344void mallocExit(int *g) {
345 struct xx *p = malloc(12);
Anna Zaksd3571e5a2012-02-11 21:02:40 +0000346 if (g != 0)
347 exit(1);
Anna Zakse963fd52012-02-10 01:11:03 +0000348 free(p);
349 return;
350}
351
Anna Zakse963fd52012-02-10 01:11:03 +0000352extern void __assert_fail (__const char *__assertion, __const char *__file,
353 unsigned int __line, __const char *__function)
354 __attribute__ ((__noreturn__));
355#define assert(expr) \
356 ((expr) ? (void)(0) : __assert_fail (#expr, __FILE__, __LINE__, __func__))
357void mallocAssert(int *g) {
358 struct xx *p = malloc(12);
359
Anna Zaksd3571e5a2012-02-11 21:02:40 +0000360 assert(g != 0);
Anna Zakse963fd52012-02-10 01:11:03 +0000361 free(p);
362 return;
363}
364
Anna Zaksd3571e5a2012-02-11 21:02:40 +0000365// Below are the known false positives.
366
Anna Zakse963fd52012-02-10 01:11:03 +0000367// TODO: There should be no warning here.
Anna Zakse963fd52012-02-10 01:11:03 +0000368void reallocFails(int *g, int f) {
369 char *p = malloc(12);
370 char *r = realloc(p, 12+1);
371 if (!r) {
372 free(p); // expected-warning {{Try to free a memory block that has been released}}
373 } else {
374 free(r);
375 }
376}
377
378// TODO: There should be no warning here. This one might be difficult to get rid of.
379void dependsOnValueOfPtr(int *g, unsigned f) {
380 int *p;
381
382 if (f) {
383 p = g;
384 } else {
385 p = malloc(12);
386 }
387
388 if (p != g)
389 free(p);
390 else
391 return; // expected-warning{{Allocated memory never released. Potential memory leak}}
392 return;
393}
394
395// TODO: Should this be a warning?
396// Here we are returning a pointer one past the allocated value. An idiom which
397// can be used for implementing special malloc. The correct uses of this might
398// be rare enough so that we could keep this as a warning.
399static void *specialMalloc(int n){
400 int *p;
401 p = malloc( n+8 );
402 if( p ){
403 p[0] = n;
404 p++;
405 }
406 return p;// expected-warning {{Allocated memory never released. Potential memory leak.}}
407}