blob: 3e5f914e9d91c033b1cbc2c1dc6db44563bcfe1f [file] [log] [blame]
Ted Kremenek722398f2012-08-24 20:39:55 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=core,alpha.deadcode.UnreachableCode,alpha.core.CastSize,unix.Malloc,debug.ExprInspection -analyzer-store=region -verify %s
NAKAMURA Takumid05b01a2012-11-19 10:00:59 +00002
Chandler Carruth66a34a62012-09-12 01:11:10 +00003#include "Inputs/system-header-simulator.h"
Anna Zaks41b84842012-02-11 23:46:36 +00004
Anna Zaks93205d02012-06-08 00:04:40 +00005void clang_analyzer_eval(int);
6
Eli Friedmanb7746852009-11-14 04:23:25 +00007typedef __typeof(sizeof(int)) size_t;
Ted Kremenek9430bf22009-11-13 20:03:22 +00008void *malloc(size_t);
Anna Zaksd5157482012-02-15 00:11:22 +00009void *valloc(size_t);
Ted Kremenek9430bf22009-11-13 20:03:22 +000010void free(void *);
Zhongxing Xuc0484fa2009-12-12 12:29:38 +000011void *realloc(void *ptr, size_t size);
Anna Zaksac068142012-02-15 00:11:25 +000012void *reallocf(void *ptr, size_t size);
Zhongxing Xuc0484fa2009-12-12 12:29:38 +000013void *calloc(size_t nmemb, size_t size);
Anna Zaks46d01602012-05-18 01:16:10 +000014char *strdup(const char *s);
15char *strndup(const char *s, size_t n);
Ted Kremenekd21139a2010-07-31 01:52:11 +000016
Anna Zaksa1b227b2012-02-08 23:16:56 +000017void myfoo(int *p);
18void myfooint(int p);
Anna Zaks5a6213d2012-02-15 00:11:28 +000019char *fooRetPtr();
Zhongxing Xuc7460962009-11-13 07:48:11 +000020
21void f1() {
Zhongxing Xu658dd8b2010-05-25 04:59:19 +000022 int *p = malloc(12);
Jordan Rosed86b3bd2012-08-08 18:23:36 +000023 return; // expected-warning{{Memory is never released; potential leak of memory pointed to by 'p'}}
Zhongxing Xuc7460962009-11-13 07:48:11 +000024}
25
Zhongxing Xuc7460962009-11-13 07:48:11 +000026void f2() {
Zhongxing Xu658dd8b2010-05-25 04:59:19 +000027 int *p = malloc(12);
Zhongxing Xuc7460962009-11-13 07:48:11 +000028 free(p);
Anna Zaks546c49c2012-02-16 22:26:12 +000029 free(p); // expected-warning{{Attempt to free released memory}}
Zhongxing Xuc7460962009-11-13 07:48:11 +000030}
Ted Kremeneke5e977012009-11-13 20:00:28 +000031
Lenny Maiorani005b5c12011-04-27 14:49:29 +000032void f2_realloc_0() {
33 int *p = malloc(12);
34 realloc(p,0);
Anna Zaks546c49c2012-02-16 22:26:12 +000035 realloc(p,0); // expected-warning{{Attempt to free released memory}}
Lenny Maiorani005b5c12011-04-27 14:49:29 +000036}
37
38void f2_realloc_1() {
39 int *p = malloc(12);
Zhongxing Xubfb8e2f2011-09-01 04:53:59 +000040 int *q = realloc(p,0); // no-warning
Lenny Maiorani005b5c12011-04-27 14:49:29 +000041}
42
Anna Zaksd56c8792012-02-13 18:05:39 +000043void reallocNotNullPtr(unsigned sizeIn) {
44 unsigned size = 12;
45 char *p = (char*)malloc(size);
46 if (p) {
47 char *q = (char*)realloc(p, sizeIn);
Jordan Rosed86b3bd2012-08-08 18:23:36 +000048 char x = *q; // expected-warning {{Memory is never released; potential leak of memory pointed to by 'q'}}
Anna Zaksd56c8792012-02-13 18:05:39 +000049 }
50}
51
52int *realloctest1() {
53 int *q = malloc(12);
54 q = realloc(q, 20);
55 return q; // no warning - returning the allocated value
56}
57
58// p should be freed if realloc fails.
59void reallocFails() {
60 char *p = malloc(12);
61 char *r = realloc(p, 12+1);
62 if (!r) {
63 free(p);
64 } else {
65 free(r);
66 }
67}
68
Anna Zaks8fd0f2a2012-02-13 20:57:07 +000069void reallocSizeZero1() {
70 char *p = malloc(12);
71 char *r = realloc(p, 0);
72 if (!r) {
Anna Zaks52242a62012-08-03 18:30:18 +000073 free(p); // expected-warning {{Attempt to free released memory}}
Anna Zaks8fd0f2a2012-02-13 20:57:07 +000074 } else {
75 free(r);
76 }
77}
78
79void reallocSizeZero2() {
80 char *p = malloc(12);
81 char *r = realloc(p, 0);
82 if (!r) {
Anna Zaks52242a62012-08-03 18:30:18 +000083 free(p); // expected-warning {{Attempt to free released memory}}
Anna Zaks8fd0f2a2012-02-13 20:57:07 +000084 } else {
85 free(r);
86 }
Anna Zaks546c49c2012-02-16 22:26:12 +000087 free(p); // expected-warning {{Attempt to free released memory}}
Anna Zaks8fd0f2a2012-02-13 20:57:07 +000088}
89
90void reallocSizeZero3() {
91 char *p = malloc(12);
92 char *r = realloc(p, 0);
93 free(r);
94}
95
96void reallocSizeZero4() {
97 char *r = realloc(0, 0);
98 free(r);
99}
100
101void reallocSizeZero5() {
102 char *r = realloc(0, 0);
103}
104
105void reallocPtrZero1() {
Jordan Rosee37ab502012-11-15 19:11:43 +0000106 char *r = realloc(0, 12);
107} // expected-warning {{Memory is never released; potential leak of memory pointed to by 'r'}}
Anna Zaks8fd0f2a2012-02-13 20:57:07 +0000108
109void reallocPtrZero2() {
110 char *r = realloc(0, 12);
111 if (r)
112 free(r);
113}
114
115void reallocPtrZero3() {
116 char *r = realloc(0, 12);
117 free(r);
118}
119
Anna Zaksad01ef52012-02-14 00:26:13 +0000120void reallocRadar6337483_1() {
121 char *buf = malloc(100);
122 buf = (char*)realloc(buf, 0x1000000);
123 if (!buf) {
Anna Zaksfc2e1532012-03-21 19:45:08 +0000124 return;// expected-warning {{Memory is never released; potential leak}}
Anna Zaksad01ef52012-02-14 00:26:13 +0000125 }
126 free(buf);
127}
128
129void reallocRadar6337483_2() {
130 char *buf = malloc(100);
131 char *buf2 = (char*)realloc(buf, 0x1000000);
Jordan Rosee37ab502012-11-15 19:11:43 +0000132 if (!buf2) {
Anna Zaksad01ef52012-02-14 00:26:13 +0000133 ;
134 } else {
135 free(buf2);
136 }
Jordan Rosee37ab502012-11-15 19:11:43 +0000137} // expected-warning {{Memory is never released; potential leak}}
Anna Zaksad01ef52012-02-14 00:26:13 +0000138
139void reallocRadar6337483_3() {
140 char * buf = malloc(100);
141 char * tmp;
142 tmp = (char*)realloc(buf, 0x1000000);
143 if (!tmp) {
144 free(buf);
145 return;
146 }
147 buf = tmp;
148 free(buf);
149}
150
151void reallocRadar6337483_4() {
152 char *buf = malloc(100);
153 char *buf2 = (char*)realloc(buf, 0x1000000);
154 if (!buf2) {
Anna Zaksfc2e1532012-03-21 19:45:08 +0000155 return; // expected-warning {{Memory is never released; potential leak}}
Anna Zaksad01ef52012-02-14 00:26:13 +0000156 } else {
157 free(buf2);
158 }
159}
160
Anna Zaksac068142012-02-15 00:11:25 +0000161int *reallocfTest1() {
162 int *q = malloc(12);
163 q = reallocf(q, 20);
164 return q; // no warning - returning the allocated value
165}
166
167void reallocfRadar6337483_4() {
168 char *buf = malloc(100);
169 char *buf2 = (char*)reallocf(buf, 0x1000000);
170 if (!buf2) {
171 return; // no warning - reallocf frees even on failure
172 } else {
173 free(buf2);
174 }
175}
176
177void reallocfRadar6337483_3() {
178 char * buf = malloc(100);
179 char * tmp;
180 tmp = (char*)reallocf(buf, 0x1000000);
181 if (!tmp) {
Anna Zaks546c49c2012-02-16 22:26:12 +0000182 free(buf); // expected-warning {{Attempt to free released memory}}
Anna Zaksac068142012-02-15 00:11:25 +0000183 return;
184 }
185 buf = tmp;
186 free(buf);
187}
188
189void reallocfPtrZero1() {
Jordan Rosee37ab502012-11-15 19:11:43 +0000190 char *r = reallocf(0, 12);
191} // expected-warning {{Memory is never released; potential leak}}
Anna Zaksac068142012-02-15 00:11:25 +0000192
193
Zhongxing Xu4668c7e2009-11-17 07:54:15 +0000194// This case tests that storing malloc'ed memory to a static variable which is
195// then returned is not leaked. In the absence of known contracts for functions
196// or inter-procedural analysis, this is a conservative answer.
Ted Kremeneke5e977012009-11-13 20:00:28 +0000197int *f3() {
198 static int *p = 0;
Zhongxing Xu658dd8b2010-05-25 04:59:19 +0000199 p = malloc(12);
Zhongxing Xu23baa012009-11-17 08:58:18 +0000200 return p; // no-warning
Ted Kremeneke5e977012009-11-13 20:00:28 +0000201}
202
Zhongxing Xu4668c7e2009-11-17 07:54:15 +0000203// This case tests that storing malloc'ed memory to a static global variable
204// which is then returned is not leaked. In the absence of known contracts for
205// functions or inter-procedural analysis, this is a conservative answer.
Ted Kremeneke5e977012009-11-13 20:00:28 +0000206static int *p_f4 = 0;
207int *f4() {
Zhongxing Xu658dd8b2010-05-25 04:59:19 +0000208 p_f4 = malloc(12);
Zhongxing Xu23baa012009-11-17 08:58:18 +0000209 return p_f4; // no-warning
Ted Kremeneke5e977012009-11-13 20:00:28 +0000210}
Zhongxing Xuc0484fa2009-12-12 12:29:38 +0000211
212int *f5() {
Zhongxing Xu658dd8b2010-05-25 04:59:19 +0000213 int *q = malloc(12);
Zhongxing Xuc0484fa2009-12-12 12:29:38 +0000214 q = realloc(q, 20);
215 return q; // no-warning
216}
Zhongxing Xub0e15df2009-12-31 06:13:07 +0000217
218void f6() {
Zhongxing Xu658dd8b2010-05-25 04:59:19 +0000219 int *p = malloc(12);
Zhongxing Xub0e15df2009-12-31 06:13:07 +0000220 if (!p)
221 return; // no-warning
222 else
223 free(p);
224}
Zhongxing Xu5fcd99b2010-01-18 04:01:40 +0000225
Lenny Maiorani005b5c12011-04-27 14:49:29 +0000226void f6_realloc() {
227 int *p = malloc(12);
228 if (!p)
229 return; // no-warning
230 else
231 realloc(p,0);
232}
233
234
Zhongxing Xu5fcd99b2010-01-18 04:01:40 +0000235char *doit2();
236void pr6069() {
237 char *buf = doit2();
238 free(buf);
239}
Zhongxing Xube36ecb2010-02-14 06:49:48 +0000240
241void pr6293() {
242 free(0);
243}
Zhongxing Xu1bb6a1a2010-03-10 04:58:55 +0000244
245void f7() {
246 char *x = (char*) malloc(4);
247 free(x);
Anna Zaks546c49c2012-02-16 22:26:12 +0000248 x[0] = 'a'; // expected-warning{{Use of memory after it is freed}}
Zhongxing Xu1bb6a1a2010-03-10 04:58:55 +0000249}
Zhongxing Xu658dd8b2010-05-25 04:59:19 +0000250
Anna Zaks46d01602012-05-18 01:16:10 +0000251void f8() {
252 char *x = (char*) malloc(4);
253 free(x);
254 char *y = strndup(x, 4); // expected-warning{{Use of memory after it is freed}}
255}
256
Lenny Maiorani005b5c12011-04-27 14:49:29 +0000257void f7_realloc() {
258 char *x = (char*) malloc(4);
259 realloc(x,0);
Anna Zaks546c49c2012-02-16 22:26:12 +0000260 x[0] = 'a'; // expected-warning{{Use of memory after it is freed}}
Lenny Maiorani005b5c12011-04-27 14:49:29 +0000261}
262
Zhongxing Xu658dd8b2010-05-25 04:59:19 +0000263void PR6123() {
Ted Kremenek9bf9af92012-08-16 17:45:23 +0000264 int *x = malloc(11); // expected-warning{{Cast a region whose size is not a multiple of the destination type size}}
Zhongxing Xu658dd8b2010-05-25 04:59:19 +0000265}
266
267void PR7217() {
Ted Kremenek9bf9af92012-08-16 17:45:23 +0000268 int *buf = malloc(2); // expected-warning{{Cast a region whose size is not a multiple of the destination type size}}
Zhongxing Xu658dd8b2010-05-25 04:59:19 +0000269 buf[1] = 'c'; // not crash
Zhongxing Xu658dd8b2010-05-25 04:59:19 +0000270}
Jordy Rose2dd9b022010-06-20 04:30:57 +0000271
272void mallocCastToVoid() {
273 void *p = malloc(2);
274 const void *cp = p; // not crash
275 free(p);
276}
277
278void mallocCastToFP() {
279 void *p = malloc(2);
280 void (*fp)() = p; // not crash
281 free(p);
282}
283
Zhongxing Xu527ff6d2010-06-01 03:01:33 +0000284// This tests that malloc() buffers are undefined by default
285char mallocGarbage () {
286 char *buf = malloc(2);
287 char result = buf[1]; // expected-warning{{undefined}}
288 free(buf);
289 return result;
290}
291
292// This tests that calloc() buffers need to be freed
293void callocNoFree () {
294 char *buf = calloc(2,2);
295 return; // expected-warning{{never released}}
296}
297
298// These test that calloc() buffers are zeroed by default
299char callocZeroesGood () {
300 char *buf = calloc(2,2);
301 char result = buf[3]; // no-warning
302 if (buf[1] == 0) {
303 free(buf);
304 }
305 return result; // no-warning
306}
307
308char callocZeroesBad () {
309 char *buf = calloc(2,2);
310 char result = buf[3]; // no-warning
311 if (buf[1] != 0) {
Tom Carecba9f512010-07-23 23:04:53 +0000312 free(buf); // expected-warning{{never executed}}
Zhongxing Xu527ff6d2010-06-01 03:01:33 +0000313 }
314 return result; // expected-warning{{never released}}
315}
Anna Zaksa1b227b2012-02-08 23:16:56 +0000316
317void nullFree() {
318 int *p = 0;
319 free(p); // no warning - a nop
320}
321
322void paramFree(int *p) {
323 myfoo(p);
324 free(p); // no warning
Anna Zaks52242a62012-08-03 18:30:18 +0000325 myfoo(p); // expected-warning {{Use of memory after it is freed}}
Anna Zaksa1b227b2012-02-08 23:16:56 +0000326}
327
328int* mallocEscapeRet() {
329 int *p = malloc(12);
330 return p; // no warning
331}
332
333void mallocEscapeFoo() {
334 int *p = malloc(12);
335 myfoo(p);
336 return; // no warning
337}
338
339void mallocEscapeFree() {
340 int *p = malloc(12);
341 myfoo(p);
342 free(p);
343}
344
345void mallocEscapeFreeFree() {
346 int *p = malloc(12);
347 myfoo(p);
348 free(p);
Anna Zaks546c49c2012-02-16 22:26:12 +0000349 free(p); // expected-warning{{Attempt to free released memory}}
Anna Zaksa1b227b2012-02-08 23:16:56 +0000350}
351
352void mallocEscapeFreeUse() {
353 int *p = malloc(12);
354 myfoo(p);
355 free(p);
Anna Zaks546c49c2012-02-16 22:26:12 +0000356 myfoo(p); // expected-warning{{Use of memory after it is freed}}
Anna Zaksa1b227b2012-02-08 23:16:56 +0000357}
358
359int *myalloc();
360void myalloc2(int **p);
361
362void mallocEscapeFreeCustomAlloc() {
363 int *p = malloc(12);
364 myfoo(p);
365 free(p);
366 p = myalloc();
367 free(p); // no warning
368}
369
370void mallocEscapeFreeCustomAlloc2() {
371 int *p = malloc(12);
372 myfoo(p);
373 free(p);
374 myalloc2(&p);
375 free(p); // no warning
376}
377
378void mallocBindFreeUse() {
379 int *x = malloc(12);
380 int *y = x;
381 free(y);
Anna Zaks546c49c2012-02-16 22:26:12 +0000382 myfoo(x); // expected-warning{{Use of memory after it is freed}}
Anna Zaksa1b227b2012-02-08 23:16:56 +0000383}
384
385void mallocEscapeMalloc() {
386 int *p = malloc(12);
387 myfoo(p);
Jordan Rosee37ab502012-11-15 19:11:43 +0000388 p = malloc(12);
389} // expected-warning{{Memory is never released; potential leak}}
Anna Zaksa1b227b2012-02-08 23:16:56 +0000390
391void mallocMalloc() {
392 int *p = malloc(12);
Jordan Rosee37ab502012-11-15 19:11:43 +0000393 p = malloc(12);
394} // expected-warning {{Memory is never released; potential leak}}
Anna Zaksa1b227b2012-02-08 23:16:56 +0000395
396void mallocFreeMalloc() {
397 int *p = malloc(12);
398 free(p);
399 p = malloc(12);
400 free(p);
401}
402
Anna Zaks12259b42012-02-09 06:25:47 +0000403void mallocFreeUse_params() {
Anna Zaksa1b227b2012-02-08 23:16:56 +0000404 int *p = malloc(12);
405 free(p);
Anna Zaks546c49c2012-02-16 22:26:12 +0000406 myfoo(p); //expected-warning{{Use of memory after it is freed}}
Anna Zaks41b84842012-02-11 23:46:36 +0000407}
408
409void mallocFreeUse_params2() {
410 int *p = malloc(12);
411 free(p);
Anna Zaks546c49c2012-02-16 22:26:12 +0000412 myfooint(*p); //expected-warning{{Use of memory after it is freed}}
Anna Zaksa1b227b2012-02-08 23:16:56 +0000413}
414
Anna Zaks2b5bb972012-02-09 06:25:51 +0000415void mallocFailedOrNot() {
416 int *p = malloc(12);
417 if (!p)
418 free(p);
419 else
420 free(p);
421}
422
Anna Zaks31886862012-02-10 01:11:00 +0000423struct StructWithInt {
424 int g;
425};
Anna Zaks3aa52252012-02-11 21:44:39 +0000426
427int *mallocReturnFreed() {
428 int *p = malloc(12);
429 free(p);
Anna Zaks546c49c2012-02-16 22:26:12 +0000430 return p; // expected-warning {{Use of memory after it is freed}}
Anna Zaks3aa52252012-02-11 21:44:39 +0000431}
432
433int useAfterFreeStruct() {
434 struct StructWithInt *px= malloc(sizeof(struct StructWithInt));
435 px->g = 5;
436 free(px);
Anna Zaks546c49c2012-02-16 22:26:12 +0000437 return px->g; // expected-warning {{Use of memory after it is freed}}
Anna Zaks3aa52252012-02-11 21:44:39 +0000438}
439
Anna Zaks31886862012-02-10 01:11:00 +0000440void nonSymbolAsFirstArg(int *pp, struct StructWithInt *p);
441
442void mallocEscapeFooNonSymbolArg() {
443 struct StructWithInt *p = malloc(sizeof(struct StructWithInt));
444 nonSymbolAsFirstArg(&p->g, p);
445 return; // no warning
446}
447
Anna Zaksbb1ef902012-02-11 21:02:35 +0000448void mallocFailedOrNotLeak() {
449 int *p = malloc(12);
450 if (p == 0)
451 return; // no warning
452 else
Anna Zaksfc2e1532012-03-21 19:45:08 +0000453 return; // expected-warning {{Memory is never released; potential leak}}
Anna Zaksbb1ef902012-02-11 21:02:35 +0000454}
Anna Zaks31886862012-02-10 01:11:00 +0000455
Anna Zaks5a6213d2012-02-15 00:11:28 +0000456void mallocAssignment() {
457 char *p = malloc(12);
Jordan Rosee37ab502012-11-15 19:11:43 +0000458 p = fooRetPtr();
459} // expected-warning {{leak}}
Anna Zaks5a6213d2012-02-15 00:11:28 +0000460
Anna Zaksd5157482012-02-15 00:11:22 +0000461int vallocTest() {
462 char *mem = valloc(12);
Anna Zaksfc2e1532012-03-21 19:45:08 +0000463 return 0; // expected-warning {{Memory is never released; potential leak}}
Anna Zaksd5157482012-02-15 00:11:22 +0000464}
465
466void vallocEscapeFreeUse() {
467 int *p = valloc(12);
468 myfoo(p);
469 free(p);
Anna Zaks546c49c2012-02-16 22:26:12 +0000470 myfoo(p); // expected-warning{{Use of memory after it is freed}}
Anna Zaksd5157482012-02-15 00:11:22 +0000471}
472
Anna Zaks12259b42012-02-09 06:25:47 +0000473int *Gl;
474struct GlStTy {
475 int *x;
476};
477
478struct GlStTy GlS = {0};
479
480void GlobalFree() {
481 free(Gl);
482}
483
484void GlobalMalloc() {
485 Gl = malloc(12);
486}
487
488void GlobalStructMalloc() {
489 int *a = malloc(12);
490 GlS.x = a;
491}
492
493void GlobalStructMallocFree() {
494 int *a = malloc(12);
495 GlS.x = a;
496 free(GlS.x);
497}
Anna Zakse963fd52012-02-10 01:11:03 +0000498
Anna Zaks33c364b2012-02-16 22:26:15 +0000499char *ArrayG[12];
500
501void globalArrayTest() {
502 char *p = (char*)malloc(12);
503 ArrayG[0] = p;
504}
505
Anna Zaksd32ead82012-02-16 03:40:57 +0000506// Make sure that we properly handle a pointer stored into a local struct/array.
507typedef struct _StructWithPtr {
508 int *memP;
509} StructWithPtr;
510
511static StructWithPtr arrOfStructs[10];
512
513void testMalloc() {
514 int *x = malloc(12);
515 StructWithPtr St;
516 St.memP = x;
Jordan Rose356279c2012-08-08 18:23:31 +0000517 arrOfStructs[0] = St; // no-warning
Anna Zaksd32ead82012-02-16 03:40:57 +0000518}
519
520StructWithPtr testMalloc2() {
521 int *x = malloc(12);
522 StructWithPtr St;
523 St.memP = x;
Jordan Rose356279c2012-08-08 18:23:31 +0000524 return St; // no-warning
Anna Zaksd32ead82012-02-16 03:40:57 +0000525}
526
527int *testMalloc3() {
528 int *x = malloc(12);
529 int *y = x;
Jordan Rose356279c2012-08-08 18:23:31 +0000530 return y; // no-warning
Anna Zaksd32ead82012-02-16 03:40:57 +0000531}
532
Jordan Rosed86b3bd2012-08-08 18:23:36 +0000533void testStructLeak() {
534 StructWithPtr St;
535 St.memP = malloc(12);
536 return; // expected-warning {{Memory is never released; potential leak of memory pointed to by 'St.memP'}}
537}
538
Anna Zaks1fdedc92012-02-17 22:35:34 +0000539void testElemRegion1() {
540 char *x = (void*)malloc(2);
541 int *ix = (int*)x;
542 free(&(x[0]));
543}
544
545void testElemRegion2(int **pp) {
546 int *p = malloc(12);
547 *pp = p;
548 free(pp[0]);
549}
550
551void testElemRegion3(int **pp) {
552 int *p = malloc(12);
553 *pp = p;
554 free(*pp);
555}
Anna Zaksbb1ef902012-02-11 21:02:35 +0000556// Region escape testing.
557
558unsigned takePtrToPtr(int **p);
559void PassTheAddrOfAllocatedData(int f) {
560 int *p = malloc(12);
561 // We don't know what happens after the call. Should stop tracking here.
562 if (takePtrToPtr(&p))
563 f++;
564 free(p); // no warning
565}
566
567struct X {
568 int *p;
569};
570unsigned takePtrToStruct(struct X *s);
571int ** foo2(int *g, int f) {
572 int *p = malloc(12);
573 struct X *px= malloc(sizeof(struct X));
574 px->p = p;
575 // We don't know what happens after this call. Should not track px nor p.
576 if (takePtrToStruct(px))
577 f++;
578 free(p);
579 return 0;
580}
581
582struct X* RegInvalidationDetect1(struct X *s2) {
583 struct X *px= malloc(sizeof(struct X));
584 px->p = 0;
585 px = s2;
Anna Zaksfc2e1532012-03-21 19:45:08 +0000586 return px; // expected-warning {{Memory is never released; potential leak}}
Anna Zaksbb1ef902012-02-11 21:02:35 +0000587}
588
589struct X* RegInvalidationGiveUp1() {
590 int *p = malloc(12);
591 struct X *px= malloc(sizeof(struct X));
592 px->p = p;
593 return px;
594}
595
596int **RegInvalidationDetect2(int **pp) {
597 int *p = malloc(12);
598 pp = &p;
599 pp++;
Anna Zaksfc2e1532012-03-21 19:45:08 +0000600 return 0;// expected-warning {{Memory is never released; potential leak}}
Anna Zaksbb1ef902012-02-11 21:02:35 +0000601}
Anna Zakse963fd52012-02-10 01:11:03 +0000602
Anna Zakse963fd52012-02-10 01:11:03 +0000603extern void exit(int) __attribute__ ((__noreturn__));
604void mallocExit(int *g) {
605 struct xx *p = malloc(12);
Anna Zaksd3571e5a2012-02-11 21:02:40 +0000606 if (g != 0)
607 exit(1);
Anna Zakse963fd52012-02-10 01:11:03 +0000608 free(p);
609 return;
610}
611
Anna Zakse963fd52012-02-10 01:11:03 +0000612extern void __assert_fail (__const char *__assertion, __const char *__file,
613 unsigned int __line, __const char *__function)
614 __attribute__ ((__noreturn__));
615#define assert(expr) \
616 ((expr) ? (void)(0) : __assert_fail (#expr, __FILE__, __LINE__, __func__))
617void mallocAssert(int *g) {
618 struct xx *p = malloc(12);
619
Anna Zaksd3571e5a2012-02-11 21:02:40 +0000620 assert(g != 0);
Anna Zakse963fd52012-02-10 01:11:03 +0000621 free(p);
622 return;
623}
624
Anna Zaks41b84842012-02-11 23:46:36 +0000625void doNotInvalidateWhenPassedToSystemCalls(char *s) {
626 char *p = malloc(12);
627 strlen(p);
Jordan Rosee37ab502012-11-15 19:11:43 +0000628 strcpy(p, s);
629} // expected-warning {{leak}}
Anna Zaks41b84842012-02-11 23:46:36 +0000630
Anna Zakse56167e2012-02-17 22:35:31 +0000631// Rely on the CString checker evaluation of the strcpy API to convey that the result of strcpy is equal to p.
632void symbolLostWithStrcpy(char *s) {
633 char *p = malloc(12);
634 p = strcpy(p, s);
635 free(p);
636}
637
638
639// The same test as the one above, but with what is actually generated on a mac.
640static __inline char *
641__inline_strcpy_chk (char *restrict __dest, const char *restrict __src)
642{
643 return __builtin___strcpy_chk (__dest, __src, __builtin_object_size (__dest, 2 > 1));
644}
645
646void symbolLostWithStrcpy_InlineStrcpyVersion(char *s) {
647 char *p = malloc(12);
648 p = ((__builtin_object_size (p, 0) != (size_t) -1) ? __builtin___strcpy_chk (p, s, __builtin_object_size (p, 2 > 1)) : __inline_strcpy_chk (p, s));
649 free(p);
650}
Anna Zaks4ca45b12012-02-22 02:36:01 +0000651
652// Here we are returning a pointer one past the allocated value. An idiom which
653// can be used for implementing special malloc. The correct uses of this might
654// be rare enough so that we could keep this as a warning.
655static void *specialMalloc(int n){
656 int *p;
657 p = malloc( n+8 );
658 if( p ){
659 p[0] = n;
660 p++;
661 }
662 return p;
663}
664
665// Potentially, the user could free the struct by performing pointer arithmetic on the return value.
666// This is a variation of the specialMalloc issue, though probably would be more rare in correct code.
667int *specialMallocWithStruct() {
668 struct StructWithInt *px= malloc(sizeof(struct StructWithInt));
669 return &(px->g);
670}
671
Anna Zaks199e8e52012-02-22 03:14:20 +0000672// Test various allocation/deallocation functions.
Anna Zaks199e8e52012-02-22 03:14:20 +0000673void testStrdup(const char *s, unsigned validIndex) {
674 char *s2 = strdup(s);
Jordan Rosee37ab502012-11-15 19:11:43 +0000675 s2[validIndex + 1] = 'b';
676} // expected-warning {{Memory is never released; potential leak}}
Anna Zaks199e8e52012-02-22 03:14:20 +0000677
678int testStrndup(const char *s, unsigned validIndex, unsigned size) {
679 char *s2 = strndup(s, size);
680 s2 [validIndex + 1] = 'b';
681 if (s2[validIndex] != 'a')
Anna Zaksdf901a42012-02-23 21:38:21 +0000682 return 0;
Anna Zaks199e8e52012-02-22 03:14:20 +0000683 else
Anna Zaksfc2e1532012-03-21 19:45:08 +0000684 return 1;// expected-warning {{Memory is never released; potential leak}}
Anna Zaks199e8e52012-02-22 03:14:20 +0000685}
686
Anna Zaks40a7eb32012-02-22 19:24:52 +0000687void testStrdupContentIsDefined(const char *s, unsigned validIndex) {
688 char *s2 = strdup(s);
689 char result = s2[1];// no warning
690 free(s2);
691}
692
Anna Zakse0c03ca2012-02-29 18:42:47 +0000693// ----------------------------------------------------------------------------
Anna Zaks07de9c12012-02-23 01:05:27 +0000694// Test the system library functions to which the pointer can escape.
Anna Zakse0c03ca2012-02-29 18:42:47 +0000695// This tests false positive suppression.
Anna Zaks07de9c12012-02-23 01:05:27 +0000696
697// For now, we assume memory passed to pthread_specific escapes.
698// TODO: We could check that if a new pthread binding is set, the existing
699// binding must be freed; otherwise, a memory leak can occur.
700void testPthereadSpecificEscape(pthread_key_t key) {
701 void *buf = malloc(12);
702 pthread_setspecific(key, buf); // no warning
703}
704
Anna Zakse0c03ca2012-02-29 18:42:47 +0000705// PR12101: Test funopen().
706static int releasePtr(void *_ctx) {
707 free(_ctx);
708 return 0;
709}
710FILE *useFunOpen() {
711 void *ctx = malloc(sizeof(int));
712 FILE *f = funopen(ctx, 0, 0, 0, releasePtr); // no warning
713 if (f == 0) {
714 free(ctx);
715 }
716 return f;
717}
718FILE *useFunOpenNoReleaseFunction() {
719 void *ctx = malloc(sizeof(int));
720 FILE *f = funopen(ctx, 0, 0, 0, 0);
721 if (f == 0) {
722 free(ctx);
723 }
724 return f; // expected-warning{{leak}}
725}
726
Jordan Rose7ab01822012-07-02 19:27:51 +0000727static int readNothing(void *_ctx, char *buf, int size) {
728 return 0;
729}
730FILE *useFunOpenReadNoRelease() {
731 void *ctx = malloc(sizeof(int));
732 FILE *f = funopen(ctx, readNothing, 0, 0, 0);
733 if (f == 0) {
734 free(ctx);
735 }
736 return f; // expected-warning{{leak}}
737}
738
Anna Zakse0c03ca2012-02-29 18:42:47 +0000739// Test setbuf, setvbuf.
740int my_main_no_warning() {
741 char *p = malloc(100);
742 setvbuf(stdout, p, 0, 100);
743 return 0;
744}
745int my_main_no_warning2() {
746 char *p = malloc(100);
747 setbuf(__stdoutp, p);
748 return 0;
749}
750int my_main_warn(FILE *f) {
751 char *p = malloc(100);
752 setvbuf(f, p, 0, 100);
753 return 0;// expected-warning {{leak}}
754}
755
Ted Kremenek9d96f842012-03-05 23:06:19 +0000756// <rdar://problem/10978247>.
757// some people use stack allocated memory as an optimization to avoid
758// a heap allocation for small work sizes. This tests the analyzer's
759// understanding that the malloc'ed memory is not the same as stackBuffer.
760void radar10978247(int myValueSize) {
761 char stackBuffer[128];
762 char *buffer;
763
764 if (myValueSize <= sizeof(stackBuffer))
765 buffer = stackBuffer;
766 else
767 buffer = malloc(myValueSize);
768
769 // do stuff with the buffer
770 if (buffer != stackBuffer)
771 free(buffer);
772}
773
774void radar10978247_positive(int myValueSize) {
775 char stackBuffer[128];
776 char *buffer;
777
778 if (myValueSize <= sizeof(stackBuffer))
779 buffer = stackBuffer;
780 else
781 buffer = malloc(myValueSize);
782
783 // do stuff with the buffer
Jordan Rosee37ab502012-11-15 19:11:43 +0000784 if (buffer == stackBuffer)
Ted Kremenek9d96f842012-03-05 23:06:19 +0000785 return;
Jordan Rosee37ab502012-11-15 19:11:43 +0000786 else
787 return; // expected-warning {{leak}}
788}
Ted Kremenek468365b2012-04-26 05:08:26 +0000789// <rdar://problem/11269741> Previously this triggered a false positive
790// because malloc() is known to return uninitialized memory and the binding
791// of 'o' to 'p->n' was not getting propertly handled. Now we report a leak.
792struct rdar11269741_a_t {
793 struct rdar11269741_b_t {
794 int m;
795 } n;
796};
797
798int rdar11269741(struct rdar11269741_b_t o)
799{
800 struct rdar11269741_a_t *p = (struct rdar11269741_a_t *) malloc(sizeof(*p));
801 p->n = o;
802 return p->n.m; // expected-warning {{leak}}
803}
804
Anna Zaks1655aee2012-05-03 02:13:56 +0000805// Pointer arithmetic, returning an ElementRegion.
806void *radar11329382(unsigned bl) {
807 void *ptr = malloc (16);
808 ptr = ptr + (2 - bl);
809 return ptr; // no warning
810}
811
Anna Zaks84d70a92012-05-01 21:10:29 +0000812void __assert_rtn(const char *, const char *, int, const char *) __attribute__((__noreturn__));
813int strcmp(const char *, const char *);
814char *a (void);
815void radar11270219(void) {
816 char *x = a(), *y = a();
817 (__builtin_expect(!(x && y), 0) ? __assert_rtn(__func__, "/Users/zaks/tmp/ex.c", 24, "x && y") : (void)0);
818 strcmp(x, y); // no warning
819}
820
Anna Zaks263b7e02012-05-02 00:05:20 +0000821void radar_11358224_test_double_assign_ints_positive_2()
822{
823 void *ptr = malloc(16);
Jordan Rosee37ab502012-11-15 19:11:43 +0000824 ptr = ptr;
825} // expected-warning {{leak}}
Anna Zaks263b7e02012-05-02 00:05:20 +0000826
Anna Zaks228f9c72012-05-03 23:50:28 +0000827// Assume that functions which take a function pointer can free memory even if
828// they are defined in system headers and take the const pointer to the
829// allocated memory. (radar://11160612)
830int const_ptr_and_callback(int, const char*, int n, void(*)(void*));
831void r11160612_1() {
832 char *x = malloc(12);
833 const_ptr_and_callback(0, x, 12, free); // no - warning
834}
835
836// Null is passed as callback.
837void r11160612_2() {
838 char *x = malloc(12);
Jordan Rosee37ab502012-11-15 19:11:43 +0000839 const_ptr_and_callback(0, x, 12, 0);
840} // expected-warning {{leak}}
Anna Zaks228f9c72012-05-03 23:50:28 +0000841
842// Callback is passed to a function defined in a system header.
843void r11160612_4() {
844 char *x = malloc(12);
845 sqlite3_bind_text_my(0, x, 12, free); // no - warning
846}
847
Anna Zaks6ccfcf32012-05-03 23:50:33 +0000848// Passing callbacks in a struct.
849void r11160612_5(StWithCallback St) {
850 void *x = malloc(12);
851 dealocateMemWhenDoneByVal(x, St);
852}
853void r11160612_6(StWithCallback St) {
854 void *x = malloc(12);
855 dealocateMemWhenDoneByRef(&St, x);
856}
857
Anna Zaks63509fb2012-05-04 17:37:16 +0000858int mySub(int, int);
859int myAdd(int, int);
860int fPtr(unsigned cond, int x) {
861 return (cond ? mySub : myAdd)(x, x);
862}
863
Anna Zaks3563fde2012-06-07 03:57:32 +0000864// Test anti-aliasing.
Anna Zaksd3571e5a2012-02-11 21:02:40 +0000865
Anna Zakse963fd52012-02-10 01:11:03 +0000866void dependsOnValueOfPtr(int *g, unsigned f) {
867 int *p;
868
869 if (f) {
870 p = g;
871 } else {
872 p = malloc(12);
873 }
874
875 if (p != g)
876 free(p);
877 else
Anna Zaks3563fde2012-06-07 03:57:32 +0000878 return; // no warning
Anna Zakse963fd52012-02-10 01:11:03 +0000879 return;
880}
881
Anna Zaks3563fde2012-06-07 03:57:32 +0000882int CMPRegionHeapToStack() {
883 int x = 0;
884 int *x1 = malloc(8);
885 int *x2 = &x;
Anna Zaks93205d02012-06-08 00:04:40 +0000886 clang_analyzer_eval(x1 == x2); // expected-warning{{FALSE}}
Anna Zaks3563fde2012-06-07 03:57:32 +0000887 free(x1);
888 return x;
889}
890
891int CMPRegionHeapToHeap2() {
892 int x = 0;
893 int *x1 = malloc(8);
894 int *x2 = malloc(8);
895 int *x4 = x1;
896 int *x5 = x2;
Anna Zaks93205d02012-06-08 00:04:40 +0000897 clang_analyzer_eval(x4 == x5); // expected-warning{{FALSE}}
Anna Zaks3563fde2012-06-07 03:57:32 +0000898 free(x1);
899 free(x2);
900 return x;
901}
902
903int CMPRegionHeapToHeap() {
904 int x = 0;
905 int *x1 = malloc(8);
906 int *x4 = x1;
907 if (x1 == x4) {
908 free(x1);
909 return 5/x; // expected-warning{{Division by zero}}
910 }
911 return x;// expected-warning{{This statement is never executed}}
912}
913
914int HeapAssignment() {
915 int m = 0;
916 int *x = malloc(4);
917 int *y = x;
918 *x = 5;
Anna Zaks93205d02012-06-08 00:04:40 +0000919 clang_analyzer_eval(*x != *y); // expected-warning{{FALSE}}
Anna Zaks3563fde2012-06-07 03:57:32 +0000920 free(x);
921 return 0;
922}
923
Anna Zaksa7dcc992012-06-07 20:18:08 +0000924int *retPtr();
925int *retPtrMightAlias(int *x);
926int cmpHeapAllocationToUnknown() {
927 int zero = 0;
928 int *yBefore = retPtr();
929 int *m = malloc(8);
930 int *yAfter = retPtrMightAlias(m);
Anna Zaks93205d02012-06-08 00:04:40 +0000931 clang_analyzer_eval(yBefore == m); // expected-warning{{FALSE}}
932 clang_analyzer_eval(yAfter == m); // expected-warning{{FALSE}}
Anna Zaksa7dcc992012-06-07 20:18:08 +0000933 free(m);
934 return 0;
935}
936
Anna Zaks33c364b2012-02-16 22:26:15 +0000937void localArrayTest() {
938 char *p = (char*)malloc(12);
939 char *ArrayL[12];
Jordan Rosee37ab502012-11-15 19:11:43 +0000940 ArrayL[0] = p;
941} // expected-warning {{leak}}
Jordan Rose356279c2012-08-08 18:23:31 +0000942
943void localStructTest() {
944 StructWithPtr St;
945 StructWithPtr *pSt = &St;
Jordan Rosee37ab502012-11-15 19:11:43 +0000946 pSt->memP = malloc(12);
947} // expected-warning{{Memory is never released; potential leak}}
Anna Zaks33c364b2012-02-16 22:26:15 +0000948
Jordan Rose3ba8c792012-11-27 02:37:49 +0000949#ifdef __INTPTR_TYPE__
Ted Kremenekf56d4f22012-05-01 21:58:29 +0000950// Test double assignment through integers.
Jordan Rose3ba8c792012-11-27 02:37:49 +0000951typedef __INTPTR_TYPE__ intptr_t;
952typedef unsigned __INTPTR_TYPE__ uintptr_t;
953
954static intptr_t glob;
Ted Kremenekf56d4f22012-05-01 21:58:29 +0000955void test_double_assign_ints()
956{
957 void *ptr = malloc (16); // no-warning
Jordan Rose3ba8c792012-11-27 02:37:49 +0000958 glob = (intptr_t)(uintptr_t)ptr;
Ted Kremenekf56d4f22012-05-01 21:58:29 +0000959}
960
961void test_double_assign_ints_positive()
962{
963 void *ptr = malloc(16);
Jordan Rose3ba8c792012-11-27 02:37:49 +0000964 (void*)(intptr_t)(uintptr_t)ptr; // expected-warning {{unused}}
Jordan Rosee37ab502012-11-15 19:11:43 +0000965} // expected-warning {{leak}}
Jordan Rose3ba8c792012-11-27 02:37:49 +0000966#endif
Jordan Rosede409b62012-06-16 00:09:20 +0000967
968void testCGContextNoLeak()
969{
970 void *ptr = malloc(16);
971 CGContextRef context = CGBitmapContextCreate(ptr);
972
973 // Because you can get the data back out like this, even much later,
974 // CGBitmapContextCreate is one of our "stop-tracking" exceptions.
975 free(CGBitmapContextGetData(context));
976}
977
978void testCGContextLeak()
979{
980 void *ptr = malloc(16);
981 CGContextRef context = CGBitmapContextCreate(ptr);
982 // However, this time we're just leaking the data, because the context
983 // object doesn't escape and it hasn't been freed in this function.
984}
985
Anna Zaks886dfb82012-06-20 23:35:57 +0000986// Allow xpc context to escape. radar://11635258
987// TODO: Would be great if we checked that the finalize_connection_context actually releases it.
988static void finalize_connection_context(void *ctx) {
989 int *context = ctx;
990 free(context);
991}
992void foo (xpc_connection_t peer) {
993 int *ctx = calloc(1, sizeof(int));
994 xpc_connection_set_context(peer, ctx);
995 xpc_connection_set_finalizer_f(peer, finalize_connection_context);
996 xpc_connection_resume(peer);
997}
998
Anna Zaks52242a62012-08-03 18:30:18 +0000999// Make sure we catch errors when we free in a function which does not allocate memory.
1000void freeButNoMalloc(int *p, int x){
1001 if (x) {
1002 free(p);
1003 //user forgot a return here.
1004 }
1005 free(p); // expected-warning {{Attempt to free released memory}}
1006}
Anna Zaks6ce686e2012-08-04 02:04:27 +00001007
1008struct HasPtr {
Anna Zaksfe6eb672012-08-24 02:28:20 +00001009 char *p;
Anna Zaks6ce686e2012-08-04 02:04:27 +00001010};
1011
Anna Zaksfe6eb672012-08-24 02:28:20 +00001012char* reallocButNoMalloc(struct HasPtr *a, int c, int size) {
Anna Zaks6ce686e2012-08-04 02:04:27 +00001013 int *s;
Anna Zaksfe6eb672012-08-24 02:28:20 +00001014 char *b = realloc(a->p, size);
1015 char *m = realloc(a->p, size); // expected-warning {{Attempt to free released memory}}
Anna Zaks6ce686e2012-08-04 02:04:27 +00001016 return a->p;
1017}
Jordan Rose356279c2012-08-08 18:23:31 +00001018
Anna Zaksfe6eb672012-08-24 02:28:20 +00001019// We should not warn in this case since the caller will presumably free a->p in all cases.
1020int reallocButNoMallocPR13674(struct HasPtr *a, int c, int size) {
1021 int *s;
1022 char *b = realloc(a->p, size);
1023 if (b == 0)
1024 return -1;
1025 a->p = b;
1026 return 0;
1027}
1028
Anna Zaks75cfbb62012-09-12 22:57:34 +00001029// Test realloc with no visible malloc.
1030void *test(void *ptr) {
1031 void *newPtr = realloc(ptr, 4);
1032 if (newPtr == 0) {
1033 if (ptr)
1034 free(ptr); // no-warning
1035 }
1036 return newPtr;
1037}
1038
Jordan Roseb5b0fc12012-11-15 19:11:27 +00001039
1040char *testLeakWithinReturn(char *str) {
1041 return strdup(strdup(str)); // expected-warning{{leak}}
1042}
1043
Jordan Rose356279c2012-08-08 18:23:31 +00001044// ----------------------------------------------------------------------------
1045// False negatives.
1046
1047// TODO: This is another false negative.
1048void testMallocWithParam(int **p) {
1049 *p = (int*) malloc(sizeof(int));
1050 *p = 0;
1051}
1052
1053void testMallocWithParam_2(int **p) {
1054 *p = (int*) malloc(sizeof(int));
1055}