blob: 881eb38ad8401dc978c5895407a887e2bdf2a8c1 [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);
Anton Yartsev9907fc92015-03-04 23:18:21 +00009void *alloca(size_t);
Anna Zaksd5157482012-02-15 00:11:22 +000010void *valloc(size_t);
Ted Kremenek9430bf22009-11-13 20:03:22 +000011void free(void *);
Zhongxing Xuc0484fa2009-12-12 12:29:38 +000012void *realloc(void *ptr, size_t size);
Anna Zaksac068142012-02-15 00:11:25 +000013void *reallocf(void *ptr, size_t size);
Zhongxing Xuc0484fa2009-12-12 12:29:38 +000014void *calloc(size_t nmemb, size_t size);
Anna Zaks46d01602012-05-18 01:16:10 +000015char *strdup(const char *s);
16char *strndup(const char *s, size_t n);
Anna Zaksacdc13c2013-02-07 23:05:43 +000017int memcmp(const void *s1, const void *s2, size_t n);
Ted Kremenekd21139a2010-07-31 01:52:11 +000018
Anna Zaksa1b227b2012-02-08 23:16:56 +000019void myfoo(int *p);
20void myfooint(int p);
Anna Zaks5a6213d2012-02-15 00:11:28 +000021char *fooRetPtr();
Zhongxing Xuc7460962009-11-13 07:48:11 +000022
23void f1() {
Zhongxing Xu658dd8b2010-05-25 04:59:19 +000024 int *p = malloc(12);
Anna Zaksa1de8562013-04-06 00:41:36 +000025 return; // expected-warning{{Potential leak of memory pointed to by 'p'}}
Zhongxing Xuc7460962009-11-13 07:48:11 +000026}
27
Zhongxing Xuc7460962009-11-13 07:48:11 +000028void f2() {
Zhongxing Xu658dd8b2010-05-25 04:59:19 +000029 int *p = malloc(12);
Zhongxing Xuc7460962009-11-13 07:48:11 +000030 free(p);
Anna Zaks546c49c2012-02-16 22:26:12 +000031 free(p); // expected-warning{{Attempt to free released memory}}
Zhongxing Xuc7460962009-11-13 07:48:11 +000032}
Ted Kremeneke5e977012009-11-13 20:00:28 +000033
Lenny Maiorani005b5c12011-04-27 14:49:29 +000034void f2_realloc_0() {
35 int *p = malloc(12);
36 realloc(p,0);
Anna Zaks546c49c2012-02-16 22:26:12 +000037 realloc(p,0); // expected-warning{{Attempt to free released memory}}
Lenny Maiorani005b5c12011-04-27 14:49:29 +000038}
39
40void f2_realloc_1() {
41 int *p = malloc(12);
Zhongxing Xubfb8e2f2011-09-01 04:53:59 +000042 int *q = realloc(p,0); // no-warning
Lenny Maiorani005b5c12011-04-27 14:49:29 +000043}
44
Anna Zaksd56c8792012-02-13 18:05:39 +000045void reallocNotNullPtr(unsigned sizeIn) {
46 unsigned size = 12;
47 char *p = (char*)malloc(size);
48 if (p) {
49 char *q = (char*)realloc(p, sizeIn);
Anna Zaksa1de8562013-04-06 00:41:36 +000050 char x = *q; // expected-warning {{Potential leak of memory pointed to by 'q'}}
Anna Zaksd56c8792012-02-13 18:05:39 +000051 }
52}
53
Anton Yartsev9907fc92015-03-04 23:18:21 +000054void allocaTest() {
55 int *p = alloca(sizeof(int));
56} // no warn
57
58void allocaBuiltinTest() {
59 int *p = __builtin_alloca(sizeof(int));
60} // no warn
61
Anna Zaksd56c8792012-02-13 18:05:39 +000062int *realloctest1() {
63 int *q = malloc(12);
64 q = realloc(q, 20);
65 return q; // no warning - returning the allocated value
66}
67
68// p should be freed if realloc fails.
69void reallocFails() {
70 char *p = malloc(12);
71 char *r = realloc(p, 12+1);
72 if (!r) {
73 free(p);
74 } else {
75 free(r);
76 }
77}
78
Anna Zaks8fd0f2a2012-02-13 20:57:07 +000079void reallocSizeZero1() {
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 }
87}
88
89void reallocSizeZero2() {
90 char *p = malloc(12);
91 char *r = realloc(p, 0);
92 if (!r) {
Anna Zaks52242a62012-08-03 18:30:18 +000093 free(p); // expected-warning {{Attempt to free released memory}}
Anna Zaks8fd0f2a2012-02-13 20:57:07 +000094 } else {
95 free(r);
96 }
Anna Zaks546c49c2012-02-16 22:26:12 +000097 free(p); // expected-warning {{Attempt to free released memory}}
Anna Zaks8fd0f2a2012-02-13 20:57:07 +000098}
99
100void reallocSizeZero3() {
101 char *p = malloc(12);
102 char *r = realloc(p, 0);
103 free(r);
104}
105
106void reallocSizeZero4() {
107 char *r = realloc(0, 0);
108 free(r);
109}
110
111void reallocSizeZero5() {
112 char *r = realloc(0, 0);
113}
114
115void reallocPtrZero1() {
Jordan Rosee37ab502012-11-15 19:11:43 +0000116 char *r = realloc(0, 12);
Anna Zaksa1de8562013-04-06 00:41:36 +0000117} // expected-warning {{Potential leak of memory pointed to by 'r'}}
Anna Zaks8fd0f2a2012-02-13 20:57:07 +0000118
119void reallocPtrZero2() {
120 char *r = realloc(0, 12);
121 if (r)
122 free(r);
123}
124
125void reallocPtrZero3() {
126 char *r = realloc(0, 12);
127 free(r);
128}
129
Anna Zaksad01ef52012-02-14 00:26:13 +0000130void reallocRadar6337483_1() {
131 char *buf = malloc(100);
132 buf = (char*)realloc(buf, 0x1000000);
133 if (!buf) {
Anna Zaksa1de8562013-04-06 00:41:36 +0000134 return;// expected-warning {{Potential leak of memory pointed to by}}
Anna Zaksad01ef52012-02-14 00:26:13 +0000135 }
136 free(buf);
137}
138
139void reallocRadar6337483_2() {
140 char *buf = malloc(100);
141 char *buf2 = (char*)realloc(buf, 0x1000000);
Jordan Rosee37ab502012-11-15 19:11:43 +0000142 if (!buf2) {
Anna Zaksad01ef52012-02-14 00:26:13 +0000143 ;
144 } else {
145 free(buf2);
146 }
Anna Zaksa1de8562013-04-06 00:41:36 +0000147} // expected-warning {{Potential leak of memory pointed to by}}
Anna Zaksad01ef52012-02-14 00:26:13 +0000148
149void reallocRadar6337483_3() {
150 char * buf = malloc(100);
151 char * tmp;
152 tmp = (char*)realloc(buf, 0x1000000);
153 if (!tmp) {
154 free(buf);
155 return;
156 }
157 buf = tmp;
158 free(buf);
159}
160
161void reallocRadar6337483_4() {
162 char *buf = malloc(100);
163 char *buf2 = (char*)realloc(buf, 0x1000000);
164 if (!buf2) {
Anna Zaksa1de8562013-04-06 00:41:36 +0000165 return; // expected-warning {{Potential leak of memory pointed to by}}
Anna Zaksad01ef52012-02-14 00:26:13 +0000166 } else {
167 free(buf2);
168 }
169}
170
Anna Zaksac068142012-02-15 00:11:25 +0000171int *reallocfTest1() {
172 int *q = malloc(12);
173 q = reallocf(q, 20);
174 return q; // no warning - returning the allocated value
175}
176
177void reallocfRadar6337483_4() {
178 char *buf = malloc(100);
179 char *buf2 = (char*)reallocf(buf, 0x1000000);
180 if (!buf2) {
181 return; // no warning - reallocf frees even on failure
182 } else {
183 free(buf2);
184 }
185}
186
187void reallocfRadar6337483_3() {
188 char * buf = malloc(100);
189 char * tmp;
190 tmp = (char*)reallocf(buf, 0x1000000);
191 if (!tmp) {
Anna Zaks546c49c2012-02-16 22:26:12 +0000192 free(buf); // expected-warning {{Attempt to free released memory}}
Anna Zaksac068142012-02-15 00:11:25 +0000193 return;
194 }
195 buf = tmp;
196 free(buf);
197}
198
199void reallocfPtrZero1() {
Jordan Rosee37ab502012-11-15 19:11:43 +0000200 char *r = reallocf(0, 12);
Anna Zaksa1de8562013-04-06 00:41:36 +0000201} // expected-warning {{Potential leak of memory pointed to by}}
Anna Zaksac068142012-02-15 00:11:25 +0000202
Anton Yartsevb50f4ba2015-04-14 14:18:04 +0000203//------------------- Check usage of zero-allocated memory ---------------------
204void CheckUseZeroAllocatedNoWarn1() {
205 int *p = malloc(0);
206 free(p); // no warning
207}
208
209void CheckUseZeroAllocatedNoWarn2() {
210 int *p = alloca(0); // no warning
211}
212
213void CheckUseZeroAllocatedNoWarn3() {
214 int *p = malloc(0);
215 int *q = realloc(p, 8); // no warning
216 free(q);
217}
218
219void CheckUseZeroAllocatedNoWarn4() {
220 int *p = realloc(0, 8);
221 *p = 1; // no warning
222 free(p);
223}
224
225void CheckUseZeroAllocated1() {
226 int *p = malloc(0);
227 *p = 1; // expected-warning {{Use of zero-allocated memory}}
228 free(p);
229}
230
231char CheckUseZeroAllocated2() {
232 char *p = alloca(0);
233 return *p; // expected-warning {{Use of zero-allocated memory}}
234}
235
236void UseZeroAllocated(int *p) {
237 if (p)
238 *p = 7; // expected-warning {{Use of zero-allocated memory}}
239}
240void CheckUseZeroAllocated3() {
241 int *p = malloc(0);
242 UseZeroAllocated(p);
243}
244
245void f(char);
246void CheckUseZeroAllocated4() {
247 char *p = valloc(0);
248 f(*p); // expected-warning {{Use of zero-allocated memory}}
249 free(p);
250}
251
252void CheckUseZeroAllocated5() {
253 int *p = calloc(0, 2);
254 *p = 1; // expected-warning {{Use of zero-allocated memory}}
255 free(p);
256}
257
258void CheckUseZeroAllocated6() {
259 int *p = calloc(2, 0);
260 *p = 1; // expected-warning {{Use of zero-allocated memory}}
261 free(p);
262}
263
264void CheckUseZeroAllocated7() {
265 int *p = realloc(0, 0);
Devin Coughlin81771732015-09-22 22:47:14 +0000266 *p = 1; // expected-warning {{Use of zero-allocated memory}}
Anton Yartsevb50f4ba2015-04-14 14:18:04 +0000267 free(p);
268}
269
270void CheckUseZeroAllocated8() {
271 int *p = malloc(8);
272 int *q = realloc(p, 0);
Devin Coughlin81771732015-09-22 22:47:14 +0000273 *q = 1; // expected-warning {{Use of zero-allocated memory}}
Anton Yartsevb50f4ba2015-04-14 14:18:04 +0000274 free(q);
275}
276
277void CheckUseZeroAllocated9() {
278 int *p = realloc(0, 0);
279 int *q = realloc(p, 0);
Devin Coughlin81771732015-09-22 22:47:14 +0000280 *q = 1; // expected-warning {{Use of zero-allocated memory}}
Anton Yartsevb50f4ba2015-04-14 14:18:04 +0000281 free(q);
282}
283
284void CheckUseZeroAllocatedPathNoWarn(_Bool b) {
285 int s = 0;
286 if (b)
287 s= 10;
288
289 char *p = malloc(s);
290
291 if (b)
292 *p = 1; // no warning
293
294 free(p);
295}
296
297void CheckUseZeroAllocatedPathWarn(_Bool b) {
298 int s = 10;
299 if (b)
300 s= 0;
301
302 char *p = malloc(s);
303
304 if (b)
305 *p = 1; // expected-warning {{Use of zero-allocated memory}}
306
307 free(p);
308}
Anna Zaksac068142012-02-15 00:11:25 +0000309
Devin Coughlin81771732015-09-22 22:47:14 +0000310void CheckUseZeroReallocatedPathNoWarn(_Bool b) {
311 int s = 0;
312 if (b)
313 s= 10;
314
315 char *p = malloc(8);
316 char *q = realloc(p, s);
317
318 if (b)
319 *q = 1; // no warning
320
321 free(q);
322}
323
324void CheckUseZeroReallocatedPathWarn(_Bool b) {
325 int s = 10;
326 if (b)
327 s= 0;
328
329 char *p = malloc(8);
330 char *q = realloc(p, s);
331
332 if (b)
333 *q = 1; // expected-warning {{Use of zero-allocated memory}}
334
335 free(q);
336}
337
Zhongxing Xu4668c7e2009-11-17 07:54:15 +0000338// This case tests that storing malloc'ed memory to a static variable which is
339// then returned is not leaked. In the absence of known contracts for functions
340// or inter-procedural analysis, this is a conservative answer.
Ted Kremeneke5e977012009-11-13 20:00:28 +0000341int *f3() {
342 static int *p = 0;
Zhongxing Xu658dd8b2010-05-25 04:59:19 +0000343 p = malloc(12);
Zhongxing Xu23baa012009-11-17 08:58:18 +0000344 return p; // no-warning
Ted Kremeneke5e977012009-11-13 20:00:28 +0000345}
346
Zhongxing Xu4668c7e2009-11-17 07:54:15 +0000347// This case tests that storing malloc'ed memory to a static global variable
348// which is then returned is not leaked. In the absence of known contracts for
349// functions or inter-procedural analysis, this is a conservative answer.
Ted Kremeneke5e977012009-11-13 20:00:28 +0000350static int *p_f4 = 0;
351int *f4() {
Zhongxing Xu658dd8b2010-05-25 04:59:19 +0000352 p_f4 = malloc(12);
Zhongxing Xu23baa012009-11-17 08:58:18 +0000353 return p_f4; // no-warning
Ted Kremeneke5e977012009-11-13 20:00:28 +0000354}
Zhongxing Xuc0484fa2009-12-12 12:29:38 +0000355
356int *f5() {
Zhongxing Xu658dd8b2010-05-25 04:59:19 +0000357 int *q = malloc(12);
Zhongxing Xuc0484fa2009-12-12 12:29:38 +0000358 q = realloc(q, 20);
359 return q; // no-warning
360}
Zhongxing Xub0e15df2009-12-31 06:13:07 +0000361
362void f6() {
Zhongxing Xu658dd8b2010-05-25 04:59:19 +0000363 int *p = malloc(12);
Zhongxing Xub0e15df2009-12-31 06:13:07 +0000364 if (!p)
365 return; // no-warning
366 else
367 free(p);
368}
Zhongxing Xu5fcd99b2010-01-18 04:01:40 +0000369
Lenny Maiorani005b5c12011-04-27 14:49:29 +0000370void f6_realloc() {
371 int *p = malloc(12);
372 if (!p)
373 return; // no-warning
374 else
375 realloc(p,0);
376}
377
378
Zhongxing Xu5fcd99b2010-01-18 04:01:40 +0000379char *doit2();
380void pr6069() {
381 char *buf = doit2();
382 free(buf);
383}
Zhongxing Xube36ecb2010-02-14 06:49:48 +0000384
385void pr6293() {
386 free(0);
387}
Zhongxing Xu1bb6a1a2010-03-10 04:58:55 +0000388
389void f7() {
390 char *x = (char*) malloc(4);
391 free(x);
Anna Zaks546c49c2012-02-16 22:26:12 +0000392 x[0] = 'a'; // expected-warning{{Use of memory after it is freed}}
Zhongxing Xu1bb6a1a2010-03-10 04:58:55 +0000393}
Zhongxing Xu658dd8b2010-05-25 04:59:19 +0000394
Anna Zaks46d01602012-05-18 01:16:10 +0000395void f8() {
396 char *x = (char*) malloc(4);
397 free(x);
398 char *y = strndup(x, 4); // expected-warning{{Use of memory after it is freed}}
399}
400
Lenny Maiorani005b5c12011-04-27 14:49:29 +0000401void f7_realloc() {
402 char *x = (char*) malloc(4);
403 realloc(x,0);
Anna Zaks546c49c2012-02-16 22:26:12 +0000404 x[0] = 'a'; // expected-warning{{Use of memory after it is freed}}
Lenny Maiorani005b5c12011-04-27 14:49:29 +0000405}
406
Zhongxing Xu658dd8b2010-05-25 04:59:19 +0000407void PR6123() {
Ted Kremenek9bf9af92012-08-16 17:45:23 +0000408 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 +0000409}
410
411void PR7217() {
Ted Kremenek9bf9af92012-08-16 17:45:23 +0000412 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 +0000413 buf[1] = 'c'; // not crash
Zhongxing Xu658dd8b2010-05-25 04:59:19 +0000414}
Jordy Rose2dd9b022010-06-20 04:30:57 +0000415
Jordan Rose97d2c9c2014-02-18 17:06:30 +0000416void cast_emtpy_struct() {
417 struct st {
418 };
419
420 struct st *s = malloc(sizeof(struct st)); // no-warning
421 free(s);
422}
423
424void cast_struct_1() {
425 struct st {
426 int i[100];
427 char j[];
428 };
429
430 struct st *s = malloc(sizeof(struct st)); // no-warning
431 free(s);
432}
433
434void cast_struct_2() {
435 struct st {
436 int i[100];
437 char j[0];
438 };
439
440 struct st *s = malloc(sizeof(struct st)); // no-warning
441 free(s);
442}
443
444void cast_struct_3() {
445 struct st {
446 int i[100];
447 char j[1];
448 };
449
450 struct st *s = malloc(sizeof(struct st)); // no-warning
451 free(s);
452}
453
454void cast_struct_4() {
455 struct st {
456 int i[100];
457 char j[2];
458 };
459
460 struct st *s = malloc(sizeof(struct st)); // no-warning
461 free(s);
462}
463
464void cast_struct_5() {
465 struct st {
466 char i[200];
467 char j[1];
468 };
469
470 struct st *s = malloc(sizeof(struct st) - sizeof(char)); // no-warning
471 free(s);
472}
473
474void cast_struct_warn_1() {
475 struct st {
476 int i[100];
477 char j[2];
478 };
479
480 struct st *s = malloc(sizeof(struct st) + 2); // expected-warning{{Cast a region whose size is not a multiple of the destination type size}}
481 free(s);
482}
483
484void cast_struct_warn_2() {
485 struct st {
486 int i[100];
487 char j[2];
488 };
489
490 struct st *s = malloc(2); // expected-warning{{Cast a region whose size is not a multiple of the destination type size}}
491 free(s);
492}
493
494void cast_struct_flex_array_1() {
495 struct st {
496 int i[100];
497 char j[];
498 };
499
500 struct st *s = malloc(sizeof(struct st) + 3); // no-warning
501 free(s);
502}
503
504void cast_struct_flex_array_2() {
505 struct st {
506 int i[100];
507 char j[0];
508 };
509
510 struct st *s = malloc(sizeof(struct st) + 3); // no-warning
511 free(s);
512}
513
514void cast_struct_flex_array_3() {
515 struct st {
516 int i[100];
517 char j[1];
518 };
519
520 struct st *s = malloc(sizeof(struct st) + 3); // no-warning
521 free(s);
522}
523
524void cast_struct_flex_array_4() {
525 struct foo {
526 char f[32];
527 };
528 struct st {
529 char i[100];
530 struct foo data[];
531 };
532
533 struct st *s = malloc(sizeof(struct st) + 3 * sizeof(struct foo)); // no-warning
534 free(s);
535}
536
537void cast_struct_flex_array_5() {
538 struct foo {
539 char f[32];
540 };
541 struct st {
542 char i[100];
543 struct foo data[0];
544 };
545
546 struct st *s = malloc(sizeof(struct st) + 3 * sizeof(struct foo)); // no-warning
547 free(s);
548}
549
550void cast_struct_flex_array_6() {
551 struct foo {
552 char f[32];
553 };
554 struct st {
555 char i[100];
556 struct foo data[1];
557 };
558
559 struct st *s = malloc(sizeof(struct st) + 3 * sizeof(struct foo)); // no-warning
560 free(s);
561}
562
563void cast_struct_flex_array_warn_1() {
564 struct foo {
565 char f[32];
566 };
567 struct st {
568 char i[100];
569 struct foo data[];
570 };
571
572 struct st *s = malloc(3 * sizeof(struct st) + 3 * sizeof(struct foo)); // expected-warning{{Cast a region whose size is not a multiple of the destination type size}}
573 free(s);
574}
575
576void cast_struct_flex_array_warn_2() {
577 struct foo {
578 char f[32];
579 };
580 struct st {
581 char i[100];
582 struct foo data[0];
583 };
584
585 struct st *s = malloc(3 * sizeof(struct st) + 3 * sizeof(struct foo)); // expected-warning{{Cast a region whose size is not a multiple of the destination type size}}
586 free(s);
587}
588
589void cast_struct_flex_array_warn_3() {
590 struct foo {
591 char f[32];
592 };
593 struct st {
594 char i[100];
595 struct foo data[1];
596 };
597
598 struct st *s = malloc(3 * sizeof(struct st) + 3 * sizeof(struct foo)); // expected-warning{{Cast a region whose size is not a multiple of the destination type size}}
599 free(s);
600}
601
602void cast_struct_flex_array_warn_4() {
603 struct st {
604 int i[100];
605 int j[];
606 };
607
608 struct st *s = malloc(sizeof(struct st) + 3); // expected-warning{{Cast a region whose size is not a multiple of the destination type size}}
609 free(s);
610}
611
612void cast_struct_flex_array_warn_5() {
613 struct st {
614 int i[100];
615 int j[0];
616 };
617
618 struct st *s = malloc(sizeof(struct st) + 3); // expected-warning{{Cast a region whose size is not a multiple of the destination type size}}
619 free(s);
620}
621
622void cast_struct_flex_array_warn_6() {
623 struct st {
624 int i[100];
625 int j[1];
626 };
627
628 struct st *s = malloc(sizeof(struct st) + 3); // expected-warning{{Cast a region whose size is not a multiple of the destination type size}}
629 free(s);
630}
631
Jordy Rose2dd9b022010-06-20 04:30:57 +0000632void mallocCastToVoid() {
633 void *p = malloc(2);
634 const void *cp = p; // not crash
635 free(p);
636}
637
638void mallocCastToFP() {
639 void *p = malloc(2);
640 void (*fp)() = p; // not crash
641 free(p);
642}
643
Zhongxing Xu527ff6d2010-06-01 03:01:33 +0000644// This tests that malloc() buffers are undefined by default
645char mallocGarbage () {
646 char *buf = malloc(2);
647 char result = buf[1]; // expected-warning{{undefined}}
648 free(buf);
649 return result;
650}
651
652// This tests that calloc() buffers need to be freed
653void callocNoFree () {
654 char *buf = calloc(2,2);
Anna Zaksa1de8562013-04-06 00:41:36 +0000655 return; // expected-warning{{Potential leak of memory pointed to by 'buf'}}
Zhongxing Xu527ff6d2010-06-01 03:01:33 +0000656}
657
658// These test that calloc() buffers are zeroed by default
659char callocZeroesGood () {
660 char *buf = calloc(2,2);
661 char result = buf[3]; // no-warning
662 if (buf[1] == 0) {
663 free(buf);
664 }
665 return result; // no-warning
666}
667
668char callocZeroesBad () {
669 char *buf = calloc(2,2);
670 char result = buf[3]; // no-warning
671 if (buf[1] != 0) {
Tom Carecba9f512010-07-23 23:04:53 +0000672 free(buf); // expected-warning{{never executed}}
Zhongxing Xu527ff6d2010-06-01 03:01:33 +0000673 }
Anna Zaksa1de8562013-04-06 00:41:36 +0000674 return result; // expected-warning{{Potential leak of memory pointed to by 'buf'}}
Zhongxing Xu527ff6d2010-06-01 03:01:33 +0000675}
Anna Zaksa1b227b2012-02-08 23:16:56 +0000676
677void nullFree() {
678 int *p = 0;
679 free(p); // no warning - a nop
680}
681
682void paramFree(int *p) {
683 myfoo(p);
684 free(p); // no warning
Anna Zaks52242a62012-08-03 18:30:18 +0000685 myfoo(p); // expected-warning {{Use of memory after it is freed}}
Anna Zaksa1b227b2012-02-08 23:16:56 +0000686}
687
688int* mallocEscapeRet() {
689 int *p = malloc(12);
690 return p; // no warning
691}
692
693void mallocEscapeFoo() {
694 int *p = malloc(12);
695 myfoo(p);
696 return; // no warning
697}
698
699void mallocEscapeFree() {
700 int *p = malloc(12);
701 myfoo(p);
702 free(p);
703}
704
705void mallocEscapeFreeFree() {
706 int *p = malloc(12);
707 myfoo(p);
708 free(p);
Anna Zaks546c49c2012-02-16 22:26:12 +0000709 free(p); // expected-warning{{Attempt to free released memory}}
Anna Zaksa1b227b2012-02-08 23:16:56 +0000710}
711
712void mallocEscapeFreeUse() {
713 int *p = malloc(12);
714 myfoo(p);
715 free(p);
Anna Zaks546c49c2012-02-16 22:26:12 +0000716 myfoo(p); // expected-warning{{Use of memory after it is freed}}
Anna Zaksa1b227b2012-02-08 23:16:56 +0000717}
718
719int *myalloc();
720void myalloc2(int **p);
721
722void mallocEscapeFreeCustomAlloc() {
723 int *p = malloc(12);
724 myfoo(p);
725 free(p);
726 p = myalloc();
727 free(p); // no warning
728}
729
730void mallocEscapeFreeCustomAlloc2() {
731 int *p = malloc(12);
732 myfoo(p);
733 free(p);
734 myalloc2(&p);
735 free(p); // no warning
736}
737
738void mallocBindFreeUse() {
739 int *x = malloc(12);
740 int *y = x;
741 free(y);
Anna Zaks546c49c2012-02-16 22:26:12 +0000742 myfoo(x); // expected-warning{{Use of memory after it is freed}}
Anna Zaksa1b227b2012-02-08 23:16:56 +0000743}
744
745void mallocEscapeMalloc() {
746 int *p = malloc(12);
747 myfoo(p);
Jordan Rosee37ab502012-11-15 19:11:43 +0000748 p = malloc(12);
Anna Zaksa1de8562013-04-06 00:41:36 +0000749} // expected-warning{{Potential leak of memory pointed to by}}
Anna Zaksa1b227b2012-02-08 23:16:56 +0000750
751void mallocMalloc() {
752 int *p = malloc(12);
Jordan Rosee37ab502012-11-15 19:11:43 +0000753 p = malloc(12);
Anna Zaksa1de8562013-04-06 00:41:36 +0000754} // expected-warning {{Potential leak of memory pointed to by}}
Anna Zaksa1b227b2012-02-08 23:16:56 +0000755
756void mallocFreeMalloc() {
757 int *p = malloc(12);
758 free(p);
759 p = malloc(12);
760 free(p);
761}
762
Anna Zaks12259b42012-02-09 06:25:47 +0000763void mallocFreeUse_params() {
Anna Zaksa1b227b2012-02-08 23:16:56 +0000764 int *p = malloc(12);
765 free(p);
Anna Zaks546c49c2012-02-16 22:26:12 +0000766 myfoo(p); //expected-warning{{Use of memory after it is freed}}
Anna Zaks41b84842012-02-11 23:46:36 +0000767}
768
769void mallocFreeUse_params2() {
770 int *p = malloc(12);
771 free(p);
Anna Zaks546c49c2012-02-16 22:26:12 +0000772 myfooint(*p); //expected-warning{{Use of memory after it is freed}}
Anna Zaksa1b227b2012-02-08 23:16:56 +0000773}
774
Anna Zaks2b5bb972012-02-09 06:25:51 +0000775void mallocFailedOrNot() {
776 int *p = malloc(12);
777 if (!p)
778 free(p);
779 else
780 free(p);
781}
782
Anna Zaks31886862012-02-10 01:11:00 +0000783struct StructWithInt {
784 int g;
785};
Anna Zaks3aa52252012-02-11 21:44:39 +0000786
787int *mallocReturnFreed() {
788 int *p = malloc(12);
789 free(p);
Anna Zaks546c49c2012-02-16 22:26:12 +0000790 return p; // expected-warning {{Use of memory after it is freed}}
Anna Zaks3aa52252012-02-11 21:44:39 +0000791}
792
793int useAfterFreeStruct() {
794 struct StructWithInt *px= malloc(sizeof(struct StructWithInt));
795 px->g = 5;
796 free(px);
Anna Zaks546c49c2012-02-16 22:26:12 +0000797 return px->g; // expected-warning {{Use of memory after it is freed}}
Anna Zaks3aa52252012-02-11 21:44:39 +0000798}
799
Anna Zaks31886862012-02-10 01:11:00 +0000800void nonSymbolAsFirstArg(int *pp, struct StructWithInt *p);
801
802void mallocEscapeFooNonSymbolArg() {
803 struct StructWithInt *p = malloc(sizeof(struct StructWithInt));
804 nonSymbolAsFirstArg(&p->g, p);
805 return; // no warning
806}
807
Anna Zaksbb1ef902012-02-11 21:02:35 +0000808void mallocFailedOrNotLeak() {
809 int *p = malloc(12);
810 if (p == 0)
811 return; // no warning
812 else
Anna Zaksa1de8562013-04-06 00:41:36 +0000813 return; // expected-warning {{Potential leak of memory pointed to by}}
Anna Zaksbb1ef902012-02-11 21:02:35 +0000814}
Anna Zaks31886862012-02-10 01:11:00 +0000815
Anna Zaks5a6213d2012-02-15 00:11:28 +0000816void mallocAssignment() {
817 char *p = malloc(12);
Jordan Rosee37ab502012-11-15 19:11:43 +0000818 p = fooRetPtr();
819} // expected-warning {{leak}}
Anna Zaks5a6213d2012-02-15 00:11:28 +0000820
Anna Zaksd5157482012-02-15 00:11:22 +0000821int vallocTest() {
822 char *mem = valloc(12);
Anna Zaksa1de8562013-04-06 00:41:36 +0000823 return 0; // expected-warning {{Potential leak of memory pointed to by}}
Anna Zaksd5157482012-02-15 00:11:22 +0000824}
825
826void vallocEscapeFreeUse() {
827 int *p = valloc(12);
828 myfoo(p);
829 free(p);
Anna Zaks546c49c2012-02-16 22:26:12 +0000830 myfoo(p); // expected-warning{{Use of memory after it is freed}}
Anna Zaksd5157482012-02-15 00:11:22 +0000831}
832
Anna Zaks12259b42012-02-09 06:25:47 +0000833int *Gl;
834struct GlStTy {
835 int *x;
836};
837
838struct GlStTy GlS = {0};
839
840void GlobalFree() {
841 free(Gl);
842}
843
844void GlobalMalloc() {
845 Gl = malloc(12);
846}
847
848void GlobalStructMalloc() {
849 int *a = malloc(12);
850 GlS.x = a;
851}
852
853void GlobalStructMallocFree() {
854 int *a = malloc(12);
855 GlS.x = a;
856 free(GlS.x);
857}
Anna Zakse963fd52012-02-10 01:11:03 +0000858
Anna Zaks33c364b2012-02-16 22:26:15 +0000859char *ArrayG[12];
860
861void globalArrayTest() {
862 char *p = (char*)malloc(12);
863 ArrayG[0] = p;
864}
865
Anna Zaksd32ead82012-02-16 03:40:57 +0000866// Make sure that we properly handle a pointer stored into a local struct/array.
867typedef struct _StructWithPtr {
868 int *memP;
869} StructWithPtr;
870
871static StructWithPtr arrOfStructs[10];
872
873void testMalloc() {
874 int *x = malloc(12);
875 StructWithPtr St;
876 St.memP = x;
Jordan Rose356279c2012-08-08 18:23:31 +0000877 arrOfStructs[0] = St; // no-warning
Anna Zaksd32ead82012-02-16 03:40:57 +0000878}
879
880StructWithPtr testMalloc2() {
881 int *x = malloc(12);
882 StructWithPtr St;
883 St.memP = x;
Jordan Rose356279c2012-08-08 18:23:31 +0000884 return St; // no-warning
Anna Zaksd32ead82012-02-16 03:40:57 +0000885}
886
887int *testMalloc3() {
888 int *x = malloc(12);
889 int *y = x;
Jordan Rose356279c2012-08-08 18:23:31 +0000890 return y; // no-warning
Anna Zaksd32ead82012-02-16 03:40:57 +0000891}
892
Jordan Rose5d22fcb2013-03-20 20:35:57 +0000893void testStructLeak() {
894 StructWithPtr St;
895 St.memP = malloc(12);
Anna Zaksa1de8562013-04-06 00:41:36 +0000896 return; // expected-warning {{Potential leak of memory pointed to by 'St.memP'}}
Jordan Rose5d22fcb2013-03-20 20:35:57 +0000897}
898
Anna Zaks1fdedc92012-02-17 22:35:34 +0000899void testElemRegion1() {
900 char *x = (void*)malloc(2);
901 int *ix = (int*)x;
902 free(&(x[0]));
903}
904
905void testElemRegion2(int **pp) {
906 int *p = malloc(12);
907 *pp = p;
908 free(pp[0]);
909}
910
911void testElemRegion3(int **pp) {
912 int *p = malloc(12);
913 *pp = p;
914 free(*pp);
915}
Anna Zaksbb1ef902012-02-11 21:02:35 +0000916// Region escape testing.
917
918unsigned takePtrToPtr(int **p);
919void PassTheAddrOfAllocatedData(int f) {
920 int *p = malloc(12);
921 // We don't know what happens after the call. Should stop tracking here.
922 if (takePtrToPtr(&p))
923 f++;
924 free(p); // no warning
925}
926
927struct X {
928 int *p;
929};
930unsigned takePtrToStruct(struct X *s);
931int ** foo2(int *g, int f) {
932 int *p = malloc(12);
933 struct X *px= malloc(sizeof(struct X));
934 px->p = p;
935 // We don't know what happens after this call. Should not track px nor p.
936 if (takePtrToStruct(px))
937 f++;
938 free(p);
939 return 0;
940}
941
942struct X* RegInvalidationDetect1(struct X *s2) {
943 struct X *px= malloc(sizeof(struct X));
944 px->p = 0;
945 px = s2;
Anna Zaksa1de8562013-04-06 00:41:36 +0000946 return px; // expected-warning {{Potential leak of memory pointed to by}}
Anna Zaksbb1ef902012-02-11 21:02:35 +0000947}
948
949struct X* RegInvalidationGiveUp1() {
950 int *p = malloc(12);
951 struct X *px= malloc(sizeof(struct X));
952 px->p = p;
953 return px;
954}
955
956int **RegInvalidationDetect2(int **pp) {
957 int *p = malloc(12);
958 pp = &p;
959 pp++;
Anna Zaksa1de8562013-04-06 00:41:36 +0000960 return 0;// expected-warning {{Potential leak of memory pointed to by}}
Anna Zaksbb1ef902012-02-11 21:02:35 +0000961}
Anna Zakse963fd52012-02-10 01:11:03 +0000962
Anna Zakse963fd52012-02-10 01:11:03 +0000963extern void exit(int) __attribute__ ((__noreturn__));
964void mallocExit(int *g) {
965 struct xx *p = malloc(12);
Anna Zaksd3571e5a2012-02-11 21:02:40 +0000966 if (g != 0)
967 exit(1);
Anna Zakse963fd52012-02-10 01:11:03 +0000968 free(p);
969 return;
970}
971
Anna Zakse963fd52012-02-10 01:11:03 +0000972extern void __assert_fail (__const char *__assertion, __const char *__file,
973 unsigned int __line, __const char *__function)
974 __attribute__ ((__noreturn__));
975#define assert(expr) \
976 ((expr) ? (void)(0) : __assert_fail (#expr, __FILE__, __LINE__, __func__))
977void mallocAssert(int *g) {
978 struct xx *p = malloc(12);
979
Anna Zaksd3571e5a2012-02-11 21:02:40 +0000980 assert(g != 0);
Anna Zakse963fd52012-02-10 01:11:03 +0000981 free(p);
982 return;
983}
984
Anna Zaks41b84842012-02-11 23:46:36 +0000985void doNotInvalidateWhenPassedToSystemCalls(char *s) {
986 char *p = malloc(12);
987 strlen(p);
Jordan Rosee37ab502012-11-15 19:11:43 +0000988 strcpy(p, s);
Anton Yartsev968c60a2013-11-17 09:18:48 +0000989 strcpy(s, p);
990 strcpy(p, p);
991 memcpy(p, s, 1);
992 memcpy(s, p, 1);
993 memcpy(p, p, 1);
Jordan Rosee37ab502012-11-15 19:11:43 +0000994} // expected-warning {{leak}}
Anna Zaks41b84842012-02-11 23:46:36 +0000995
Anton Yartsev968c60a2013-11-17 09:18:48 +0000996// Treat source buffer contents as escaped.
997void escapeSourceContents(char *s) {
998 char *p = malloc(12);
999 memcpy(s, &p, 12); // no warning
1000
1001 void *p1 = malloc(7);
1002 char *a;
1003 memcpy(&a, &p1, sizeof a);
1004 // FIXME: No warning due to limitations imposed by current modelling of
1005 // 'memcpy' (regions metadata is not copied).
1006
1007 int *ptrs[2];
1008 int *allocated = (int *)malloc(4);
1009 memcpy(&ptrs[0], &allocated, sizeof(int *));
1010 // FIXME: No warning due to limitations imposed by current modelling of
1011 // 'memcpy' (regions metadata is not copied).
1012}
1013
1014void invalidateDestinationContents() {
1015 int *null = 0;
1016 int *p = (int *)malloc(4);
1017 memcpy(&p, &null, sizeof(int *));
1018
1019 int *ptrs1[2]; // expected-warning {{Potential leak of memory pointed to by}}
1020 ptrs1[0] = (int *)malloc(4);
1021 memcpy(ptrs1, &null, sizeof(int *));
1022
1023 int *ptrs2[2]; // expected-warning {{Potential memory leak}}
1024 ptrs2[0] = (int *)malloc(4);
1025 memcpy(&ptrs2[1], &null, sizeof(int *));
1026
1027 int *ptrs3[2]; // expected-warning {{Potential memory leak}}
1028 ptrs3[0] = (int *)malloc(4);
1029 memcpy(&ptrs3[0], &null, sizeof(int *));
1030} // expected-warning {{Potential memory leak}}
1031
Anna Zakse56167e2012-02-17 22:35:31 +00001032// Rely on the CString checker evaluation of the strcpy API to convey that the result of strcpy is equal to p.
1033void symbolLostWithStrcpy(char *s) {
1034 char *p = malloc(12);
1035 p = strcpy(p, s);
1036 free(p);
1037}
1038
1039
1040// The same test as the one above, but with what is actually generated on a mac.
1041static __inline char *
1042__inline_strcpy_chk (char *restrict __dest, const char *restrict __src)
1043{
1044 return __builtin___strcpy_chk (__dest, __src, __builtin_object_size (__dest, 2 > 1));
1045}
1046
1047void symbolLostWithStrcpy_InlineStrcpyVersion(char *s) {
1048 char *p = malloc(12);
1049 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));
1050 free(p);
1051}
Anna Zaks4ca45b12012-02-22 02:36:01 +00001052
1053// Here we are returning a pointer one past the allocated value. An idiom which
1054// can be used for implementing special malloc. The correct uses of this might
1055// be rare enough so that we could keep this as a warning.
1056static void *specialMalloc(int n){
1057 int *p;
1058 p = malloc( n+8 );
1059 if( p ){
1060 p[0] = n;
1061 p++;
1062 }
1063 return p;
1064}
1065
1066// Potentially, the user could free the struct by performing pointer arithmetic on the return value.
1067// This is a variation of the specialMalloc issue, though probably would be more rare in correct code.
1068int *specialMallocWithStruct() {
1069 struct StructWithInt *px= malloc(sizeof(struct StructWithInt));
1070 return &(px->g);
1071}
1072
Anna Zaks199e8e52012-02-22 03:14:20 +00001073// Test various allocation/deallocation functions.
Anna Zaks199e8e52012-02-22 03:14:20 +00001074void testStrdup(const char *s, unsigned validIndex) {
1075 char *s2 = strdup(s);
Jordan Rosee37ab502012-11-15 19:11:43 +00001076 s2[validIndex + 1] = 'b';
Anna Zaksa1de8562013-04-06 00:41:36 +00001077} // expected-warning {{Potential leak of memory pointed to by}}
Anna Zaks199e8e52012-02-22 03:14:20 +00001078
1079int testStrndup(const char *s, unsigned validIndex, unsigned size) {
1080 char *s2 = strndup(s, size);
1081 s2 [validIndex + 1] = 'b';
1082 if (s2[validIndex] != 'a')
Anna Zaksdf901a42012-02-23 21:38:21 +00001083 return 0;
Anna Zaks199e8e52012-02-22 03:14:20 +00001084 else
Anna Zaksa1de8562013-04-06 00:41:36 +00001085 return 1;// expected-warning {{Potential leak of memory pointed to by}}
Anna Zaks199e8e52012-02-22 03:14:20 +00001086}
1087
Anna Zaks40a7eb32012-02-22 19:24:52 +00001088void testStrdupContentIsDefined(const char *s, unsigned validIndex) {
1089 char *s2 = strdup(s);
1090 char result = s2[1];// no warning
1091 free(s2);
1092}
1093
Anna Zakse0c03ca2012-02-29 18:42:47 +00001094// ----------------------------------------------------------------------------
Anna Zaks07de9c12012-02-23 01:05:27 +00001095// Test the system library functions to which the pointer can escape.
Anna Zakse0c03ca2012-02-29 18:42:47 +00001096// This tests false positive suppression.
Anna Zaks07de9c12012-02-23 01:05:27 +00001097
1098// For now, we assume memory passed to pthread_specific escapes.
1099// TODO: We could check that if a new pthread binding is set, the existing
1100// binding must be freed; otherwise, a memory leak can occur.
1101void testPthereadSpecificEscape(pthread_key_t key) {
1102 void *buf = malloc(12);
1103 pthread_setspecific(key, buf); // no warning
1104}
1105
Anna Zakse0c03ca2012-02-29 18:42:47 +00001106// PR12101: Test funopen().
1107static int releasePtr(void *_ctx) {
1108 free(_ctx);
1109 return 0;
1110}
1111FILE *useFunOpen() {
1112 void *ctx = malloc(sizeof(int));
1113 FILE *f = funopen(ctx, 0, 0, 0, releasePtr); // no warning
1114 if (f == 0) {
1115 free(ctx);
1116 }
1117 return f;
1118}
1119FILE *useFunOpenNoReleaseFunction() {
1120 void *ctx = malloc(sizeof(int));
1121 FILE *f = funopen(ctx, 0, 0, 0, 0);
1122 if (f == 0) {
1123 free(ctx);
1124 }
1125 return f; // expected-warning{{leak}}
1126}
1127
Jordan Rose7ab01822012-07-02 19:27:51 +00001128static int readNothing(void *_ctx, char *buf, int size) {
1129 return 0;
1130}
1131FILE *useFunOpenReadNoRelease() {
1132 void *ctx = malloc(sizeof(int));
1133 FILE *f = funopen(ctx, readNothing, 0, 0, 0);
1134 if (f == 0) {
1135 free(ctx);
1136 }
1137 return f; // expected-warning{{leak}}
1138}
1139
Anna Zakse0c03ca2012-02-29 18:42:47 +00001140// Test setbuf, setvbuf.
1141int my_main_no_warning() {
1142 char *p = malloc(100);
1143 setvbuf(stdout, p, 0, 100);
1144 return 0;
1145}
1146int my_main_no_warning2() {
1147 char *p = malloc(100);
1148 setbuf(__stdoutp, p);
1149 return 0;
1150}
1151int my_main_warn(FILE *f) {
1152 char *p = malloc(100);
1153 setvbuf(f, p, 0, 100);
1154 return 0;// expected-warning {{leak}}
1155}
1156
Ted Kremenek9d96f842012-03-05 23:06:19 +00001157// <rdar://problem/10978247>.
1158// some people use stack allocated memory as an optimization to avoid
1159// a heap allocation for small work sizes. This tests the analyzer's
1160// understanding that the malloc'ed memory is not the same as stackBuffer.
1161void radar10978247(int myValueSize) {
1162 char stackBuffer[128];
1163 char *buffer;
1164
1165 if (myValueSize <= sizeof(stackBuffer))
1166 buffer = stackBuffer;
1167 else
1168 buffer = malloc(myValueSize);
1169
1170 // do stuff with the buffer
1171 if (buffer != stackBuffer)
1172 free(buffer);
1173}
1174
1175void radar10978247_positive(int myValueSize) {
1176 char stackBuffer[128];
1177 char *buffer;
1178
1179 if (myValueSize <= sizeof(stackBuffer))
1180 buffer = stackBuffer;
1181 else
1182 buffer = malloc(myValueSize);
1183
1184 // do stuff with the buffer
Jordan Rosee37ab502012-11-15 19:11:43 +00001185 if (buffer == stackBuffer)
Ted Kremenek9d96f842012-03-05 23:06:19 +00001186 return;
Jordan Rosee37ab502012-11-15 19:11:43 +00001187 else
1188 return; // expected-warning {{leak}}
1189}
Ted Kremenek468365b2012-04-26 05:08:26 +00001190// <rdar://problem/11269741> Previously this triggered a false positive
1191// because malloc() is known to return uninitialized memory and the binding
1192// of 'o' to 'p->n' was not getting propertly handled. Now we report a leak.
1193struct rdar11269741_a_t {
1194 struct rdar11269741_b_t {
1195 int m;
1196 } n;
1197};
1198
1199int rdar11269741(struct rdar11269741_b_t o)
1200{
1201 struct rdar11269741_a_t *p = (struct rdar11269741_a_t *) malloc(sizeof(*p));
1202 p->n = o;
1203 return p->n.m; // expected-warning {{leak}}
1204}
1205
Anna Zaks1655aee2012-05-03 02:13:56 +00001206// Pointer arithmetic, returning an ElementRegion.
1207void *radar11329382(unsigned bl) {
1208 void *ptr = malloc (16);
1209 ptr = ptr + (2 - bl);
1210 return ptr; // no warning
1211}
1212
Anna Zaks84d70a92012-05-01 21:10:29 +00001213void __assert_rtn(const char *, const char *, int, const char *) __attribute__((__noreturn__));
1214int strcmp(const char *, const char *);
1215char *a (void);
1216void radar11270219(void) {
1217 char *x = a(), *y = a();
1218 (__builtin_expect(!(x && y), 0) ? __assert_rtn(__func__, "/Users/zaks/tmp/ex.c", 24, "x && y") : (void)0);
1219 strcmp(x, y); // no warning
1220}
1221
Anna Zaks263b7e02012-05-02 00:05:20 +00001222void radar_11358224_test_double_assign_ints_positive_2()
1223{
1224 void *ptr = malloc(16);
Jordan Rosee37ab502012-11-15 19:11:43 +00001225 ptr = ptr;
1226} // expected-warning {{leak}}
Anna Zaks263b7e02012-05-02 00:05:20 +00001227
Anna Zaks228f9c72012-05-03 23:50:28 +00001228// Assume that functions which take a function pointer can free memory even if
1229// they are defined in system headers and take the const pointer to the
1230// allocated memory. (radar://11160612)
1231int const_ptr_and_callback(int, const char*, int n, void(*)(void*));
1232void r11160612_1() {
1233 char *x = malloc(12);
1234 const_ptr_and_callback(0, x, 12, free); // no - warning
1235}
1236
1237// Null is passed as callback.
1238void r11160612_2() {
1239 char *x = malloc(12);
Jordan Rosee37ab502012-11-15 19:11:43 +00001240 const_ptr_and_callback(0, x, 12, 0);
1241} // expected-warning {{leak}}
Anna Zaks228f9c72012-05-03 23:50:28 +00001242
1243// Callback is passed to a function defined in a system header.
1244void r11160612_4() {
1245 char *x = malloc(12);
1246 sqlite3_bind_text_my(0, x, 12, free); // no - warning
1247}
1248
Anna Zaks6ccfcf32012-05-03 23:50:33 +00001249// Passing callbacks in a struct.
1250void r11160612_5(StWithCallback St) {
1251 void *x = malloc(12);
1252 dealocateMemWhenDoneByVal(x, St);
1253}
1254void r11160612_6(StWithCallback St) {
1255 void *x = malloc(12);
1256 dealocateMemWhenDoneByRef(&St, x);
1257}
1258
Anna Zaks63509fb2012-05-04 17:37:16 +00001259int mySub(int, int);
1260int myAdd(int, int);
1261int fPtr(unsigned cond, int x) {
1262 return (cond ? mySub : myAdd)(x, x);
1263}
1264
Anna Zaks3563fde2012-06-07 03:57:32 +00001265// Test anti-aliasing.
Anna Zaksd3571e5a2012-02-11 21:02:40 +00001266
Anna Zakse963fd52012-02-10 01:11:03 +00001267void dependsOnValueOfPtr(int *g, unsigned f) {
1268 int *p;
1269
1270 if (f) {
1271 p = g;
1272 } else {
1273 p = malloc(12);
1274 }
1275
1276 if (p != g)
1277 free(p);
1278 else
Anna Zaks3563fde2012-06-07 03:57:32 +00001279 return; // no warning
Anna Zakse963fd52012-02-10 01:11:03 +00001280 return;
1281}
1282
Anna Zaks3563fde2012-06-07 03:57:32 +00001283int CMPRegionHeapToStack() {
1284 int x = 0;
1285 int *x1 = malloc(8);
1286 int *x2 = &x;
Anna Zaks93205d02012-06-08 00:04:40 +00001287 clang_analyzer_eval(x1 == x2); // expected-warning{{FALSE}}
Anna Zaks3563fde2012-06-07 03:57:32 +00001288 free(x1);
1289 return x;
1290}
1291
1292int CMPRegionHeapToHeap2() {
1293 int x = 0;
1294 int *x1 = malloc(8);
1295 int *x2 = malloc(8);
1296 int *x4 = x1;
1297 int *x5 = x2;
Anna Zaks93205d02012-06-08 00:04:40 +00001298 clang_analyzer_eval(x4 == x5); // expected-warning{{FALSE}}
Anna Zaks3563fde2012-06-07 03:57:32 +00001299 free(x1);
1300 free(x2);
1301 return x;
1302}
1303
1304int CMPRegionHeapToHeap() {
1305 int x = 0;
1306 int *x1 = malloc(8);
1307 int *x4 = x1;
1308 if (x1 == x4) {
1309 free(x1);
1310 return 5/x; // expected-warning{{Division by zero}}
1311 }
1312 return x;// expected-warning{{This statement is never executed}}
1313}
1314
1315int HeapAssignment() {
1316 int m = 0;
1317 int *x = malloc(4);
1318 int *y = x;
1319 *x = 5;
Anna Zaks93205d02012-06-08 00:04:40 +00001320 clang_analyzer_eval(*x != *y); // expected-warning{{FALSE}}
Anna Zaks3563fde2012-06-07 03:57:32 +00001321 free(x);
1322 return 0;
1323}
1324
Anna Zaksa7dcc992012-06-07 20:18:08 +00001325int *retPtr();
1326int *retPtrMightAlias(int *x);
1327int cmpHeapAllocationToUnknown() {
1328 int zero = 0;
1329 int *yBefore = retPtr();
1330 int *m = malloc(8);
1331 int *yAfter = retPtrMightAlias(m);
Anna Zaks93205d02012-06-08 00:04:40 +00001332 clang_analyzer_eval(yBefore == m); // expected-warning{{FALSE}}
1333 clang_analyzer_eval(yAfter == m); // expected-warning{{FALSE}}
Anna Zaksa7dcc992012-06-07 20:18:08 +00001334 free(m);
1335 return 0;
1336}
1337
Jordan Rose5d22fcb2013-03-20 20:35:57 +00001338void localArrayTest() {
1339 char *p = (char*)malloc(12);
1340 char *ArrayL[12];
1341 ArrayL[0] = p;
1342} // expected-warning {{leak}}
1343
1344void localStructTest() {
1345 StructWithPtr St;
1346 StructWithPtr *pSt = &St;
1347 pSt->memP = malloc(12);
Anna Zaksa1de8562013-04-06 00:41:36 +00001348} // expected-warning{{Potential leak of memory pointed to by}}
Jordan Rose5d22fcb2013-03-20 20:35:57 +00001349
Jordan Rose3ba8c792012-11-27 02:37:49 +00001350#ifdef __INTPTR_TYPE__
Ted Kremenekf56d4f22012-05-01 21:58:29 +00001351// Test double assignment through integers.
Jordan Rose3ba8c792012-11-27 02:37:49 +00001352typedef __INTPTR_TYPE__ intptr_t;
1353typedef unsigned __INTPTR_TYPE__ uintptr_t;
1354
1355static intptr_t glob;
Ted Kremenekf56d4f22012-05-01 21:58:29 +00001356void test_double_assign_ints()
1357{
1358 void *ptr = malloc (16); // no-warning
Jordan Rose3ba8c792012-11-27 02:37:49 +00001359 glob = (intptr_t)(uintptr_t)ptr;
Ted Kremenekf56d4f22012-05-01 21:58:29 +00001360}
1361
1362void test_double_assign_ints_positive()
1363{
1364 void *ptr = malloc(16);
Jordan Rose3ba8c792012-11-27 02:37:49 +00001365 (void*)(intptr_t)(uintptr_t)ptr; // expected-warning {{unused}}
Jordan Rosee37ab502012-11-15 19:11:43 +00001366} // expected-warning {{leak}}
Jordan Rose3ba8c792012-11-27 02:37:49 +00001367#endif
Jordan Rosede409b62012-06-16 00:09:20 +00001368
1369void testCGContextNoLeak()
1370{
1371 void *ptr = malloc(16);
1372 CGContextRef context = CGBitmapContextCreate(ptr);
1373
1374 // Because you can get the data back out like this, even much later,
1375 // CGBitmapContextCreate is one of our "stop-tracking" exceptions.
1376 free(CGBitmapContextGetData(context));
1377}
1378
1379void testCGContextLeak()
1380{
1381 void *ptr = malloc(16);
1382 CGContextRef context = CGBitmapContextCreate(ptr);
1383 // However, this time we're just leaking the data, because the context
1384 // object doesn't escape and it hasn't been freed in this function.
1385}
1386
Anna Zaks886dfb82012-06-20 23:35:57 +00001387// Allow xpc context to escape. radar://11635258
1388// TODO: Would be great if we checked that the finalize_connection_context actually releases it.
1389static void finalize_connection_context(void *ctx) {
1390 int *context = ctx;
1391 free(context);
1392}
1393void foo (xpc_connection_t peer) {
1394 int *ctx = calloc(1, sizeof(int));
1395 xpc_connection_set_context(peer, ctx);
1396 xpc_connection_set_finalizer_f(peer, finalize_connection_context);
1397 xpc_connection_resume(peer);
1398}
1399
Anna Zaks52242a62012-08-03 18:30:18 +00001400// Make sure we catch errors when we free in a function which does not allocate memory.
1401void freeButNoMalloc(int *p, int x){
1402 if (x) {
1403 free(p);
1404 //user forgot a return here.
1405 }
1406 free(p); // expected-warning {{Attempt to free released memory}}
1407}
Anna Zaks6ce686e2012-08-04 02:04:27 +00001408
1409struct HasPtr {
Anna Zaksfe6eb672012-08-24 02:28:20 +00001410 char *p;
Anna Zaks6ce686e2012-08-04 02:04:27 +00001411};
1412
Anna Zaksfe6eb672012-08-24 02:28:20 +00001413char* reallocButNoMalloc(struct HasPtr *a, int c, int size) {
Anna Zaks6ce686e2012-08-04 02:04:27 +00001414 int *s;
Anna Zaksfe6eb672012-08-24 02:28:20 +00001415 char *b = realloc(a->p, size);
1416 char *m = realloc(a->p, size); // expected-warning {{Attempt to free released memory}}
Devin Coughline39bd402015-09-16 22:03:05 +00001417 // We don't expect a use-after-free for a->P here because the warning above
1418 // is a sink.
1419 return a->p; // no-warning
Anna Zaks6ce686e2012-08-04 02:04:27 +00001420}
Jordan Rose356279c2012-08-08 18:23:31 +00001421
Anna Zaksfe6eb672012-08-24 02:28:20 +00001422// We should not warn in this case since the caller will presumably free a->p in all cases.
1423int reallocButNoMallocPR13674(struct HasPtr *a, int c, int size) {
1424 int *s;
1425 char *b = realloc(a->p, size);
1426 if (b == 0)
1427 return -1;
1428 a->p = b;
1429 return 0;
1430}
1431
Anna Zaks75cfbb62012-09-12 22:57:34 +00001432// Test realloc with no visible malloc.
1433void *test(void *ptr) {
1434 void *newPtr = realloc(ptr, 4);
1435 if (newPtr == 0) {
1436 if (ptr)
1437 free(ptr); // no-warning
1438 }
1439 return newPtr;
1440}
1441
Jordan Roseb5b0fc12012-11-15 19:11:27 +00001442
1443char *testLeakWithinReturn(char *str) {
1444 return strdup(strdup(str)); // expected-warning{{leak}}
1445}
1446
Anna Zaksacdc13c2013-02-07 23:05:43 +00001447void passConstPtr(const char * ptr);
1448
1449void testPassConstPointer() {
1450 char * string = malloc(sizeof(char)*10);
1451 passConstPtr(string);
1452 return; // expected-warning {{leak}}
1453}
1454
1455void testPassConstPointerIndirectly() {
1456 char *p = malloc(1);
1457 p++;
1458 memcmp(p, p, sizeof(&p));
1459 return; // expected-warning {{leak}}
1460}
1461
Anna Zaksacdc13c2013-02-07 23:05:43 +00001462void testPassConstPointerIndirectlyStruct() {
1463 struct HasPtr hp;
1464 hp.p = malloc(10);
1465 memcmp(&hp, &hp, sizeof(hp));
Anna Zaksa1de8562013-04-06 00:41:36 +00001466 return; // expected-warning {{Potential leak of memory pointed to by 'hp.p'}}
Anna Zaksacdc13c2013-02-07 23:05:43 +00001467}
1468
1469void testPassToSystemHeaderFunctionIndirectlyStruct() {
1470 SomeStruct ss;
1471 ss.p = malloc(1);
Jordan Rose757fbb02013-05-10 17:07:16 +00001472 fakeSystemHeaderCall(&ss); // invalidates ss, making ss.p unreachable
1473 // Technically a false negative here -- we know the system function won't free
1474 // ss.p, but nothing else will either!
1475} // no-warning
1476
1477void testPassToSystemHeaderFunctionIndirectlyStructFree() {
1478 SomeStruct ss;
1479 ss.p = malloc(1);
1480 fakeSystemHeaderCall(&ss); // invalidates ss, making ss.p unreachable
1481 free(ss.p);
1482} // no-warning
1483
1484void testPassToSystemHeaderFunctionIndirectlyArray() {
1485 int *p[1];
1486 p[0] = malloc(sizeof(int));
1487 fakeSystemHeaderCallIntPtr(p); // invalidates p, making p[0] unreachable
1488 // Technically a false negative here -- we know the system function won't free
1489 // p[0], but nothing else will either!
1490} // no-warning
1491
1492void testPassToSystemHeaderFunctionIndirectlyArrayFree() {
1493 int *p[1];
1494 p[0] = malloc(sizeof(int));
1495 fakeSystemHeaderCallIntPtr(p); // invalidates p, making p[0] unreachable
1496 free(p[0]);
1497} // no-warning
Anna Zaks258f9352013-02-06 00:01:14 +00001498
Anna Zaksc89ad072013-02-07 23:05:47 +00001499int *testOffsetAllocate(size_t size) {
1500 int *memoryBlock = (int *)malloc(size + sizeof(int));
1501 return &memoryBlock[1]; // no-warning
1502}
1503
1504void testOffsetDeallocate(int *memoryBlock) {
1505 free(&memoryBlock[-1]); // no-warning
1506}
1507
1508void testOffsetOfRegionFreed() {
1509 __int64_t * array = malloc(sizeof(__int64_t)*2);
1510 array += 1;
1511 free(&array[0]); // expected-warning{{Argument to free() is offset by 8 bytes from the start of memory allocated by malloc()}}
1512}
1513
1514void testOffsetOfRegionFreed2() {
1515 __int64_t *p = malloc(sizeof(__int64_t)*2);
1516 p += 1;
1517 free(p); // expected-warning{{Argument to free() is offset by 8 bytes from the start of memory allocated by malloc()}}
1518}
1519
1520void testOffsetOfRegionFreed3() {
1521 char *r = malloc(sizeof(char));
1522 r = r - 10;
1523 free(r); // expected-warning {{Argument to free() is offset by -10 bytes from the start of memory allocated by malloc()}}
1524}
1525
1526void testOffsetOfRegionFreedAfterFunctionCall() {
1527 int *p = malloc(sizeof(int)*2);
1528 p += 1;
1529 myfoo(p);
Anna Zaks93a21a82013-04-09 00:30:28 +00001530 free(p); // expected-warning{{Argument to free() is offset by 4 bytes from the start of memory allocated by malloc()}}
Anna Zaksc89ad072013-02-07 23:05:47 +00001531}
1532
1533void testFixManipulatedPointerBeforeFree() {
1534 int * array = malloc(sizeof(int)*2);
1535 array += 1;
1536 free(&array[-1]); // no-warning
1537}
1538
1539void testFixManipulatedPointerBeforeFree2() {
1540 char *r = malloc(sizeof(char));
1541 r = r + 10;
1542 free(r-10); // no-warning
1543}
1544
1545void freeOffsetPointerPassedToFunction() {
1546 __int64_t *p = malloc(sizeof(__int64_t)*2);
1547 p[1] = 0;
1548 p += 1;
1549 myfooint(*p); // not passing the pointer, only a value pointed by pointer
1550 free(p); // expected-warning {{Argument to free() is offset by 8 bytes from the start of memory allocated by malloc()}}
1551}
1552
1553int arbitraryInt();
1554void freeUnknownOffsetPointer() {
1555 char *r = malloc(sizeof(char));
1556 r = r + arbitraryInt(); // unable to reason about what the offset might be
1557 free(r); // no-warning
1558}
1559
1560void testFreeNonMallocPointerWithNoOffset() {
1561 char c;
1562 char *r = &c;
1563 r = r + 10;
1564 free(r-10); // expected-warning {{Argument to free() is the address of the local variable 'c', which is not memory allocated by malloc()}}
1565}
1566
1567void testFreeNonMallocPointerWithOffset() {
1568 char c;
1569 char *r = &c;
1570 free(r+1); // expected-warning {{Argument to free() is the address of the local variable 'c', which is not memory allocated by malloc()}}
1571}
1572
1573void testOffsetZeroDoubleFree() {
1574 int *array = malloc(sizeof(int)*2);
1575 int *p = &array[0];
1576 free(p);
1577 free(&array[0]); // expected-warning{{Attempt to free released memory}}
1578}
1579
1580void testOffsetPassedToStrlen() {
1581 char * string = malloc(sizeof(char)*10);
1582 string += 1;
Anna Zaksa1de8562013-04-06 00:41:36 +00001583 int length = strlen(string); // expected-warning {{Potential leak of memory pointed to by 'string'}}
Anna Zaksc89ad072013-02-07 23:05:47 +00001584}
1585
1586void testOffsetPassedToStrlenThenFree() {
1587 char * string = malloc(sizeof(char)*10);
1588 string += 1;
1589 int length = strlen(string);
1590 free(string); // expected-warning {{Argument to free() is offset by 1 byte from the start of memory allocated by malloc()}}
1591}
1592
1593void testOffsetPassedAsConst() {
1594 char * string = malloc(sizeof(char)*10);
1595 string += 1;
1596 passConstPtr(string);
1597 free(string); // expected-warning {{Argument to free() is offset by 1 byte from the start of memory allocated by malloc()}}
1598}
Anna Zaks258f9352013-02-06 00:01:14 +00001599
Anna Zaksbda130f2013-03-15 23:34:29 +00001600char **_vectorSegments;
1601int _nVectorSegments;
1602
1603void poolFreeC(void* s) {
1604 free(s); // no-warning
1605}
1606void freeMemory() {
1607 while (_nVectorSegments) {
1608 poolFreeC(_vectorSegments[_nVectorSegments++]);
1609 }
1610}
Jordan Rose5d22fcb2013-03-20 20:35:57 +00001611
Jordan Rose2f8b0222013-08-15 17:22:06 +00001612// PR16730
1613void testReallocEscaped(void **memory) {
1614 *memory = malloc(47);
1615 char *new_memory = realloc(*memory, 47);
1616 if (new_memory != 0) {
1617 *memory = new_memory;
1618 }
1619}
1620
Jordan Rose60619a62013-08-19 16:27:34 +00001621// PR16558
1622void *smallocNoWarn(size_t size) {
1623 if (size == 0) {
1624 return malloc(1); // this branch is never called
1625 }
1626 else {
1627 return malloc(size);
1628 }
1629}
1630
1631char *dupstrNoWarn(const char *s) {
1632 const int len = strlen(s);
1633 char *p = (char*) smallocNoWarn(len + 1);
1634 strcpy(p, s); // no-warning
1635 return p;
1636}
1637
1638void *smallocWarn(size_t size) {
1639 if (size == 2) {
1640 return malloc(1);
1641 }
1642 else {
1643 return malloc(size);
1644 }
1645}
1646
1647char *dupstrWarn(const char *s) {
1648 const int len = strlen(s);
1649 char *p = (char*) smallocWarn(len + 1);
1650 strcpy(p, s); // expected-warning{{String copy function overflows destination buffer}}
1651 return p;
1652}
Jordan Rose2f8b0222013-08-15 17:22:06 +00001653
Anna Zaksf5308fa2013-12-06 19:28:16 +00001654int *radar15580979() {
1655 int *data = (int *)malloc(32);
1656 int *p = data ?: (int*)malloc(32); // no warning
1657 return p;
1658}
1659
Anna Zaksfe1eca52015-10-27 20:19:45 +00001660// Some data structures may hold onto the pointer and free it later.
1661void testEscapeThroughSystemCallTakingVoidPointer1(void *queue) {
1662 int *data = (int *)malloc(32);
1663 fake_insque(queue, data); // no warning
1664}
1665
1666void testEscapeThroughSystemCallTakingVoidPointer2(fake_rb_tree_t *rbt) {
1667 int *data = (int *)malloc(32);
1668 fake_rb_tree_init(rbt, data);
1669} //expected-warning{{Potential leak}}
1670
1671void testEscapeThroughSystemCallTakingVoidPointer3(fake_rb_tree_t *rbt) {
1672 int *data = (int *)malloc(32);
1673 fake_rb_tree_init(rbt, data);
1674 fake_rb_tree_insert_node(rbt, data); // no warning
1675}
1676
Jordan Rose5d22fcb2013-03-20 20:35:57 +00001677// ----------------------------------------------------------------------------
1678// False negatives.
1679
1680void testMallocWithParam(int **p) {
1681 *p = (int*) malloc(sizeof(int));
1682 *p = 0; // FIXME: should warn here
1683}
1684
1685void testMallocWithParam_2(int **p) {
1686 *p = (int*) malloc(sizeof(int)); // no-warning
1687}
Jordan Rose757fbb02013-05-10 17:07:16 +00001688
1689void testPassToSystemHeaderFunctionIndirectly() {
1690 int *p = malloc(4);
1691 p++;
1692 fakeSystemHeaderCallInt(p);
1693 // FIXME: This is a leak: if we think a system function won't free p, it
1694 // won't free (p-1) either.
1695}