blob: 2d62706ce4409263add2e20d5659c25596917e8b [file] [log] [blame]
Ted Kremenek033a07e2011-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
Anna Zaks15d0ae12012-02-11 23:46:36 +00002#include "system-header-simulator.h"
3
Eli Friedman2f005522009-11-14 04:23:25 +00004typedef __typeof(sizeof(int)) size_t;
Ted Kremenekc3607752009-11-13 20:03:22 +00005void *malloc(size_t);
Anna Zaksb16ce452012-02-15 00:11:22 +00006void *valloc(size_t);
Ted Kremenekc3607752009-11-13 20:03:22 +00007void free(void *);
Zhongxing Xud9c84c82009-12-12 12:29:38 +00008void *realloc(void *ptr, size_t size);
Anna Zaks40add292012-02-15 00:11:25 +00009void *reallocf(void *ptr, size_t size);
Zhongxing Xud9c84c82009-12-12 12:29:38 +000010void *calloc(size_t nmemb, size_t size);
Ted Kremenekdd0e4902010-07-31 01:52:11 +000011
Anna Zaks91c2a112012-02-08 23:16:56 +000012void myfoo(int *p);
13void myfooint(int p);
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +000014
15void f1() {
Zhongxing Xuab280992010-05-25 04:59:19 +000016 int *p = malloc(12);
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +000017 return; // expected-warning{{Allocated memory never released. Potential memory leak.}}
18}
19
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +000020void f2() {
Zhongxing Xuab280992010-05-25 04:59:19 +000021 int *p = malloc(12);
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +000022 free(p);
23 free(p); // expected-warning{{Try to free a memory block that has been released}}
24}
Ted Kremenekc764d4b2009-11-13 20:00:28 +000025
Lenny Maiorani4d8d8032011-04-27 14:49:29 +000026void f2_realloc_0() {
27 int *p = malloc(12);
28 realloc(p,0);
29 realloc(p,0); // expected-warning{{Try to free a memory block that has been released}}
30}
31
32void f2_realloc_1() {
33 int *p = malloc(12);
Zhongxing Xud56763f2011-09-01 04:53:59 +000034 int *q = realloc(p,0); // no-warning
Lenny Maiorani4d8d8032011-04-27 14:49:29 +000035}
36
Anna Zaksc8bb3be2012-02-13 18:05:39 +000037void reallocNotNullPtr(unsigned sizeIn) {
38 unsigned size = 12;
39 char *p = (char*)malloc(size);
40 if (p) {
41 char *q = (char*)realloc(p, sizeIn);
42 char x = *q; // expected-warning {{Allocated memory never released.}}
43 }
44}
45
46int *realloctest1() {
47 int *q = malloc(12);
48 q = realloc(q, 20);
49 return q; // no warning - returning the allocated value
50}
51
52// p should be freed if realloc fails.
53void reallocFails() {
54 char *p = malloc(12);
55 char *r = realloc(p, 12+1);
56 if (!r) {
57 free(p);
58 } else {
59 free(r);
60 }
61}
62
Anna Zaks30838b92012-02-13 20:57:07 +000063void reallocSizeZero1() {
64 char *p = malloc(12);
65 char *r = realloc(p, 0);
66 if (!r) {
67 free(p);
68 } else {
69 free(r);
70 }
71}
72
73void reallocSizeZero2() {
74 char *p = malloc(12);
75 char *r = realloc(p, 0);
76 if (!r) {
77 free(p);
78 } else {
79 free(r);
80 }
81 free(p); // expected-warning {{Try to free a memory block that has been released}}
82}
83
84void reallocSizeZero3() {
85 char *p = malloc(12);
86 char *r = realloc(p, 0);
87 free(r);
88}
89
90void reallocSizeZero4() {
91 char *r = realloc(0, 0);
92 free(r);
93}
94
95void reallocSizeZero5() {
96 char *r = realloc(0, 0);
97}
98
99void reallocPtrZero1() {
100 char *r = realloc(0, 12); // expected-warning {{Allocated memory never released.}}
101}
102
103void reallocPtrZero2() {
104 char *r = realloc(0, 12);
105 if (r)
106 free(r);
107}
108
109void reallocPtrZero3() {
110 char *r = realloc(0, 12);
111 free(r);
112}
113
Anna Zaksb276bd92012-02-14 00:26:13 +0000114void reallocRadar6337483_1() {
115 char *buf = malloc(100);
116 buf = (char*)realloc(buf, 0x1000000);
117 if (!buf) {
118 return;// expected-warning {{Allocated memory never released.}}
119 }
120 free(buf);
121}
122
123void reallocRadar6337483_2() {
124 char *buf = malloc(100);
125 char *buf2 = (char*)realloc(buf, 0x1000000);
126 if (!buf2) { // expected-warning {{Allocated memory never released.}}
127 ;
128 } else {
129 free(buf2);
130 }
131}
132
133void reallocRadar6337483_3() {
134 char * buf = malloc(100);
135 char * tmp;
136 tmp = (char*)realloc(buf, 0x1000000);
137 if (!tmp) {
138 free(buf);
139 return;
140 }
141 buf = tmp;
142 free(buf);
143}
144
145void reallocRadar6337483_4() {
146 char *buf = malloc(100);
147 char *buf2 = (char*)realloc(buf, 0x1000000);
148 if (!buf2) {
149 return; // expected-warning {{Allocated memory never released.}}
150 } else {
151 free(buf2);
152 }
153}
154
Anna Zaks40add292012-02-15 00:11:25 +0000155int *reallocfTest1() {
156 int *q = malloc(12);
157 q = reallocf(q, 20);
158 return q; // no warning - returning the allocated value
159}
160
161void reallocfRadar6337483_4() {
162 char *buf = malloc(100);
163 char *buf2 = (char*)reallocf(buf, 0x1000000);
164 if (!buf2) {
165 return; // no warning - reallocf frees even on failure
166 } else {
167 free(buf2);
168 }
169}
170
171void reallocfRadar6337483_3() {
172 char * buf = malloc(100);
173 char * tmp;
174 tmp = (char*)reallocf(buf, 0x1000000);
175 if (!tmp) {
176 free(buf); // expected-warning {{Try to free a memory block that has been released}}
177 return;
178 }
179 buf = tmp;
180 free(buf);
181}
182
183void reallocfPtrZero1() {
184 char *r = reallocf(0, 12); // expected-warning {{Allocated memory never released.}}
185}
186
187
Zhongxing Xu243fde92009-11-17 07:54:15 +0000188// This case tests that storing malloc'ed memory to a static variable which is
189// then returned is not leaked. In the absence of known contracts for functions
190// or inter-procedural analysis, this is a conservative answer.
Ted Kremenekc764d4b2009-11-13 20:00:28 +0000191int *f3() {
192 static int *p = 0;
Zhongxing Xuab280992010-05-25 04:59:19 +0000193 p = malloc(12);
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000194 return p; // no-warning
Ted Kremenekc764d4b2009-11-13 20:00:28 +0000195}
196
Zhongxing Xu243fde92009-11-17 07:54:15 +0000197// This case tests that storing malloc'ed memory to a static global variable
198// which is then returned is not leaked. In the absence of known contracts for
199// functions or inter-procedural analysis, this is a conservative answer.
Ted Kremenekc764d4b2009-11-13 20:00:28 +0000200static int *p_f4 = 0;
201int *f4() {
Zhongxing Xuab280992010-05-25 04:59:19 +0000202 p_f4 = malloc(12);
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000203 return p_f4; // no-warning
Ted Kremenekc764d4b2009-11-13 20:00:28 +0000204}
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000205
206int *f5() {
Zhongxing Xuab280992010-05-25 04:59:19 +0000207 int *q = malloc(12);
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000208 q = realloc(q, 20);
209 return q; // no-warning
210}
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000211
212void f6() {
Zhongxing Xuab280992010-05-25 04:59:19 +0000213 int *p = malloc(12);
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000214 if (!p)
215 return; // no-warning
216 else
217 free(p);
218}
Zhongxing Xu425c7ed2010-01-18 04:01:40 +0000219
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000220void f6_realloc() {
221 int *p = malloc(12);
222 if (!p)
223 return; // no-warning
224 else
225 realloc(p,0);
226}
227
228
Zhongxing Xu425c7ed2010-01-18 04:01:40 +0000229char *doit2();
230void pr6069() {
231 char *buf = doit2();
232 free(buf);
233}
Zhongxing Xu181cc3d2010-02-14 06:49:48 +0000234
235void pr6293() {
236 free(0);
237}
Zhongxing Xuc8023782010-03-10 04:58:55 +0000238
239void f7() {
240 char *x = (char*) malloc(4);
241 free(x);
Anna Zakse9ef5622012-02-10 01:11:00 +0000242 x[0] = 'a'; // expected-warning{{Use of dynamically allocated memory after it is freed.}}
Zhongxing Xuc8023782010-03-10 04:58:55 +0000243}
Zhongxing Xuab280992010-05-25 04:59:19 +0000244
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000245void f7_realloc() {
246 char *x = (char*) malloc(4);
247 realloc(x,0);
Anna Zakse9ef5622012-02-10 01:11:00 +0000248 x[0] = 'a'; // expected-warning{{Use of dynamically allocated memory after it is freed.}}
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000249}
250
Zhongxing Xuab280992010-05-25 04:59:19 +0000251void PR6123() {
252 int *x = malloc(11); // expected-warning{{Cast a region whose size is not a multiple of the destination type size.}}
253}
254
255void PR7217() {
256 int *buf = malloc(2); // expected-warning{{Cast a region whose size is not a multiple of the destination type size.}}
257 buf[1] = 'c'; // not crash
Zhongxing Xuab280992010-05-25 04:59:19 +0000258}
Jordy Rosec580f2e2010-06-20 04:30:57 +0000259
260void mallocCastToVoid() {
261 void *p = malloc(2);
262 const void *cp = p; // not crash
263 free(p);
264}
265
266void mallocCastToFP() {
267 void *p = malloc(2);
268 void (*fp)() = p; // not crash
269 free(p);
270}
271
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000272// This tests that malloc() buffers are undefined by default
273char mallocGarbage () {
274 char *buf = malloc(2);
275 char result = buf[1]; // expected-warning{{undefined}}
276 free(buf);
277 return result;
278}
279
280// This tests that calloc() buffers need to be freed
281void callocNoFree () {
282 char *buf = calloc(2,2);
283 return; // expected-warning{{never released}}
284}
285
286// These test that calloc() buffers are zeroed by default
287char callocZeroesGood () {
288 char *buf = calloc(2,2);
289 char result = buf[3]; // no-warning
290 if (buf[1] == 0) {
291 free(buf);
292 }
293 return result; // no-warning
294}
295
296char callocZeroesBad () {
297 char *buf = calloc(2,2);
298 char result = buf[3]; // no-warning
299 if (buf[1] != 0) {
Tom Carec4b5bd82010-07-23 23:04:53 +0000300 free(buf); // expected-warning{{never executed}}
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000301 }
302 return result; // expected-warning{{never released}}
303}
Anna Zaks91c2a112012-02-08 23:16:56 +0000304
305void nullFree() {
306 int *p = 0;
307 free(p); // no warning - a nop
308}
309
310void paramFree(int *p) {
311 myfoo(p);
312 free(p); // no warning
313 myfoo(p); // TODO: This should be a warning.
314}
315
316int* mallocEscapeRet() {
317 int *p = malloc(12);
318 return p; // no warning
319}
320
321void mallocEscapeFoo() {
322 int *p = malloc(12);
323 myfoo(p);
324 return; // no warning
325}
326
327void mallocEscapeFree() {
328 int *p = malloc(12);
329 myfoo(p);
330 free(p);
331}
332
333void mallocEscapeFreeFree() {
334 int *p = malloc(12);
335 myfoo(p);
336 free(p);
337 free(p); // expected-warning{{Try to free a memory block that has been released}}
338}
339
340void mallocEscapeFreeUse() {
341 int *p = malloc(12);
342 myfoo(p);
343 free(p);
Anna Zakse9ef5622012-02-10 01:11:00 +0000344 myfoo(p); // expected-warning{{Use of dynamically allocated memory after it is freed.}}
Anna Zaks91c2a112012-02-08 23:16:56 +0000345}
346
347int *myalloc();
348void myalloc2(int **p);
349
350void mallocEscapeFreeCustomAlloc() {
351 int *p = malloc(12);
352 myfoo(p);
353 free(p);
354 p = myalloc();
355 free(p); // no warning
356}
357
358void mallocEscapeFreeCustomAlloc2() {
359 int *p = malloc(12);
360 myfoo(p);
361 free(p);
362 myalloc2(&p);
363 free(p); // no warning
364}
365
366void mallocBindFreeUse() {
367 int *x = malloc(12);
368 int *y = x;
369 free(y);
Anna Zakse9ef5622012-02-10 01:11:00 +0000370 myfoo(x); // expected-warning{{Use of dynamically allocated memory after it is freed.}}
Anna Zaks91c2a112012-02-08 23:16:56 +0000371}
372
373void mallocEscapeMalloc() {
374 int *p = malloc(12);
375 myfoo(p);
376 p = malloc(12); // expected-warning{{Allocated memory never released. Potential memory leak.}}
377}
378
379void mallocMalloc() {
380 int *p = malloc(12);
381 p = malloc(12); // expected-warning{{Allocated memory never released. Potential memory leak}}
382}
383
384void mallocFreeMalloc() {
385 int *p = malloc(12);
386 free(p);
387 p = malloc(12);
388 free(p);
389}
390
Anna Zakscdfec5e2012-02-09 06:25:47 +0000391void mallocFreeUse_params() {
Anna Zaks91c2a112012-02-08 23:16:56 +0000392 int *p = malloc(12);
393 free(p);
Anna Zakse9ef5622012-02-10 01:11:00 +0000394 myfoo(p); //expected-warning{{Use of dynamically allocated memory after it is freed}}
Anna Zaks15d0ae12012-02-11 23:46:36 +0000395}
396
397void mallocFreeUse_params2() {
398 int *p = malloc(12);
399 free(p);
Anna Zakse9ef5622012-02-10 01:11:00 +0000400 myfooint(*p); //expected-warning{{Use of dynamically allocated memory after it is freed}}
Anna Zaks91c2a112012-02-08 23:16:56 +0000401}
402
Anna Zaksff3b9fd2012-02-09 06:25:51 +0000403void mallocFailedOrNot() {
404 int *p = malloc(12);
405 if (!p)
406 free(p);
407 else
408 free(p);
409}
410
Anna Zakse9ef5622012-02-10 01:11:00 +0000411struct StructWithInt {
412 int g;
413};
Anna Zaks0860cd02012-02-11 21:44:39 +0000414
415int *mallocReturnFreed() {
416 int *p = malloc(12);
417 free(p);
418 return p; // expected-warning {{Use of dynamically allocated}}
419}
420
421int useAfterFreeStruct() {
422 struct StructWithInt *px= malloc(sizeof(struct StructWithInt));
423 px->g = 5;
424 free(px);
425 return px->g; // expected-warning {{Use of dynamically allocated}}
426}
427
Anna Zakse9ef5622012-02-10 01:11:00 +0000428void nonSymbolAsFirstArg(int *pp, struct StructWithInt *p);
429
430void mallocEscapeFooNonSymbolArg() {
431 struct StructWithInt *p = malloc(sizeof(struct StructWithInt));
432 nonSymbolAsFirstArg(&p->g, p);
433 return; // no warning
434}
435
Anna Zaks4fb54872012-02-11 21:02:35 +0000436void mallocFailedOrNotLeak() {
437 int *p = malloc(12);
438 if (p == 0)
439 return; // no warning
440 else
441 return; // expected-warning {{Allocated memory never released. Potential memory leak.}}
442}
Anna Zakse9ef5622012-02-10 01:11:00 +0000443
Anna Zaksb16ce452012-02-15 00:11:22 +0000444int vallocTest() {
445 char *mem = valloc(12);
446 return 0; // expected-warning {{Allocated memory never released. Potential memory leak.}}
447}
448
449void vallocEscapeFreeUse() {
450 int *p = valloc(12);
451 myfoo(p);
452 free(p);
453 myfoo(p); // expected-warning{{Use of dynamically allocated memory after it is freed.}}
454}
455
Anna Zakscdfec5e2012-02-09 06:25:47 +0000456int *Gl;
457struct GlStTy {
458 int *x;
459};
460
461struct GlStTy GlS = {0};
462
463void GlobalFree() {
464 free(Gl);
465}
466
467void GlobalMalloc() {
468 Gl = malloc(12);
469}
470
471void GlobalStructMalloc() {
472 int *a = malloc(12);
473 GlS.x = a;
474}
475
476void GlobalStructMallocFree() {
477 int *a = malloc(12);
478 GlS.x = a;
479 free(GlS.x);
480}
Anna Zaksf8b1c312012-02-10 01:11:03 +0000481
Anna Zaks4fb54872012-02-11 21:02:35 +0000482// Region escape testing.
483
484unsigned takePtrToPtr(int **p);
485void PassTheAddrOfAllocatedData(int f) {
486 int *p = malloc(12);
487 // We don't know what happens after the call. Should stop tracking here.
488 if (takePtrToPtr(&p))
489 f++;
490 free(p); // no warning
491}
492
493struct X {
494 int *p;
495};
496unsigned takePtrToStruct(struct X *s);
497int ** foo2(int *g, int f) {
498 int *p = malloc(12);
499 struct X *px= malloc(sizeof(struct X));
500 px->p = p;
501 // We don't know what happens after this call. Should not track px nor p.
502 if (takePtrToStruct(px))
503 f++;
504 free(p);
505 return 0;
506}
507
508struct X* RegInvalidationDetect1(struct X *s2) {
509 struct X *px= malloc(sizeof(struct X));
510 px->p = 0;
511 px = s2;
512 return px; // expected-warning {{Allocated memory never released. Potential memory leak.}}
513}
514
515struct X* RegInvalidationGiveUp1() {
516 int *p = malloc(12);
517 struct X *px= malloc(sizeof(struct X));
518 px->p = p;
519 return px;
520}
521
522int **RegInvalidationDetect2(int **pp) {
523 int *p = malloc(12);
524 pp = &p;
525 pp++;
526 return 0;// expected-warning {{Allocated memory never released. Potential memory leak.}}
527}
Anna Zaksf8b1c312012-02-10 01:11:03 +0000528
Anna Zaksf8b1c312012-02-10 01:11:03 +0000529extern void exit(int) __attribute__ ((__noreturn__));
530void mallocExit(int *g) {
531 struct xx *p = malloc(12);
Anna Zaksda046772012-02-11 21:02:40 +0000532 if (g != 0)
533 exit(1);
Anna Zaksf8b1c312012-02-10 01:11:03 +0000534 free(p);
535 return;
536}
537
Anna Zaksf8b1c312012-02-10 01:11:03 +0000538extern void __assert_fail (__const char *__assertion, __const char *__file,
539 unsigned int __line, __const char *__function)
540 __attribute__ ((__noreturn__));
541#define assert(expr) \
542 ((expr) ? (void)(0) : __assert_fail (#expr, __FILE__, __LINE__, __func__))
543void mallocAssert(int *g) {
544 struct xx *p = malloc(12);
545
Anna Zaksda046772012-02-11 21:02:40 +0000546 assert(g != 0);
Anna Zaksf8b1c312012-02-10 01:11:03 +0000547 free(p);
548 return;
549}
550
Anna Zaks15d0ae12012-02-11 23:46:36 +0000551void doNotInvalidateWhenPassedToSystemCalls(char *s) {
552 char *p = malloc(12);
553 strlen(p);
554 strcpy(p, s); // expected-warning {{leak}}
555}
556
Anna Zaksda046772012-02-11 21:02:40 +0000557// Below are the known false positives.
558
Anna Zaksf8b1c312012-02-10 01:11:03 +0000559// TODO: There should be no warning here. This one might be difficult to get rid of.
560void dependsOnValueOfPtr(int *g, unsigned f) {
561 int *p;
562
563 if (f) {
564 p = g;
565 } else {
566 p = malloc(12);
567 }
568
569 if (p != g)
570 free(p);
571 else
572 return; // expected-warning{{Allocated memory never released. Potential memory leak}}
573 return;
574}
575
576// TODO: Should this be a warning?
577// Here we are returning a pointer one past the allocated value. An idiom which
578// can be used for implementing special malloc. The correct uses of this might
579// be rare enough so that we could keep this as a warning.
580static void *specialMalloc(int n){
581 int *p;
582 p = malloc( n+8 );
583 if( p ){
584 p[0] = n;
585 p++;
586 }
587 return p;// expected-warning {{Allocated memory never released. Potential memory leak.}}
588}