blob: f60271f39f4c290fa14b9caa2b938c73aca21eb3 [file] [log] [blame]
Anna Zaksadccc3f2012-06-08 00:04:40 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=core,experimental.deadcode.UnreachableCode,experimental.core.CastSize,unix.Malloc,debug.ExprInspection -analyzer-store=region -verify %s
Anna Zaks15d0ae12012-02-11 23:46:36 +00002#include "system-header-simulator.h"
3
Anna Zaksadccc3f2012-06-08 00:04:40 +00004void clang_analyzer_eval(int);
5
Eli Friedman2f005522009-11-14 04:23:25 +00006typedef __typeof(sizeof(int)) size_t;
Ted Kremenekc3607752009-11-13 20:03:22 +00007void *malloc(size_t);
Anna Zaksb16ce452012-02-15 00:11:22 +00008void *valloc(size_t);
Ted Kremenekc3607752009-11-13 20:03:22 +00009void free(void *);
Zhongxing Xud9c84c82009-12-12 12:29:38 +000010void *realloc(void *ptr, size_t size);
Anna Zaks40add292012-02-15 00:11:25 +000011void *reallocf(void *ptr, size_t size);
Zhongxing Xud9c84c82009-12-12 12:29:38 +000012void *calloc(size_t nmemb, size_t size);
Anna Zaks14345182012-05-18 01:16:10 +000013char *strdup(const char *s);
14char *strndup(const char *s, size_t n);
Ted Kremenekdd0e4902010-07-31 01:52:11 +000015
Anna Zaks91c2a112012-02-08 23:16:56 +000016void myfoo(int *p);
17void myfooint(int p);
Anna Zaksebc1d322012-02-15 00:11:28 +000018char *fooRetPtr();
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +000019
20void f1() {
Zhongxing Xuab280992010-05-25 04:59:19 +000021 int *p = malloc(12);
Jordan Rose919e8a12012-08-08 18:23:36 +000022 return; // expected-warning{{Memory is never released; potential leak of memory pointed to by 'p'}}
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +000023}
24
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +000025void f2() {
Zhongxing Xuab280992010-05-25 04:59:19 +000026 int *p = malloc(12);
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +000027 free(p);
Anna Zaksfebdc322012-02-16 22:26:12 +000028 free(p); // expected-warning{{Attempt to free released memory}}
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +000029}
Ted Kremenekc764d4b2009-11-13 20:00:28 +000030
Lenny Maiorani4d8d8032011-04-27 14:49:29 +000031void f2_realloc_0() {
32 int *p = malloc(12);
33 realloc(p,0);
Anna Zaksfebdc322012-02-16 22:26:12 +000034 realloc(p,0); // expected-warning{{Attempt to free released memory}}
Lenny Maiorani4d8d8032011-04-27 14:49:29 +000035}
36
37void f2_realloc_1() {
38 int *p = malloc(12);
Zhongxing Xud56763f2011-09-01 04:53:59 +000039 int *q = realloc(p,0); // no-warning
Lenny Maiorani4d8d8032011-04-27 14:49:29 +000040}
41
Anna Zaksc8bb3be2012-02-13 18:05:39 +000042void reallocNotNullPtr(unsigned sizeIn) {
43 unsigned size = 12;
44 char *p = (char*)malloc(size);
45 if (p) {
46 char *q = (char*)realloc(p, sizeIn);
Jordan Rose919e8a12012-08-08 18:23:36 +000047 char x = *q; // expected-warning {{Memory is never released; potential leak of memory pointed to by 'q'}}
Anna Zaksc8bb3be2012-02-13 18:05:39 +000048 }
49}
50
51int *realloctest1() {
52 int *q = malloc(12);
53 q = realloc(q, 20);
54 return q; // no warning - returning the allocated value
55}
56
57// p should be freed if realloc fails.
58void reallocFails() {
59 char *p = malloc(12);
60 char *r = realloc(p, 12+1);
61 if (!r) {
62 free(p);
63 } else {
64 free(r);
65 }
66}
67
Anna Zaks30838b92012-02-13 20:57:07 +000068void reallocSizeZero1() {
69 char *p = malloc(12);
70 char *r = realloc(p, 0);
71 if (!r) {
Anna Zaksede875b2012-08-03 18:30:18 +000072 free(p); // expected-warning {{Attempt to free released memory}}
Anna Zaks30838b92012-02-13 20:57:07 +000073 } else {
74 free(r);
75 }
76}
77
78void reallocSizeZero2() {
79 char *p = malloc(12);
80 char *r = realloc(p, 0);
81 if (!r) {
Anna Zaksede875b2012-08-03 18:30:18 +000082 free(p); // expected-warning {{Attempt to free released memory}}
Anna Zaks30838b92012-02-13 20:57:07 +000083 } else {
84 free(r);
85 }
Anna Zaksfebdc322012-02-16 22:26:12 +000086 free(p); // expected-warning {{Attempt to free released memory}}
Anna Zaks30838b92012-02-13 20:57:07 +000087}
88
89void reallocSizeZero3() {
90 char *p = malloc(12);
91 char *r = realloc(p, 0);
92 free(r);
93}
94
95void reallocSizeZero4() {
96 char *r = realloc(0, 0);
97 free(r);
98}
99
100void reallocSizeZero5() {
101 char *r = realloc(0, 0);
102}
103
104void reallocPtrZero1() {
Jordan Rose919e8a12012-08-08 18:23:36 +0000105 char *r = realloc(0, 12); // expected-warning {{Memory is never released; potential leak of memory pointed to by 'r'}}
Anna Zaks30838b92012-02-13 20:57:07 +0000106}
107
108void reallocPtrZero2() {
109 char *r = realloc(0, 12);
110 if (r)
111 free(r);
112}
113
114void reallocPtrZero3() {
115 char *r = realloc(0, 12);
116 free(r);
117}
118
Anna Zaksb276bd92012-02-14 00:26:13 +0000119void reallocRadar6337483_1() {
120 char *buf = malloc(100);
121 buf = (char*)realloc(buf, 0x1000000);
122 if (!buf) {
Anna Zaks3d7c44e2012-03-21 19:45:08 +0000123 return;// expected-warning {{Memory is never released; potential leak}}
Anna Zaksb276bd92012-02-14 00:26:13 +0000124 }
125 free(buf);
126}
127
128void reallocRadar6337483_2() {
129 char *buf = malloc(100);
130 char *buf2 = (char*)realloc(buf, 0x1000000);
Anna Zaks3d7c44e2012-03-21 19:45:08 +0000131 if (!buf2) { // expected-warning {{Memory is never released; potential leak}}
Anna Zaksb276bd92012-02-14 00:26:13 +0000132 ;
133 } else {
134 free(buf2);
135 }
136}
137
138void reallocRadar6337483_3() {
139 char * buf = malloc(100);
140 char * tmp;
141 tmp = (char*)realloc(buf, 0x1000000);
142 if (!tmp) {
143 free(buf);
144 return;
145 }
146 buf = tmp;
147 free(buf);
148}
149
150void reallocRadar6337483_4() {
151 char *buf = malloc(100);
152 char *buf2 = (char*)realloc(buf, 0x1000000);
153 if (!buf2) {
Anna Zaks3d7c44e2012-03-21 19:45:08 +0000154 return; // expected-warning {{Memory is never released; potential leak}}
Anna Zaksb276bd92012-02-14 00:26:13 +0000155 } else {
156 free(buf2);
157 }
158}
159
Anna Zaks40add292012-02-15 00:11:25 +0000160int *reallocfTest1() {
161 int *q = malloc(12);
162 q = reallocf(q, 20);
163 return q; // no warning - returning the allocated value
164}
165
166void reallocfRadar6337483_4() {
167 char *buf = malloc(100);
168 char *buf2 = (char*)reallocf(buf, 0x1000000);
169 if (!buf2) {
170 return; // no warning - reallocf frees even on failure
171 } else {
172 free(buf2);
173 }
174}
175
176void reallocfRadar6337483_3() {
177 char * buf = malloc(100);
178 char * tmp;
179 tmp = (char*)reallocf(buf, 0x1000000);
180 if (!tmp) {
Anna Zaksfebdc322012-02-16 22:26:12 +0000181 free(buf); // expected-warning {{Attempt to free released memory}}
Anna Zaks40add292012-02-15 00:11:25 +0000182 return;
183 }
184 buf = tmp;
185 free(buf);
186}
187
188void reallocfPtrZero1() {
Anna Zaks3d7c44e2012-03-21 19:45:08 +0000189 char *r = reallocf(0, 12); // expected-warning {{Memory is never released; potential leak}}
Anna Zaks40add292012-02-15 00:11:25 +0000190}
191
192
Zhongxing Xu243fde92009-11-17 07:54:15 +0000193// This case tests that storing malloc'ed memory to a static variable which is
194// then returned is not leaked. In the absence of known contracts for functions
195// or inter-procedural analysis, this is a conservative answer.
Ted Kremenekc764d4b2009-11-13 20:00:28 +0000196int *f3() {
197 static int *p = 0;
Zhongxing Xuab280992010-05-25 04:59:19 +0000198 p = malloc(12);
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000199 return p; // no-warning
Ted Kremenekc764d4b2009-11-13 20:00:28 +0000200}
201
Zhongxing Xu243fde92009-11-17 07:54:15 +0000202// This case tests that storing malloc'ed memory to a static global variable
203// which is then returned is not leaked. In the absence of known contracts for
204// functions or inter-procedural analysis, this is a conservative answer.
Ted Kremenekc764d4b2009-11-13 20:00:28 +0000205static int *p_f4 = 0;
206int *f4() {
Zhongxing Xuab280992010-05-25 04:59:19 +0000207 p_f4 = malloc(12);
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000208 return p_f4; // no-warning
Ted Kremenekc764d4b2009-11-13 20:00:28 +0000209}
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000210
211int *f5() {
Zhongxing Xuab280992010-05-25 04:59:19 +0000212 int *q = malloc(12);
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000213 q = realloc(q, 20);
214 return q; // no-warning
215}
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000216
217void f6() {
Zhongxing Xuab280992010-05-25 04:59:19 +0000218 int *p = malloc(12);
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000219 if (!p)
220 return; // no-warning
221 else
222 free(p);
223}
Zhongxing Xu425c7ed2010-01-18 04:01:40 +0000224
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000225void f6_realloc() {
226 int *p = malloc(12);
227 if (!p)
228 return; // no-warning
229 else
230 realloc(p,0);
231}
232
233
Zhongxing Xu425c7ed2010-01-18 04:01:40 +0000234char *doit2();
235void pr6069() {
236 char *buf = doit2();
237 free(buf);
238}
Zhongxing Xu181cc3d2010-02-14 06:49:48 +0000239
240void pr6293() {
241 free(0);
242}
Zhongxing Xuc8023782010-03-10 04:58:55 +0000243
244void f7() {
245 char *x = (char*) malloc(4);
246 free(x);
Anna Zaksfebdc322012-02-16 22:26:12 +0000247 x[0] = 'a'; // expected-warning{{Use of memory after it is freed}}
Zhongxing Xuc8023782010-03-10 04:58:55 +0000248}
Zhongxing Xuab280992010-05-25 04:59:19 +0000249
Anna Zaks14345182012-05-18 01:16:10 +0000250void f8() {
251 char *x = (char*) malloc(4);
252 free(x);
253 char *y = strndup(x, 4); // expected-warning{{Use of memory after it is freed}}
254}
255
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000256void f7_realloc() {
257 char *x = (char*) malloc(4);
258 realloc(x,0);
Anna Zaksfebdc322012-02-16 22:26:12 +0000259 x[0] = 'a'; // expected-warning{{Use of memory after it is freed}}
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000260}
261
Zhongxing Xuab280992010-05-25 04:59:19 +0000262void PR6123() {
263 int *x = malloc(11); // expected-warning{{Cast a region whose size is not a multiple of the destination type size.}}
264}
265
266void PR7217() {
267 int *buf = malloc(2); // expected-warning{{Cast a region whose size is not a multiple of the destination type size.}}
268 buf[1] = 'c'; // not crash
Zhongxing Xuab280992010-05-25 04:59:19 +0000269}
Jordy Rosec580f2e2010-06-20 04:30:57 +0000270
271void mallocCastToVoid() {
272 void *p = malloc(2);
273 const void *cp = p; // not crash
274 free(p);
275}
276
277void mallocCastToFP() {
278 void *p = malloc(2);
279 void (*fp)() = p; // not crash
280 free(p);
281}
282
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000283// This tests that malloc() buffers are undefined by default
284char mallocGarbage () {
285 char *buf = malloc(2);
286 char result = buf[1]; // expected-warning{{undefined}}
287 free(buf);
288 return result;
289}
290
291// This tests that calloc() buffers need to be freed
292void callocNoFree () {
293 char *buf = calloc(2,2);
294 return; // expected-warning{{never released}}
295}
296
297// These test that calloc() buffers are zeroed by default
298char callocZeroesGood () {
299 char *buf = calloc(2,2);
300 char result = buf[3]; // no-warning
301 if (buf[1] == 0) {
302 free(buf);
303 }
304 return result; // no-warning
305}
306
307char callocZeroesBad () {
308 char *buf = calloc(2,2);
309 char result = buf[3]; // no-warning
310 if (buf[1] != 0) {
Tom Carec4b5bd82010-07-23 23:04:53 +0000311 free(buf); // expected-warning{{never executed}}
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000312 }
313 return result; // expected-warning{{never released}}
314}
Anna Zaks91c2a112012-02-08 23:16:56 +0000315
316void nullFree() {
317 int *p = 0;
318 free(p); // no warning - a nop
319}
320
321void paramFree(int *p) {
322 myfoo(p);
323 free(p); // no warning
Anna Zaksede875b2012-08-03 18:30:18 +0000324 myfoo(p); // expected-warning {{Use of memory after it is freed}}
Anna Zaks91c2a112012-02-08 23:16:56 +0000325}
326
327int* mallocEscapeRet() {
328 int *p = malloc(12);
329 return p; // no warning
330}
331
332void mallocEscapeFoo() {
333 int *p = malloc(12);
334 myfoo(p);
335 return; // no warning
336}
337
338void mallocEscapeFree() {
339 int *p = malloc(12);
340 myfoo(p);
341 free(p);
342}
343
344void mallocEscapeFreeFree() {
345 int *p = malloc(12);
346 myfoo(p);
347 free(p);
Anna Zaksfebdc322012-02-16 22:26:12 +0000348 free(p); // expected-warning{{Attempt to free released memory}}
Anna Zaks91c2a112012-02-08 23:16:56 +0000349}
350
351void mallocEscapeFreeUse() {
352 int *p = malloc(12);
353 myfoo(p);
354 free(p);
Anna Zaksfebdc322012-02-16 22:26:12 +0000355 myfoo(p); // expected-warning{{Use of memory after it is freed}}
Anna Zaks91c2a112012-02-08 23:16:56 +0000356}
357
358int *myalloc();
359void myalloc2(int **p);
360
361void mallocEscapeFreeCustomAlloc() {
362 int *p = malloc(12);
363 myfoo(p);
364 free(p);
365 p = myalloc();
366 free(p); // no warning
367}
368
369void mallocEscapeFreeCustomAlloc2() {
370 int *p = malloc(12);
371 myfoo(p);
372 free(p);
373 myalloc2(&p);
374 free(p); // no warning
375}
376
377void mallocBindFreeUse() {
378 int *x = malloc(12);
379 int *y = x;
380 free(y);
Anna Zaksfebdc322012-02-16 22:26:12 +0000381 myfoo(x); // expected-warning{{Use of memory after it is freed}}
Anna Zaks91c2a112012-02-08 23:16:56 +0000382}
383
384void mallocEscapeMalloc() {
385 int *p = malloc(12);
386 myfoo(p);
Anna Zaks3d7c44e2012-03-21 19:45:08 +0000387 p = malloc(12); // expected-warning{{Memory is never released; potential leak}}
Anna Zaks91c2a112012-02-08 23:16:56 +0000388}
389
390void mallocMalloc() {
391 int *p = malloc(12);
Anna Zaks3d7c44e2012-03-21 19:45:08 +0000392 p = malloc(12); // expected-warning 2 {{Memory is never released; potential leak}}
Anna Zaks91c2a112012-02-08 23:16:56 +0000393}
394
395void mallocFreeMalloc() {
396 int *p = malloc(12);
397 free(p);
398 p = malloc(12);
399 free(p);
400}
401
Anna Zakscdfec5e2012-02-09 06:25:47 +0000402void mallocFreeUse_params() {
Anna Zaks91c2a112012-02-08 23:16:56 +0000403 int *p = malloc(12);
404 free(p);
Anna Zaksfebdc322012-02-16 22:26:12 +0000405 myfoo(p); //expected-warning{{Use of memory after it is freed}}
Anna Zaks15d0ae12012-02-11 23:46:36 +0000406}
407
408void mallocFreeUse_params2() {
409 int *p = malloc(12);
410 free(p);
Anna Zaksfebdc322012-02-16 22:26:12 +0000411 myfooint(*p); //expected-warning{{Use of memory after it is freed}}
Anna Zaks91c2a112012-02-08 23:16:56 +0000412}
413
Anna Zaksff3b9fd2012-02-09 06:25:51 +0000414void mallocFailedOrNot() {
415 int *p = malloc(12);
416 if (!p)
417 free(p);
418 else
419 free(p);
420}
421
Anna Zakse9ef5622012-02-10 01:11:00 +0000422struct StructWithInt {
423 int g;
424};
Anna Zaks0860cd02012-02-11 21:44:39 +0000425
426int *mallocReturnFreed() {
427 int *p = malloc(12);
428 free(p);
Anna Zaksfebdc322012-02-16 22:26:12 +0000429 return p; // expected-warning {{Use of memory after it is freed}}
Anna Zaks0860cd02012-02-11 21:44:39 +0000430}
431
432int useAfterFreeStruct() {
433 struct StructWithInt *px= malloc(sizeof(struct StructWithInt));
434 px->g = 5;
435 free(px);
Anna Zaksfebdc322012-02-16 22:26:12 +0000436 return px->g; // expected-warning {{Use of memory after it is freed}}
Anna Zaks0860cd02012-02-11 21:44:39 +0000437}
438
Anna Zakse9ef5622012-02-10 01:11:00 +0000439void nonSymbolAsFirstArg(int *pp, struct StructWithInt *p);
440
441void mallocEscapeFooNonSymbolArg() {
442 struct StructWithInt *p = malloc(sizeof(struct StructWithInt));
443 nonSymbolAsFirstArg(&p->g, p);
444 return; // no warning
445}
446
Anna Zaks4fb54872012-02-11 21:02:35 +0000447void mallocFailedOrNotLeak() {
448 int *p = malloc(12);
449 if (p == 0)
450 return; // no warning
451 else
Anna Zaks3d7c44e2012-03-21 19:45:08 +0000452 return; // expected-warning {{Memory is never released; potential leak}}
Anna Zaks4fb54872012-02-11 21:02:35 +0000453}
Anna Zakse9ef5622012-02-10 01:11:00 +0000454
Anna Zaksebc1d322012-02-15 00:11:28 +0000455void mallocAssignment() {
456 char *p = malloc(12);
457 p = fooRetPtr(); // expected-warning {{leak}}
458}
459
Anna Zaksb16ce452012-02-15 00:11:22 +0000460int vallocTest() {
461 char *mem = valloc(12);
Anna Zaks3d7c44e2012-03-21 19:45:08 +0000462 return 0; // expected-warning {{Memory is never released; potential leak}}
Anna Zaksb16ce452012-02-15 00:11:22 +0000463}
464
465void vallocEscapeFreeUse() {
466 int *p = valloc(12);
467 myfoo(p);
468 free(p);
Anna Zaksfebdc322012-02-16 22:26:12 +0000469 myfoo(p); // expected-warning{{Use of memory after it is freed}}
Anna Zaksb16ce452012-02-15 00:11:22 +0000470}
471
Anna Zakscdfec5e2012-02-09 06:25:47 +0000472int *Gl;
473struct GlStTy {
474 int *x;
475};
476
477struct GlStTy GlS = {0};
478
479void GlobalFree() {
480 free(Gl);
481}
482
483void GlobalMalloc() {
484 Gl = malloc(12);
485}
486
487void GlobalStructMalloc() {
488 int *a = malloc(12);
489 GlS.x = a;
490}
491
492void GlobalStructMallocFree() {
493 int *a = malloc(12);
494 GlS.x = a;
495 free(GlS.x);
496}
Anna Zaksf8b1c312012-02-10 01:11:03 +0000497
Anna Zaksad901a62012-02-16 22:26:15 +0000498char *ArrayG[12];
499
500void globalArrayTest() {
501 char *p = (char*)malloc(12);
502 ArrayG[0] = p;
503}
504
Anna Zaksac593002012-02-16 03:40:57 +0000505// Make sure that we properly handle a pointer stored into a local struct/array.
506typedef struct _StructWithPtr {
507 int *memP;
508} StructWithPtr;
509
510static StructWithPtr arrOfStructs[10];
511
512void testMalloc() {
513 int *x = malloc(12);
514 StructWithPtr St;
515 St.memP = x;
Jordan Rose0d53ab42012-08-08 18:23:31 +0000516 arrOfStructs[0] = St; // no-warning
Anna Zaksac593002012-02-16 03:40:57 +0000517}
518
519StructWithPtr testMalloc2() {
520 int *x = malloc(12);
521 StructWithPtr St;
522 St.memP = x;
Jordan Rose0d53ab42012-08-08 18:23:31 +0000523 return St; // no-warning
Anna Zaksac593002012-02-16 03:40:57 +0000524}
525
526int *testMalloc3() {
527 int *x = malloc(12);
528 int *y = x;
Jordan Rose0d53ab42012-08-08 18:23:31 +0000529 return y; // no-warning
Anna Zaksac593002012-02-16 03:40:57 +0000530}
531
Jordan Rose919e8a12012-08-08 18:23:36 +0000532void testStructLeak() {
533 StructWithPtr St;
534 St.memP = malloc(12);
535 return; // expected-warning {{Memory is never released; potential leak of memory pointed to by 'St.memP'}}
536}
537
Anna Zaksd8a8a3b2012-02-17 22:35:34 +0000538void testElemRegion1() {
539 char *x = (void*)malloc(2);
540 int *ix = (int*)x;
541 free(&(x[0]));
542}
543
544void testElemRegion2(int **pp) {
545 int *p = malloc(12);
546 *pp = p;
547 free(pp[0]);
548}
549
550void testElemRegion3(int **pp) {
551 int *p = malloc(12);
552 *pp = p;
553 free(*pp);
554}
Anna Zaks4fb54872012-02-11 21:02:35 +0000555// Region escape testing.
556
557unsigned takePtrToPtr(int **p);
558void PassTheAddrOfAllocatedData(int f) {
559 int *p = malloc(12);
560 // We don't know what happens after the call. Should stop tracking here.
561 if (takePtrToPtr(&p))
562 f++;
563 free(p); // no warning
564}
565
566struct X {
567 int *p;
568};
569unsigned takePtrToStruct(struct X *s);
570int ** foo2(int *g, int f) {
571 int *p = malloc(12);
572 struct X *px= malloc(sizeof(struct X));
573 px->p = p;
574 // We don't know what happens after this call. Should not track px nor p.
575 if (takePtrToStruct(px))
576 f++;
577 free(p);
578 return 0;
579}
580
581struct X* RegInvalidationDetect1(struct X *s2) {
582 struct X *px= malloc(sizeof(struct X));
583 px->p = 0;
584 px = s2;
Anna Zaks3d7c44e2012-03-21 19:45:08 +0000585 return px; // expected-warning {{Memory is never released; potential leak}}
Anna Zaks4fb54872012-02-11 21:02:35 +0000586}
587
588struct X* RegInvalidationGiveUp1() {
589 int *p = malloc(12);
590 struct X *px= malloc(sizeof(struct X));
591 px->p = p;
592 return px;
593}
594
595int **RegInvalidationDetect2(int **pp) {
596 int *p = malloc(12);
597 pp = &p;
598 pp++;
Anna Zaks3d7c44e2012-03-21 19:45:08 +0000599 return 0;// expected-warning {{Memory is never released; potential leak}}
Anna Zaks4fb54872012-02-11 21:02:35 +0000600}
Anna Zaksf8b1c312012-02-10 01:11:03 +0000601
Anna Zaksf8b1c312012-02-10 01:11:03 +0000602extern void exit(int) __attribute__ ((__noreturn__));
603void mallocExit(int *g) {
604 struct xx *p = malloc(12);
Anna Zaksda046772012-02-11 21:02:40 +0000605 if (g != 0)
606 exit(1);
Anna Zaksf8b1c312012-02-10 01:11:03 +0000607 free(p);
608 return;
609}
610
Anna Zaksf8b1c312012-02-10 01:11:03 +0000611extern void __assert_fail (__const char *__assertion, __const char *__file,
612 unsigned int __line, __const char *__function)
613 __attribute__ ((__noreturn__));
614#define assert(expr) \
615 ((expr) ? (void)(0) : __assert_fail (#expr, __FILE__, __LINE__, __func__))
616void mallocAssert(int *g) {
617 struct xx *p = malloc(12);
618
Anna Zaksda046772012-02-11 21:02:40 +0000619 assert(g != 0);
Anna Zaksf8b1c312012-02-10 01:11:03 +0000620 free(p);
621 return;
622}
623
Anna Zaks15d0ae12012-02-11 23:46:36 +0000624void doNotInvalidateWhenPassedToSystemCalls(char *s) {
625 char *p = malloc(12);
626 strlen(p);
627 strcpy(p, s); // expected-warning {{leak}}
628}
629
Anna Zaksf0dfc9c2012-02-17 22:35:31 +0000630// Rely on the CString checker evaluation of the strcpy API to convey that the result of strcpy is equal to p.
631void symbolLostWithStrcpy(char *s) {
632 char *p = malloc(12);
633 p = strcpy(p, s);
634 free(p);
635}
636
637
638// The same test as the one above, but with what is actually generated on a mac.
639static __inline char *
640__inline_strcpy_chk (char *restrict __dest, const char *restrict __src)
641{
642 return __builtin___strcpy_chk (__dest, __src, __builtin_object_size (__dest, 2 > 1));
643}
644
645void symbolLostWithStrcpy_InlineStrcpyVersion(char *s) {
646 char *p = malloc(12);
647 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));
648 free(p);
649}
Anna Zaksd9ab7bb2012-02-22 02:36:01 +0000650
651// Here we are returning a pointer one past the allocated value. An idiom which
652// can be used for implementing special malloc. The correct uses of this might
653// be rare enough so that we could keep this as a warning.
654static void *specialMalloc(int n){
655 int *p;
656 p = malloc( n+8 );
657 if( p ){
658 p[0] = n;
659 p++;
660 }
661 return p;
662}
663
664// Potentially, the user could free the struct by performing pointer arithmetic on the return value.
665// This is a variation of the specialMalloc issue, though probably would be more rare in correct code.
666int *specialMallocWithStruct() {
667 struct StructWithInt *px= malloc(sizeof(struct StructWithInt));
668 return &(px->g);
669}
670
Anna Zaks60a1fa42012-02-22 03:14:20 +0000671// Test various allocation/deallocation functions.
Anna Zaks60a1fa42012-02-22 03:14:20 +0000672void testStrdup(const char *s, unsigned validIndex) {
673 char *s2 = strdup(s);
Anna Zaks3d7c44e2012-03-21 19:45:08 +0000674 s2[validIndex + 1] = 'b';// expected-warning {{Memory is never released; potential leak}}
Anna Zaks60a1fa42012-02-22 03:14:20 +0000675}
676
677int testStrndup(const char *s, unsigned validIndex, unsigned size) {
678 char *s2 = strndup(s, size);
679 s2 [validIndex + 1] = 'b';
680 if (s2[validIndex] != 'a')
Anna Zaksca8e36e2012-02-23 21:38:21 +0000681 return 0;
Anna Zaks60a1fa42012-02-22 03:14:20 +0000682 else
Anna Zaks3d7c44e2012-03-21 19:45:08 +0000683 return 1;// expected-warning {{Memory is never released; potential leak}}
Anna Zaks60a1fa42012-02-22 03:14:20 +0000684}
685
Anna Zaks87cb5be2012-02-22 19:24:52 +0000686void testStrdupContentIsDefined(const char *s, unsigned validIndex) {
687 char *s2 = strdup(s);
688 char result = s2[1];// no warning
689 free(s2);
690}
691
Anna Zaksca23eb22012-02-29 18:42:47 +0000692// ----------------------------------------------------------------------------
Anna Zaks0d389b82012-02-23 01:05:27 +0000693// Test the system library functions to which the pointer can escape.
Anna Zaksca23eb22012-02-29 18:42:47 +0000694// This tests false positive suppression.
Anna Zaks0d389b82012-02-23 01:05:27 +0000695
696// For now, we assume memory passed to pthread_specific escapes.
697// TODO: We could check that if a new pthread binding is set, the existing
698// binding must be freed; otherwise, a memory leak can occur.
699void testPthereadSpecificEscape(pthread_key_t key) {
700 void *buf = malloc(12);
701 pthread_setspecific(key, buf); // no warning
702}
703
Anna Zaksca23eb22012-02-29 18:42:47 +0000704// PR12101: Test funopen().
705static int releasePtr(void *_ctx) {
706 free(_ctx);
707 return 0;
708}
709FILE *useFunOpen() {
710 void *ctx = malloc(sizeof(int));
711 FILE *f = funopen(ctx, 0, 0, 0, releasePtr); // no warning
712 if (f == 0) {
713 free(ctx);
714 }
715 return f;
716}
717FILE *useFunOpenNoReleaseFunction() {
718 void *ctx = malloc(sizeof(int));
719 FILE *f = funopen(ctx, 0, 0, 0, 0);
720 if (f == 0) {
721 free(ctx);
722 }
723 return f; // expected-warning{{leak}}
724}
725
Jordan Rose85d7e012012-07-02 19:27:51 +0000726static int readNothing(void *_ctx, char *buf, int size) {
727 return 0;
728}
729FILE *useFunOpenReadNoRelease() {
730 void *ctx = malloc(sizeof(int));
731 FILE *f = funopen(ctx, readNothing, 0, 0, 0);
732 if (f == 0) {
733 free(ctx);
734 }
735 return f; // expected-warning{{leak}}
736}
737
Anna Zaksca23eb22012-02-29 18:42:47 +0000738// Test setbuf, setvbuf.
739int my_main_no_warning() {
740 char *p = malloc(100);
741 setvbuf(stdout, p, 0, 100);
742 return 0;
743}
744int my_main_no_warning2() {
745 char *p = malloc(100);
746 setbuf(__stdoutp, p);
747 return 0;
748}
749int my_main_warn(FILE *f) {
750 char *p = malloc(100);
751 setvbuf(f, p, 0, 100);
752 return 0;// expected-warning {{leak}}
753}
754
Ted Kremeneka99f8742012-03-05 23:06:19 +0000755// <rdar://problem/10978247>.
756// some people use stack allocated memory as an optimization to avoid
757// a heap allocation for small work sizes. This tests the analyzer's
758// understanding that the malloc'ed memory is not the same as stackBuffer.
759void radar10978247(int myValueSize) {
760 char stackBuffer[128];
761 char *buffer;
762
763 if (myValueSize <= sizeof(stackBuffer))
764 buffer = stackBuffer;
765 else
766 buffer = malloc(myValueSize);
767
768 // do stuff with the buffer
769 if (buffer != stackBuffer)
770 free(buffer);
771}
772
773void radar10978247_positive(int myValueSize) {
774 char stackBuffer[128];
775 char *buffer;
776
777 if (myValueSize <= sizeof(stackBuffer))
778 buffer = stackBuffer;
779 else
780 buffer = malloc(myValueSize);
781
782 // do stuff with the buffer
783 if (buffer == stackBuffer) // expected-warning {{leak}}
784 return;
785}
786
Ted Kremenek8f40afb2012-04-26 05:08:26 +0000787// <rdar://problem/11269741> Previously this triggered a false positive
788// because malloc() is known to return uninitialized memory and the binding
789// of 'o' to 'p->n' was not getting propertly handled. Now we report a leak.
790struct rdar11269741_a_t {
791 struct rdar11269741_b_t {
792 int m;
793 } n;
794};
795
796int rdar11269741(struct rdar11269741_b_t o)
797{
798 struct rdar11269741_a_t *p = (struct rdar11269741_a_t *) malloc(sizeof(*p));
799 p->n = o;
800 return p->n.m; // expected-warning {{leak}}
801}
802
Anna Zakse55a14a2012-05-03 02:13:56 +0000803// Pointer arithmetic, returning an ElementRegion.
804void *radar11329382(unsigned bl) {
805 void *ptr = malloc (16);
806 ptr = ptr + (2 - bl);
807 return ptr; // no warning
808}
809
Anna Zaks33e4a1d2012-05-01 21:10:29 +0000810void __assert_rtn(const char *, const char *, int, const char *) __attribute__((__noreturn__));
811int strcmp(const char *, const char *);
812char *a (void);
813void radar11270219(void) {
814 char *x = a(), *y = a();
815 (__builtin_expect(!(x && y), 0) ? __assert_rtn(__func__, "/Users/zaks/tmp/ex.c", 24, "x && y") : (void)0);
816 strcmp(x, y); // no warning
817}
818
Anna Zaks93c5a242012-05-02 00:05:20 +0000819void radar_11358224_test_double_assign_ints_positive_2()
820{
821 void *ptr = malloc(16);
822 ptr = ptr; // expected-warning {{leak}}
823}
824
Anna Zaksaca0ac52012-05-03 23:50:28 +0000825// Assume that functions which take a function pointer can free memory even if
826// they are defined in system headers and take the const pointer to the
827// allocated memory. (radar://11160612)
828int const_ptr_and_callback(int, const char*, int n, void(*)(void*));
829void r11160612_1() {
830 char *x = malloc(12);
831 const_ptr_and_callback(0, x, 12, free); // no - warning
832}
833
834// Null is passed as callback.
835void r11160612_2() {
836 char *x = malloc(12);
837 const_ptr_and_callback(0, x, 12, 0); // expected-warning {{leak}}
838}
839
840// Callback is passed to a function defined in a system header.
841void r11160612_4() {
842 char *x = malloc(12);
843 sqlite3_bind_text_my(0, x, 12, free); // no - warning
844}
845
Anna Zaksb79d8622012-05-03 23:50:33 +0000846// Passing callbacks in a struct.
847void r11160612_5(StWithCallback St) {
848 void *x = malloc(12);
849 dealocateMemWhenDoneByVal(x, St);
850}
851void r11160612_6(StWithCallback St) {
852 void *x = malloc(12);
853 dealocateMemWhenDoneByRef(&St, x);
854}
855
Anna Zaks84d43842012-05-04 17:37:16 +0000856int mySub(int, int);
857int myAdd(int, int);
858int fPtr(unsigned cond, int x) {
859 return (cond ? mySub : myAdd)(x, x);
860}
861
Anna Zakse17fdb22012-06-07 03:57:32 +0000862// Test anti-aliasing.
Anna Zaksda046772012-02-11 21:02:40 +0000863
Anna Zaksf8b1c312012-02-10 01:11:03 +0000864void dependsOnValueOfPtr(int *g, unsigned f) {
865 int *p;
866
867 if (f) {
868 p = g;
869 } else {
870 p = malloc(12);
871 }
872
873 if (p != g)
874 free(p);
875 else
Anna Zakse17fdb22012-06-07 03:57:32 +0000876 return; // no warning
Anna Zaksf8b1c312012-02-10 01:11:03 +0000877 return;
878}
879
Anna Zakse17fdb22012-06-07 03:57:32 +0000880int CMPRegionHeapToStack() {
881 int x = 0;
882 int *x1 = malloc(8);
883 int *x2 = &x;
Anna Zaksadccc3f2012-06-08 00:04:40 +0000884 clang_analyzer_eval(x1 == x2); // expected-warning{{FALSE}}
Anna Zakse17fdb22012-06-07 03:57:32 +0000885 free(x1);
886 return x;
887}
888
889int CMPRegionHeapToHeap2() {
890 int x = 0;
891 int *x1 = malloc(8);
892 int *x2 = malloc(8);
893 int *x4 = x1;
894 int *x5 = x2;
Anna Zaksadccc3f2012-06-08 00:04:40 +0000895 clang_analyzer_eval(x4 == x5); // expected-warning{{FALSE}}
Anna Zakse17fdb22012-06-07 03:57:32 +0000896 free(x1);
897 free(x2);
898 return x;
899}
900
901int CMPRegionHeapToHeap() {
902 int x = 0;
903 int *x1 = malloc(8);
904 int *x4 = x1;
905 if (x1 == x4) {
906 free(x1);
907 return 5/x; // expected-warning{{Division by zero}}
908 }
909 return x;// expected-warning{{This statement is never executed}}
910}
911
912int HeapAssignment() {
913 int m = 0;
914 int *x = malloc(4);
915 int *y = x;
916 *x = 5;
Anna Zaksadccc3f2012-06-08 00:04:40 +0000917 clang_analyzer_eval(*x != *y); // expected-warning{{FALSE}}
Anna Zakse17fdb22012-06-07 03:57:32 +0000918 free(x);
919 return 0;
920}
921
Anna Zaks783f0082012-06-07 20:18:08 +0000922int *retPtr();
923int *retPtrMightAlias(int *x);
924int cmpHeapAllocationToUnknown() {
925 int zero = 0;
926 int *yBefore = retPtr();
927 int *m = malloc(8);
928 int *yAfter = retPtrMightAlias(m);
Anna Zaksadccc3f2012-06-08 00:04:40 +0000929 clang_analyzer_eval(yBefore == m); // expected-warning{{FALSE}}
930 clang_analyzer_eval(yAfter == m); // expected-warning{{FALSE}}
Anna Zaks783f0082012-06-07 20:18:08 +0000931 free(m);
932 return 0;
933}
934
Anna Zaksad901a62012-02-16 22:26:15 +0000935void localArrayTest() {
936 char *p = (char*)malloc(12);
937 char *ArrayL[12];
Jordan Rose0d53ab42012-08-08 18:23:31 +0000938 ArrayL[0] = p; // expected-warning {{leak}}
939}
940
941void localStructTest() {
942 StructWithPtr St;
943 StructWithPtr *pSt = &St;
944 pSt->memP = malloc(12); // expected-warning{{Memory is never released; potential leak}}
Anna Zaksad901a62012-02-16 22:26:15 +0000945}
946
Ted Kremenek140d0c62012-05-01 21:58:29 +0000947// Test double assignment through integers.
948static long glob;
949void test_double_assign_ints()
950{
951 void *ptr = malloc (16); // no-warning
952 glob = (long)(unsigned long)ptr;
953}
954
955void test_double_assign_ints_positive()
956{
957 void *ptr = malloc(16);
958 (void*)(long)(unsigned long)ptr; // expected-warning {{unused}} expected-warning {{leak}}
959}
960
Jordan Rose1bf908d2012-06-16 00:09:20 +0000961
962void testCGContextNoLeak()
963{
964 void *ptr = malloc(16);
965 CGContextRef context = CGBitmapContextCreate(ptr);
966
967 // Because you can get the data back out like this, even much later,
968 // CGBitmapContextCreate is one of our "stop-tracking" exceptions.
969 free(CGBitmapContextGetData(context));
970}
971
972void testCGContextLeak()
973{
974 void *ptr = malloc(16);
975 CGContextRef context = CGBitmapContextCreate(ptr);
976 // However, this time we're just leaking the data, because the context
977 // object doesn't escape and it hasn't been freed in this function.
978}
979
Anna Zaks52a04812012-06-20 23:35:57 +0000980// Allow xpc context to escape. radar://11635258
981// TODO: Would be great if we checked that the finalize_connection_context actually releases it.
982static void finalize_connection_context(void *ctx) {
983 int *context = ctx;
984 free(context);
985}
986void foo (xpc_connection_t peer) {
987 int *ctx = calloc(1, sizeof(int));
988 xpc_connection_set_context(peer, ctx);
989 xpc_connection_set_finalizer_f(peer, finalize_connection_context);
990 xpc_connection_resume(peer);
991}
992
Anna Zaksede875b2012-08-03 18:30:18 +0000993// Make sure we catch errors when we free in a function which does not allocate memory.
994void freeButNoMalloc(int *p, int x){
995 if (x) {
996 free(p);
997 //user forgot a return here.
998 }
999 free(p); // expected-warning {{Attempt to free released memory}}
1000}
Anna Zaks4d332862012-08-04 02:04:27 +00001001
1002struct HasPtr {
1003 int *p;
1004};
1005
1006int* reallocButNoMalloc(struct HasPtr *a, int c, int size) {
1007 int *s;
1008 a->p = (int *)realloc(a->p, size);
1009 if (a->p == 0)
1010 return 0; // expected-warning{{Memory is never released; potential leak}}
1011 return a->p;
1012}
Jordan Rose0d53ab42012-08-08 18:23:31 +00001013
1014// ----------------------------------------------------------------------------
1015// False negatives.
1016
1017// TODO: This is another false negative.
1018void testMallocWithParam(int **p) {
1019 *p = (int*) malloc(sizeof(int));
1020 *p = 0;
1021}
1022
1023void testMallocWithParam_2(int **p) {
1024 *p = (int*) malloc(sizeof(int));
1025}