blob: 29f5aa69ca56cbfff67a410e350cfce1a63a0a84 [file] [log] [blame]
Ted Kremenekcdc3a892012-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 Takumi7a290702012-11-19 10:00:59 +00002
Chandler Carruth1b22cec2012-09-12 01:11:10 +00003#include "Inputs/system-header-simulator.h"
Anna Zaks15d0ae12012-02-11 23:46:36 +00004
Anna Zaksadccc3f2012-06-08 00:04:40 +00005void clang_analyzer_eval(int);
6
Eli Friedman2f005522009-11-14 04:23:25 +00007typedef __typeof(sizeof(int)) size_t;
Ted Kremenekc3607752009-11-13 20:03:22 +00008void *malloc(size_t);
Anna Zaksb16ce452012-02-15 00:11:22 +00009void *valloc(size_t);
Ted Kremenekc3607752009-11-13 20:03:22 +000010void free(void *);
Zhongxing Xud9c84c82009-12-12 12:29:38 +000011void *realloc(void *ptr, size_t size);
Anna Zaks40add292012-02-15 00:11:25 +000012void *reallocf(void *ptr, size_t size);
Zhongxing Xud9c84c82009-12-12 12:29:38 +000013void *calloc(size_t nmemb, size_t size);
Anna Zaks14345182012-05-18 01:16:10 +000014char *strdup(const char *s);
15char *strndup(const char *s, size_t n);
Anna Zaks233e26a2013-02-07 23:05:43 +000016int memcmp(const void *s1, const void *s2, size_t n);
Ted Kremenekdd0e4902010-07-31 01:52:11 +000017
Anna Zaks91c2a112012-02-08 23:16:56 +000018void myfoo(int *p);
19void myfooint(int p);
Anna Zaksebc1d322012-02-15 00:11:28 +000020char *fooRetPtr();
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +000021
22void f1() {
Zhongxing Xuab280992010-05-25 04:59:19 +000023 int *p = malloc(12);
Jordan Rose919e8a12012-08-08 18:23:36 +000024 return; // expected-warning{{Memory is never released; potential leak of memory pointed to by 'p'}}
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +000025}
26
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +000027void f2() {
Zhongxing Xuab280992010-05-25 04:59:19 +000028 int *p = malloc(12);
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +000029 free(p);
Anna Zaksfebdc322012-02-16 22:26:12 +000030 free(p); // expected-warning{{Attempt to free released memory}}
Zhongxing Xufc7ac8f2009-11-13 07:48:11 +000031}
Ted Kremenekc764d4b2009-11-13 20:00:28 +000032
Lenny Maiorani4d8d8032011-04-27 14:49:29 +000033void f2_realloc_0() {
34 int *p = malloc(12);
35 realloc(p,0);
Anna Zaksfebdc322012-02-16 22:26:12 +000036 realloc(p,0); // expected-warning{{Attempt to free released memory}}
Lenny Maiorani4d8d8032011-04-27 14:49:29 +000037}
38
39void f2_realloc_1() {
40 int *p = malloc(12);
Zhongxing Xud56763f2011-09-01 04:53:59 +000041 int *q = realloc(p,0); // no-warning
Lenny Maiorani4d8d8032011-04-27 14:49:29 +000042}
43
Anna Zaksc8bb3be2012-02-13 18:05:39 +000044void reallocNotNullPtr(unsigned sizeIn) {
45 unsigned size = 12;
46 char *p = (char*)malloc(size);
47 if (p) {
48 char *q = (char*)realloc(p, sizeIn);
Jordan Rose919e8a12012-08-08 18:23:36 +000049 char x = *q; // expected-warning {{Memory is never released; potential leak of memory pointed to by 'q'}}
Anna Zaksc8bb3be2012-02-13 18:05:39 +000050 }
51}
52
53int *realloctest1() {
54 int *q = malloc(12);
55 q = realloc(q, 20);
56 return q; // no warning - returning the allocated value
57}
58
59// p should be freed if realloc fails.
60void reallocFails() {
61 char *p = malloc(12);
62 char *r = realloc(p, 12+1);
63 if (!r) {
64 free(p);
65 } else {
66 free(r);
67 }
68}
69
Anna Zaks30838b92012-02-13 20:57:07 +000070void reallocSizeZero1() {
71 char *p = malloc(12);
72 char *r = realloc(p, 0);
73 if (!r) {
Anna Zaksede875b2012-08-03 18:30:18 +000074 free(p); // expected-warning {{Attempt to free released memory}}
Anna Zaks30838b92012-02-13 20:57:07 +000075 } else {
76 free(r);
77 }
78}
79
80void reallocSizeZero2() {
81 char *p = malloc(12);
82 char *r = realloc(p, 0);
83 if (!r) {
Anna Zaksede875b2012-08-03 18:30:18 +000084 free(p); // expected-warning {{Attempt to free released memory}}
Anna Zaks30838b92012-02-13 20:57:07 +000085 } else {
86 free(r);
87 }
Anna Zaksfebdc322012-02-16 22:26:12 +000088 free(p); // expected-warning {{Attempt to free released memory}}
Anna Zaks30838b92012-02-13 20:57:07 +000089}
90
91void reallocSizeZero3() {
92 char *p = malloc(12);
93 char *r = realloc(p, 0);
94 free(r);
95}
96
97void reallocSizeZero4() {
98 char *r = realloc(0, 0);
99 free(r);
100}
101
102void reallocSizeZero5() {
103 char *r = realloc(0, 0);
104}
105
106void reallocPtrZero1() {
Jordan Rose63bc1862012-11-15 19:11:43 +0000107 char *r = realloc(0, 12);
108} // expected-warning {{Memory is never released; potential leak of memory pointed to by 'r'}}
Anna Zaks30838b92012-02-13 20:57:07 +0000109
110void reallocPtrZero2() {
111 char *r = realloc(0, 12);
112 if (r)
113 free(r);
114}
115
116void reallocPtrZero3() {
117 char *r = realloc(0, 12);
118 free(r);
119}
120
Anna Zaksb276bd92012-02-14 00:26:13 +0000121void reallocRadar6337483_1() {
122 char *buf = malloc(100);
123 buf = (char*)realloc(buf, 0x1000000);
124 if (!buf) {
Anna Zaks3d7c44e2012-03-21 19:45:08 +0000125 return;// expected-warning {{Memory is never released; potential leak}}
Anna Zaksb276bd92012-02-14 00:26:13 +0000126 }
127 free(buf);
128}
129
130void reallocRadar6337483_2() {
131 char *buf = malloc(100);
132 char *buf2 = (char*)realloc(buf, 0x1000000);
Jordan Rose63bc1862012-11-15 19:11:43 +0000133 if (!buf2) {
Anna Zaksb276bd92012-02-14 00:26:13 +0000134 ;
135 } else {
136 free(buf2);
137 }
Jordan Rose63bc1862012-11-15 19:11:43 +0000138} // expected-warning {{Memory is never released; potential leak}}
Anna Zaksb276bd92012-02-14 00:26:13 +0000139
140void reallocRadar6337483_3() {
141 char * buf = malloc(100);
142 char * tmp;
143 tmp = (char*)realloc(buf, 0x1000000);
144 if (!tmp) {
145 free(buf);
146 return;
147 }
148 buf = tmp;
149 free(buf);
150}
151
152void reallocRadar6337483_4() {
153 char *buf = malloc(100);
154 char *buf2 = (char*)realloc(buf, 0x1000000);
155 if (!buf2) {
Anna Zaks3d7c44e2012-03-21 19:45:08 +0000156 return; // expected-warning {{Memory is never released; potential leak}}
Anna Zaksb276bd92012-02-14 00:26:13 +0000157 } else {
158 free(buf2);
159 }
160}
161
Anna Zaks40add292012-02-15 00:11:25 +0000162int *reallocfTest1() {
163 int *q = malloc(12);
164 q = reallocf(q, 20);
165 return q; // no warning - returning the allocated value
166}
167
168void reallocfRadar6337483_4() {
169 char *buf = malloc(100);
170 char *buf2 = (char*)reallocf(buf, 0x1000000);
171 if (!buf2) {
172 return; // no warning - reallocf frees even on failure
173 } else {
174 free(buf2);
175 }
176}
177
178void reallocfRadar6337483_3() {
179 char * buf = malloc(100);
180 char * tmp;
181 tmp = (char*)reallocf(buf, 0x1000000);
182 if (!tmp) {
Anna Zaksfebdc322012-02-16 22:26:12 +0000183 free(buf); // expected-warning {{Attempt to free released memory}}
Anna Zaks40add292012-02-15 00:11:25 +0000184 return;
185 }
186 buf = tmp;
187 free(buf);
188}
189
190void reallocfPtrZero1() {
Jordan Rose63bc1862012-11-15 19:11:43 +0000191 char *r = reallocf(0, 12);
192} // expected-warning {{Memory is never released; potential leak}}
Anna Zaks40add292012-02-15 00:11:25 +0000193
194
Zhongxing Xu243fde92009-11-17 07:54:15 +0000195// This case tests that storing malloc'ed memory to a static variable which is
196// then returned is not leaked. In the absence of known contracts for functions
197// or inter-procedural analysis, this is a conservative answer.
Ted Kremenekc764d4b2009-11-13 20:00:28 +0000198int *f3() {
199 static int *p = 0;
Zhongxing Xuab280992010-05-25 04:59:19 +0000200 p = malloc(12);
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000201 return p; // no-warning
Ted Kremenekc764d4b2009-11-13 20:00:28 +0000202}
203
Zhongxing Xu243fde92009-11-17 07:54:15 +0000204// This case tests that storing malloc'ed memory to a static global variable
205// which is then returned is not leaked. In the absence of known contracts for
206// functions or inter-procedural analysis, this is a conservative answer.
Ted Kremenekc764d4b2009-11-13 20:00:28 +0000207static int *p_f4 = 0;
208int *f4() {
Zhongxing Xuab280992010-05-25 04:59:19 +0000209 p_f4 = malloc(12);
Zhongxing Xu4985e3e2009-11-17 08:58:18 +0000210 return p_f4; // no-warning
Ted Kremenekc764d4b2009-11-13 20:00:28 +0000211}
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000212
213int *f5() {
Zhongxing Xuab280992010-05-25 04:59:19 +0000214 int *q = malloc(12);
Zhongxing Xud9c84c82009-12-12 12:29:38 +0000215 q = realloc(q, 20);
216 return q; // no-warning
217}
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000218
219void f6() {
Zhongxing Xuab280992010-05-25 04:59:19 +0000220 int *p = malloc(12);
Zhongxing Xub94b81a2009-12-31 06:13:07 +0000221 if (!p)
222 return; // no-warning
223 else
224 free(p);
225}
Zhongxing Xu425c7ed2010-01-18 04:01:40 +0000226
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000227void f6_realloc() {
228 int *p = malloc(12);
229 if (!p)
230 return; // no-warning
231 else
232 realloc(p,0);
233}
234
235
Zhongxing Xu425c7ed2010-01-18 04:01:40 +0000236char *doit2();
237void pr6069() {
238 char *buf = doit2();
239 free(buf);
240}
Zhongxing Xu181cc3d2010-02-14 06:49:48 +0000241
242void pr6293() {
243 free(0);
244}
Zhongxing Xuc8023782010-03-10 04:58:55 +0000245
246void f7() {
247 char *x = (char*) malloc(4);
248 free(x);
Anna Zaksfebdc322012-02-16 22:26:12 +0000249 x[0] = 'a'; // expected-warning{{Use of memory after it is freed}}
Zhongxing Xuc8023782010-03-10 04:58:55 +0000250}
Zhongxing Xuab280992010-05-25 04:59:19 +0000251
Anna Zaks14345182012-05-18 01:16:10 +0000252void f8() {
253 char *x = (char*) malloc(4);
254 free(x);
255 char *y = strndup(x, 4); // expected-warning{{Use of memory after it is freed}}
256}
257
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000258void f7_realloc() {
259 char *x = (char*) malloc(4);
260 realloc(x,0);
Anna Zaksfebdc322012-02-16 22:26:12 +0000261 x[0] = 'a'; // expected-warning{{Use of memory after it is freed}}
Lenny Maiorani4d8d8032011-04-27 14:49:29 +0000262}
263
Zhongxing Xuab280992010-05-25 04:59:19 +0000264void PR6123() {
Ted Kremenekc4bac8e2012-08-16 17:45:23 +0000265 int *x = malloc(11); // expected-warning{{Cast a region whose size is not a multiple of the destination type size}}
Zhongxing Xuab280992010-05-25 04:59:19 +0000266}
267
268void PR7217() {
Ted Kremenekc4bac8e2012-08-16 17:45:23 +0000269 int *buf = malloc(2); // expected-warning{{Cast a region whose size is not a multiple of the destination type size}}
Zhongxing Xuab280992010-05-25 04:59:19 +0000270 buf[1] = 'c'; // not crash
Zhongxing Xuab280992010-05-25 04:59:19 +0000271}
Jordy Rosec580f2e2010-06-20 04:30:57 +0000272
273void mallocCastToVoid() {
274 void *p = malloc(2);
275 const void *cp = p; // not crash
276 free(p);
277}
278
279void mallocCastToFP() {
280 void *p = malloc(2);
281 void (*fp)() = p; // not crash
282 free(p);
283}
284
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000285// This tests that malloc() buffers are undefined by default
286char mallocGarbage () {
287 char *buf = malloc(2);
288 char result = buf[1]; // expected-warning{{undefined}}
289 free(buf);
290 return result;
291}
292
293// This tests that calloc() buffers need to be freed
294void callocNoFree () {
295 char *buf = calloc(2,2);
296 return; // expected-warning{{never released}}
297}
298
299// These test that calloc() buffers are zeroed by default
300char callocZeroesGood () {
301 char *buf = calloc(2,2);
302 char result = buf[3]; // no-warning
303 if (buf[1] == 0) {
304 free(buf);
305 }
306 return result; // no-warning
307}
308
309char callocZeroesBad () {
310 char *buf = calloc(2,2);
311 char result = buf[3]; // no-warning
312 if (buf[1] != 0) {
Tom Carec4b5bd82010-07-23 23:04:53 +0000313 free(buf); // expected-warning{{never executed}}
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000314 }
315 return result; // expected-warning{{never released}}
316}
Anna Zaks91c2a112012-02-08 23:16:56 +0000317
318void nullFree() {
319 int *p = 0;
320 free(p); // no warning - a nop
321}
322
323void paramFree(int *p) {
324 myfoo(p);
325 free(p); // no warning
Anna Zaksede875b2012-08-03 18:30:18 +0000326 myfoo(p); // expected-warning {{Use of memory after it is freed}}
Anna Zaks91c2a112012-02-08 23:16:56 +0000327}
328
329int* mallocEscapeRet() {
330 int *p = malloc(12);
331 return p; // no warning
332}
333
334void mallocEscapeFoo() {
335 int *p = malloc(12);
336 myfoo(p);
337 return; // no warning
338}
339
340void mallocEscapeFree() {
341 int *p = malloc(12);
342 myfoo(p);
343 free(p);
344}
345
346void mallocEscapeFreeFree() {
347 int *p = malloc(12);
348 myfoo(p);
349 free(p);
Anna Zaksfebdc322012-02-16 22:26:12 +0000350 free(p); // expected-warning{{Attempt to free released memory}}
Anna Zaks91c2a112012-02-08 23:16:56 +0000351}
352
353void mallocEscapeFreeUse() {
354 int *p = malloc(12);
355 myfoo(p);
356 free(p);
Anna Zaksfebdc322012-02-16 22:26:12 +0000357 myfoo(p); // expected-warning{{Use of memory after it is freed}}
Anna Zaks91c2a112012-02-08 23:16:56 +0000358}
359
360int *myalloc();
361void myalloc2(int **p);
362
363void mallocEscapeFreeCustomAlloc() {
364 int *p = malloc(12);
365 myfoo(p);
366 free(p);
367 p = myalloc();
368 free(p); // no warning
369}
370
371void mallocEscapeFreeCustomAlloc2() {
372 int *p = malloc(12);
373 myfoo(p);
374 free(p);
375 myalloc2(&p);
376 free(p); // no warning
377}
378
379void mallocBindFreeUse() {
380 int *x = malloc(12);
381 int *y = x;
382 free(y);
Anna Zaksfebdc322012-02-16 22:26:12 +0000383 myfoo(x); // expected-warning{{Use of memory after it is freed}}
Anna Zaks91c2a112012-02-08 23:16:56 +0000384}
385
386void mallocEscapeMalloc() {
387 int *p = malloc(12);
388 myfoo(p);
Jordan Rose63bc1862012-11-15 19:11:43 +0000389 p = malloc(12);
390} // expected-warning{{Memory is never released; potential leak}}
Anna Zaks91c2a112012-02-08 23:16:56 +0000391
392void mallocMalloc() {
393 int *p = malloc(12);
Jordan Rose63bc1862012-11-15 19:11:43 +0000394 p = malloc(12);
395} // expected-warning {{Memory is never released; potential leak}}
Anna Zaks91c2a112012-02-08 23:16:56 +0000396
397void mallocFreeMalloc() {
398 int *p = malloc(12);
399 free(p);
400 p = malloc(12);
401 free(p);
402}
403
Anna Zakscdfec5e2012-02-09 06:25:47 +0000404void mallocFreeUse_params() {
Anna Zaks91c2a112012-02-08 23:16:56 +0000405 int *p = malloc(12);
406 free(p);
Anna Zaksfebdc322012-02-16 22:26:12 +0000407 myfoo(p); //expected-warning{{Use of memory after it is freed}}
Anna Zaks15d0ae12012-02-11 23:46:36 +0000408}
409
410void mallocFreeUse_params2() {
411 int *p = malloc(12);
412 free(p);
Anna Zaksfebdc322012-02-16 22:26:12 +0000413 myfooint(*p); //expected-warning{{Use of memory after it is freed}}
Anna Zaks91c2a112012-02-08 23:16:56 +0000414}
415
Anna Zaksff3b9fd2012-02-09 06:25:51 +0000416void mallocFailedOrNot() {
417 int *p = malloc(12);
418 if (!p)
419 free(p);
420 else
421 free(p);
422}
423
Anna Zakse9ef5622012-02-10 01:11:00 +0000424struct StructWithInt {
425 int g;
426};
Anna Zaks0860cd02012-02-11 21:44:39 +0000427
428int *mallocReturnFreed() {
429 int *p = malloc(12);
430 free(p);
Anna Zaksfebdc322012-02-16 22:26:12 +0000431 return p; // expected-warning {{Use of memory after it is freed}}
Anna Zaks0860cd02012-02-11 21:44:39 +0000432}
433
434int useAfterFreeStruct() {
435 struct StructWithInt *px= malloc(sizeof(struct StructWithInt));
436 px->g = 5;
437 free(px);
Anna Zaksfebdc322012-02-16 22:26:12 +0000438 return px->g; // expected-warning {{Use of memory after it is freed}}
Anna Zaks0860cd02012-02-11 21:44:39 +0000439}
440
Anna Zakse9ef5622012-02-10 01:11:00 +0000441void nonSymbolAsFirstArg(int *pp, struct StructWithInt *p);
442
443void mallocEscapeFooNonSymbolArg() {
444 struct StructWithInt *p = malloc(sizeof(struct StructWithInt));
445 nonSymbolAsFirstArg(&p->g, p);
446 return; // no warning
447}
448
Anna Zaks4fb54872012-02-11 21:02:35 +0000449void mallocFailedOrNotLeak() {
450 int *p = malloc(12);
451 if (p == 0)
452 return; // no warning
453 else
Anna Zaks3d7c44e2012-03-21 19:45:08 +0000454 return; // expected-warning {{Memory is never released; potential leak}}
Anna Zaks4fb54872012-02-11 21:02:35 +0000455}
Anna Zakse9ef5622012-02-10 01:11:00 +0000456
Anna Zaksebc1d322012-02-15 00:11:28 +0000457void mallocAssignment() {
458 char *p = malloc(12);
Jordan Rose63bc1862012-11-15 19:11:43 +0000459 p = fooRetPtr();
460} // expected-warning {{leak}}
Anna Zaksebc1d322012-02-15 00:11:28 +0000461
Anna Zaksb16ce452012-02-15 00:11:22 +0000462int vallocTest() {
463 char *mem = valloc(12);
Anna Zaks3d7c44e2012-03-21 19:45:08 +0000464 return 0; // expected-warning {{Memory is never released; potential leak}}
Anna Zaksb16ce452012-02-15 00:11:22 +0000465}
466
467void vallocEscapeFreeUse() {
468 int *p = valloc(12);
469 myfoo(p);
470 free(p);
Anna Zaksfebdc322012-02-16 22:26:12 +0000471 myfoo(p); // expected-warning{{Use of memory after it is freed}}
Anna Zaksb16ce452012-02-15 00:11:22 +0000472}
473
Anna Zakscdfec5e2012-02-09 06:25:47 +0000474int *Gl;
475struct GlStTy {
476 int *x;
477};
478
479struct GlStTy GlS = {0};
480
481void GlobalFree() {
482 free(Gl);
483}
484
485void GlobalMalloc() {
486 Gl = malloc(12);
487}
488
489void GlobalStructMalloc() {
490 int *a = malloc(12);
491 GlS.x = a;
492}
493
494void GlobalStructMallocFree() {
495 int *a = malloc(12);
496 GlS.x = a;
497 free(GlS.x);
498}
Anna Zaksf8b1c312012-02-10 01:11:03 +0000499
Anna Zaksad901a62012-02-16 22:26:15 +0000500char *ArrayG[12];
501
502void globalArrayTest() {
503 char *p = (char*)malloc(12);
504 ArrayG[0] = p;
505}
506
Anna Zaksac593002012-02-16 03:40:57 +0000507// Make sure that we properly handle a pointer stored into a local struct/array.
508typedef struct _StructWithPtr {
509 int *memP;
510} StructWithPtr;
511
512static StructWithPtr arrOfStructs[10];
513
514void testMalloc() {
515 int *x = malloc(12);
516 StructWithPtr St;
517 St.memP = x;
Jordan Rose0d53ab42012-08-08 18:23:31 +0000518 arrOfStructs[0] = St; // no-warning
Anna Zaksac593002012-02-16 03:40:57 +0000519}
520
521StructWithPtr testMalloc2() {
522 int *x = malloc(12);
523 StructWithPtr St;
524 St.memP = x;
Jordan Rose0d53ab42012-08-08 18:23:31 +0000525 return St; // no-warning
Anna Zaksac593002012-02-16 03:40:57 +0000526}
527
528int *testMalloc3() {
529 int *x = malloc(12);
530 int *y = x;
Jordan Rose0d53ab42012-08-08 18:23:31 +0000531 return y; // no-warning
Anna Zaksac593002012-02-16 03:40:57 +0000532}
533
Anna Zaksd8a8a3b2012-02-17 22:35:34 +0000534void testElemRegion1() {
535 char *x = (void*)malloc(2);
536 int *ix = (int*)x;
537 free(&(x[0]));
538}
539
540void testElemRegion2(int **pp) {
541 int *p = malloc(12);
542 *pp = p;
543 free(pp[0]);
544}
545
546void testElemRegion3(int **pp) {
547 int *p = malloc(12);
548 *pp = p;
549 free(*pp);
550}
Anna Zaks4fb54872012-02-11 21:02:35 +0000551// Region escape testing.
552
553unsigned takePtrToPtr(int **p);
554void PassTheAddrOfAllocatedData(int f) {
555 int *p = malloc(12);
556 // We don't know what happens after the call. Should stop tracking here.
557 if (takePtrToPtr(&p))
558 f++;
559 free(p); // no warning
560}
561
562struct X {
563 int *p;
564};
565unsigned takePtrToStruct(struct X *s);
566int ** foo2(int *g, int f) {
567 int *p = malloc(12);
568 struct X *px= malloc(sizeof(struct X));
569 px->p = p;
570 // We don't know what happens after this call. Should not track px nor p.
571 if (takePtrToStruct(px))
572 f++;
573 free(p);
574 return 0;
575}
576
577struct X* RegInvalidationDetect1(struct X *s2) {
578 struct X *px= malloc(sizeof(struct X));
579 px->p = 0;
580 px = s2;
Anna Zaks3d7c44e2012-03-21 19:45:08 +0000581 return px; // expected-warning {{Memory is never released; potential leak}}
Anna Zaks4fb54872012-02-11 21:02:35 +0000582}
583
584struct X* RegInvalidationGiveUp1() {
585 int *p = malloc(12);
586 struct X *px= malloc(sizeof(struct X));
587 px->p = p;
588 return px;
589}
590
591int **RegInvalidationDetect2(int **pp) {
592 int *p = malloc(12);
593 pp = &p;
594 pp++;
Anna Zaks3d7c44e2012-03-21 19:45:08 +0000595 return 0;// expected-warning {{Memory is never released; potential leak}}
Anna Zaks4fb54872012-02-11 21:02:35 +0000596}
Anna Zaksf8b1c312012-02-10 01:11:03 +0000597
Anna Zaksf8b1c312012-02-10 01:11:03 +0000598extern void exit(int) __attribute__ ((__noreturn__));
599void mallocExit(int *g) {
600 struct xx *p = malloc(12);
Anna Zaksda046772012-02-11 21:02:40 +0000601 if (g != 0)
602 exit(1);
Anna Zaksf8b1c312012-02-10 01:11:03 +0000603 free(p);
604 return;
605}
606
Anna Zaksf8b1c312012-02-10 01:11:03 +0000607extern void __assert_fail (__const char *__assertion, __const char *__file,
608 unsigned int __line, __const char *__function)
609 __attribute__ ((__noreturn__));
610#define assert(expr) \
611 ((expr) ? (void)(0) : __assert_fail (#expr, __FILE__, __LINE__, __func__))
612void mallocAssert(int *g) {
613 struct xx *p = malloc(12);
614
Anna Zaksda046772012-02-11 21:02:40 +0000615 assert(g != 0);
Anna Zaksf8b1c312012-02-10 01:11:03 +0000616 free(p);
617 return;
618}
619
Anna Zaks15d0ae12012-02-11 23:46:36 +0000620void doNotInvalidateWhenPassedToSystemCalls(char *s) {
621 char *p = malloc(12);
622 strlen(p);
Jordan Rose63bc1862012-11-15 19:11:43 +0000623 strcpy(p, s);
624} // expected-warning {{leak}}
Anna Zaks15d0ae12012-02-11 23:46:36 +0000625
Anna Zaksf0dfc9c2012-02-17 22:35:31 +0000626// Rely on the CString checker evaluation of the strcpy API to convey that the result of strcpy is equal to p.
627void symbolLostWithStrcpy(char *s) {
628 char *p = malloc(12);
629 p = strcpy(p, s);
630 free(p);
631}
632
633
634// The same test as the one above, but with what is actually generated on a mac.
635static __inline char *
636__inline_strcpy_chk (char *restrict __dest, const char *restrict __src)
637{
638 return __builtin___strcpy_chk (__dest, __src, __builtin_object_size (__dest, 2 > 1));
639}
640
641void symbolLostWithStrcpy_InlineStrcpyVersion(char *s) {
642 char *p = malloc(12);
643 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));
644 free(p);
645}
Anna Zaksd9ab7bb2012-02-22 02:36:01 +0000646
647// Here we are returning a pointer one past the allocated value. An idiom which
648// can be used for implementing special malloc. The correct uses of this might
649// be rare enough so that we could keep this as a warning.
650static void *specialMalloc(int n){
651 int *p;
652 p = malloc( n+8 );
653 if( p ){
654 p[0] = n;
655 p++;
656 }
657 return p;
658}
659
660// Potentially, the user could free the struct by performing pointer arithmetic on the return value.
661// This is a variation of the specialMalloc issue, though probably would be more rare in correct code.
662int *specialMallocWithStruct() {
663 struct StructWithInt *px= malloc(sizeof(struct StructWithInt));
664 return &(px->g);
665}
666
Anna Zaks60a1fa42012-02-22 03:14:20 +0000667// Test various allocation/deallocation functions.
Anna Zaks60a1fa42012-02-22 03:14:20 +0000668void testStrdup(const char *s, unsigned validIndex) {
669 char *s2 = strdup(s);
Jordan Rose63bc1862012-11-15 19:11:43 +0000670 s2[validIndex + 1] = 'b';
671} // expected-warning {{Memory is never released; potential leak}}
Anna Zaks60a1fa42012-02-22 03:14:20 +0000672
673int testStrndup(const char *s, unsigned validIndex, unsigned size) {
674 char *s2 = strndup(s, size);
675 s2 [validIndex + 1] = 'b';
676 if (s2[validIndex] != 'a')
Anna Zaksca8e36e2012-02-23 21:38:21 +0000677 return 0;
Anna Zaks60a1fa42012-02-22 03:14:20 +0000678 else
Anna Zaks3d7c44e2012-03-21 19:45:08 +0000679 return 1;// expected-warning {{Memory is never released; potential leak}}
Anna Zaks60a1fa42012-02-22 03:14:20 +0000680}
681
Anna Zaks87cb5be2012-02-22 19:24:52 +0000682void testStrdupContentIsDefined(const char *s, unsigned validIndex) {
683 char *s2 = strdup(s);
684 char result = s2[1];// no warning
685 free(s2);
686}
687
Anna Zaksca23eb22012-02-29 18:42:47 +0000688// ----------------------------------------------------------------------------
Anna Zaks0d389b82012-02-23 01:05:27 +0000689// Test the system library functions to which the pointer can escape.
Anna Zaksca23eb22012-02-29 18:42:47 +0000690// This tests false positive suppression.
Anna Zaks0d389b82012-02-23 01:05:27 +0000691
692// For now, we assume memory passed to pthread_specific escapes.
693// TODO: We could check that if a new pthread binding is set, the existing
694// binding must be freed; otherwise, a memory leak can occur.
695void testPthereadSpecificEscape(pthread_key_t key) {
696 void *buf = malloc(12);
697 pthread_setspecific(key, buf); // no warning
698}
699
Anna Zaksca23eb22012-02-29 18:42:47 +0000700// PR12101: Test funopen().
701static int releasePtr(void *_ctx) {
702 free(_ctx);
703 return 0;
704}
705FILE *useFunOpen() {
706 void *ctx = malloc(sizeof(int));
707 FILE *f = funopen(ctx, 0, 0, 0, releasePtr); // no warning
708 if (f == 0) {
709 free(ctx);
710 }
711 return f;
712}
713FILE *useFunOpenNoReleaseFunction() {
714 void *ctx = malloc(sizeof(int));
715 FILE *f = funopen(ctx, 0, 0, 0, 0);
716 if (f == 0) {
717 free(ctx);
718 }
719 return f; // expected-warning{{leak}}
720}
721
Jordan Rose85d7e012012-07-02 19:27:51 +0000722static int readNothing(void *_ctx, char *buf, int size) {
723 return 0;
724}
725FILE *useFunOpenReadNoRelease() {
726 void *ctx = malloc(sizeof(int));
727 FILE *f = funopen(ctx, readNothing, 0, 0, 0);
728 if (f == 0) {
729 free(ctx);
730 }
731 return f; // expected-warning{{leak}}
732}
733
Anna Zaksca23eb22012-02-29 18:42:47 +0000734// Test setbuf, setvbuf.
735int my_main_no_warning() {
736 char *p = malloc(100);
737 setvbuf(stdout, p, 0, 100);
738 return 0;
739}
740int my_main_no_warning2() {
741 char *p = malloc(100);
742 setbuf(__stdoutp, p);
743 return 0;
744}
745int my_main_warn(FILE *f) {
746 char *p = malloc(100);
747 setvbuf(f, p, 0, 100);
748 return 0;// expected-warning {{leak}}
749}
750
Ted Kremeneka99f8742012-03-05 23:06:19 +0000751// <rdar://problem/10978247>.
752// some people use stack allocated memory as an optimization to avoid
753// a heap allocation for small work sizes. This tests the analyzer's
754// understanding that the malloc'ed memory is not the same as stackBuffer.
755void radar10978247(int myValueSize) {
756 char stackBuffer[128];
757 char *buffer;
758
759 if (myValueSize <= sizeof(stackBuffer))
760 buffer = stackBuffer;
761 else
762 buffer = malloc(myValueSize);
763
764 // do stuff with the buffer
765 if (buffer != stackBuffer)
766 free(buffer);
767}
768
769void radar10978247_positive(int myValueSize) {
770 char stackBuffer[128];
771 char *buffer;
772
773 if (myValueSize <= sizeof(stackBuffer))
774 buffer = stackBuffer;
775 else
776 buffer = malloc(myValueSize);
777
778 // do stuff with the buffer
Jordan Rose63bc1862012-11-15 19:11:43 +0000779 if (buffer == stackBuffer)
Ted Kremeneka99f8742012-03-05 23:06:19 +0000780 return;
Jordan Rose63bc1862012-11-15 19:11:43 +0000781 else
782 return; // expected-warning {{leak}}
783}
Ted Kremenek8f40afb2012-04-26 05:08:26 +0000784// <rdar://problem/11269741> Previously this triggered a false positive
785// because malloc() is known to return uninitialized memory and the binding
786// of 'o' to 'p->n' was not getting propertly handled. Now we report a leak.
787struct rdar11269741_a_t {
788 struct rdar11269741_b_t {
789 int m;
790 } n;
791};
792
793int rdar11269741(struct rdar11269741_b_t o)
794{
795 struct rdar11269741_a_t *p = (struct rdar11269741_a_t *) malloc(sizeof(*p));
796 p->n = o;
797 return p->n.m; // expected-warning {{leak}}
798}
799
Anna Zakse55a14a2012-05-03 02:13:56 +0000800// Pointer arithmetic, returning an ElementRegion.
801void *radar11329382(unsigned bl) {
802 void *ptr = malloc (16);
803 ptr = ptr + (2 - bl);
804 return ptr; // no warning
805}
806
Anna Zaks33e4a1d2012-05-01 21:10:29 +0000807void __assert_rtn(const char *, const char *, int, const char *) __attribute__((__noreturn__));
808int strcmp(const char *, const char *);
809char *a (void);
810void radar11270219(void) {
811 char *x = a(), *y = a();
812 (__builtin_expect(!(x && y), 0) ? __assert_rtn(__func__, "/Users/zaks/tmp/ex.c", 24, "x && y") : (void)0);
813 strcmp(x, y); // no warning
814}
815
Anna Zaks93c5a242012-05-02 00:05:20 +0000816void radar_11358224_test_double_assign_ints_positive_2()
817{
818 void *ptr = malloc(16);
Jordan Rose63bc1862012-11-15 19:11:43 +0000819 ptr = ptr;
820} // expected-warning {{leak}}
Anna Zaks93c5a242012-05-02 00:05:20 +0000821
Anna Zaksaca0ac52012-05-03 23:50:28 +0000822// Assume that functions which take a function pointer can free memory even if
823// they are defined in system headers and take the const pointer to the
824// allocated memory. (radar://11160612)
825int const_ptr_and_callback(int, const char*, int n, void(*)(void*));
826void r11160612_1() {
827 char *x = malloc(12);
828 const_ptr_and_callback(0, x, 12, free); // no - warning
829}
830
831// Null is passed as callback.
832void r11160612_2() {
833 char *x = malloc(12);
Jordan Rose63bc1862012-11-15 19:11:43 +0000834 const_ptr_and_callback(0, x, 12, 0);
835} // expected-warning {{leak}}
Anna Zaksaca0ac52012-05-03 23:50:28 +0000836
837// Callback is passed to a function defined in a system header.
838void r11160612_4() {
839 char *x = malloc(12);
840 sqlite3_bind_text_my(0, x, 12, free); // no - warning
841}
842
Anna Zaksb79d8622012-05-03 23:50:33 +0000843// Passing callbacks in a struct.
844void r11160612_5(StWithCallback St) {
845 void *x = malloc(12);
846 dealocateMemWhenDoneByVal(x, St);
847}
848void r11160612_6(StWithCallback St) {
849 void *x = malloc(12);
850 dealocateMemWhenDoneByRef(&St, x);
851}
852
Anna Zaks84d43842012-05-04 17:37:16 +0000853int mySub(int, int);
854int myAdd(int, int);
855int fPtr(unsigned cond, int x) {
856 return (cond ? mySub : myAdd)(x, x);
857}
858
Anna Zakse17fdb22012-06-07 03:57:32 +0000859// Test anti-aliasing.
Anna Zaksda046772012-02-11 21:02:40 +0000860
Anna Zaksf8b1c312012-02-10 01:11:03 +0000861void dependsOnValueOfPtr(int *g, unsigned f) {
862 int *p;
863
864 if (f) {
865 p = g;
866 } else {
867 p = malloc(12);
868 }
869
870 if (p != g)
871 free(p);
872 else
Anna Zakse17fdb22012-06-07 03:57:32 +0000873 return; // no warning
Anna Zaksf8b1c312012-02-10 01:11:03 +0000874 return;
875}
876
Anna Zakse17fdb22012-06-07 03:57:32 +0000877int CMPRegionHeapToStack() {
878 int x = 0;
879 int *x1 = malloc(8);
880 int *x2 = &x;
Anna Zaksadccc3f2012-06-08 00:04:40 +0000881 clang_analyzer_eval(x1 == x2); // expected-warning{{FALSE}}
Anna Zakse17fdb22012-06-07 03:57:32 +0000882 free(x1);
883 return x;
884}
885
886int CMPRegionHeapToHeap2() {
887 int x = 0;
888 int *x1 = malloc(8);
889 int *x2 = malloc(8);
890 int *x4 = x1;
891 int *x5 = x2;
Anna Zaksadccc3f2012-06-08 00:04:40 +0000892 clang_analyzer_eval(x4 == x5); // expected-warning{{FALSE}}
Anna Zakse17fdb22012-06-07 03:57:32 +0000893 free(x1);
894 free(x2);
895 return x;
896}
897
898int CMPRegionHeapToHeap() {
899 int x = 0;
900 int *x1 = malloc(8);
901 int *x4 = x1;
902 if (x1 == x4) {
903 free(x1);
904 return 5/x; // expected-warning{{Division by zero}}
905 }
906 return x;// expected-warning{{This statement is never executed}}
907}
908
909int HeapAssignment() {
910 int m = 0;
911 int *x = malloc(4);
912 int *y = x;
913 *x = 5;
Anna Zaksadccc3f2012-06-08 00:04:40 +0000914 clang_analyzer_eval(*x != *y); // expected-warning{{FALSE}}
Anna Zakse17fdb22012-06-07 03:57:32 +0000915 free(x);
916 return 0;
917}
918
Anna Zaks783f0082012-06-07 20:18:08 +0000919int *retPtr();
920int *retPtrMightAlias(int *x);
921int cmpHeapAllocationToUnknown() {
922 int zero = 0;
923 int *yBefore = retPtr();
924 int *m = malloc(8);
925 int *yAfter = retPtrMightAlias(m);
Anna Zaksadccc3f2012-06-08 00:04:40 +0000926 clang_analyzer_eval(yBefore == m); // expected-warning{{FALSE}}
927 clang_analyzer_eval(yAfter == m); // expected-warning{{FALSE}}
Anna Zaks783f0082012-06-07 20:18:08 +0000928 free(m);
929 return 0;
930}
931
Jordan Rose6e99f9f2012-11-27 02:37:49 +0000932#ifdef __INTPTR_TYPE__
Ted Kremenek140d0c62012-05-01 21:58:29 +0000933// Test double assignment through integers.
Jordan Rose6e99f9f2012-11-27 02:37:49 +0000934typedef __INTPTR_TYPE__ intptr_t;
935typedef unsigned __INTPTR_TYPE__ uintptr_t;
936
937static intptr_t glob;
Ted Kremenek140d0c62012-05-01 21:58:29 +0000938void test_double_assign_ints()
939{
940 void *ptr = malloc (16); // no-warning
Jordan Rose6e99f9f2012-11-27 02:37:49 +0000941 glob = (intptr_t)(uintptr_t)ptr;
Ted Kremenek140d0c62012-05-01 21:58:29 +0000942}
943
944void test_double_assign_ints_positive()
945{
946 void *ptr = malloc(16);
Jordan Rose6e99f9f2012-11-27 02:37:49 +0000947 (void*)(intptr_t)(uintptr_t)ptr; // expected-warning {{unused}}
Jordan Rose63bc1862012-11-15 19:11:43 +0000948} // expected-warning {{leak}}
Jordan Rose6e99f9f2012-11-27 02:37:49 +0000949#endif
Jordan Rose1bf908d2012-06-16 00:09:20 +0000950
951void testCGContextNoLeak()
952{
953 void *ptr = malloc(16);
954 CGContextRef context = CGBitmapContextCreate(ptr);
955
956 // Because you can get the data back out like this, even much later,
957 // CGBitmapContextCreate is one of our "stop-tracking" exceptions.
958 free(CGBitmapContextGetData(context));
959}
960
961void testCGContextLeak()
962{
963 void *ptr = malloc(16);
964 CGContextRef context = CGBitmapContextCreate(ptr);
965 // However, this time we're just leaking the data, because the context
966 // object doesn't escape and it hasn't been freed in this function.
967}
968
Anna Zaks52a04812012-06-20 23:35:57 +0000969// Allow xpc context to escape. radar://11635258
970// TODO: Would be great if we checked that the finalize_connection_context actually releases it.
971static void finalize_connection_context(void *ctx) {
972 int *context = ctx;
973 free(context);
974}
975void foo (xpc_connection_t peer) {
976 int *ctx = calloc(1, sizeof(int));
977 xpc_connection_set_context(peer, ctx);
978 xpc_connection_set_finalizer_f(peer, finalize_connection_context);
979 xpc_connection_resume(peer);
980}
981
Anna Zaksede875b2012-08-03 18:30:18 +0000982// Make sure we catch errors when we free in a function which does not allocate memory.
983void freeButNoMalloc(int *p, int x){
984 if (x) {
985 free(p);
986 //user forgot a return here.
987 }
988 free(p); // expected-warning {{Attempt to free released memory}}
989}
Anna Zaks4d332862012-08-04 02:04:27 +0000990
991struct HasPtr {
Anna Zaks55dd9562012-08-24 02:28:20 +0000992 char *p;
Anna Zaks4d332862012-08-04 02:04:27 +0000993};
994
Anna Zaks55dd9562012-08-24 02:28:20 +0000995char* reallocButNoMalloc(struct HasPtr *a, int c, int size) {
Anna Zaks4d332862012-08-04 02:04:27 +0000996 int *s;
Anna Zaks55dd9562012-08-24 02:28:20 +0000997 char *b = realloc(a->p, size);
998 char *m = realloc(a->p, size); // expected-warning {{Attempt to free released memory}}
Anna Zaks4d332862012-08-04 02:04:27 +0000999 return a->p;
1000}
Jordan Rose0d53ab42012-08-08 18:23:31 +00001001
Anna Zaks55dd9562012-08-24 02:28:20 +00001002// We should not warn in this case since the caller will presumably free a->p in all cases.
1003int reallocButNoMallocPR13674(struct HasPtr *a, int c, int size) {
1004 int *s;
1005 char *b = realloc(a->p, size);
1006 if (b == 0)
1007 return -1;
1008 a->p = b;
1009 return 0;
1010}
1011
Anna Zaks9dc298b2012-09-12 22:57:34 +00001012// Test realloc with no visible malloc.
1013void *test(void *ptr) {
1014 void *newPtr = realloc(ptr, 4);
1015 if (newPtr == 0) {
1016 if (ptr)
1017 free(ptr); // no-warning
1018 }
1019 return newPtr;
1020}
1021
Jordan Rose84c48452012-11-15 19:11:27 +00001022
1023char *testLeakWithinReturn(char *str) {
1024 return strdup(strdup(str)); // expected-warning{{leak}}
1025}
1026
Anna Zaks233e26a2013-02-07 23:05:43 +00001027void passConstPtr(const char * ptr);
1028
1029void testPassConstPointer() {
1030 char * string = malloc(sizeof(char)*10);
1031 passConstPtr(string);
1032 return; // expected-warning {{leak}}
1033}
1034
1035void testPassConstPointerIndirectly() {
1036 char *p = malloc(1);
1037 p++;
1038 memcmp(p, p, sizeof(&p));
1039 return; // expected-warning {{leak}}
1040}
1041
1042void testPassToSystemHeaderFunctionIndirectly() {
1043 int *p = malloc(4);
1044 p++;
1045 fakeSystemHeaderCallInt(p);
1046} // expected-warning {{leak}}
1047
Jordan Rose0d53ab42012-08-08 18:23:31 +00001048// ----------------------------------------------------------------------------
1049// False negatives.
1050
1051// TODO: This is another false negative.
1052void testMallocWithParam(int **p) {
1053 *p = (int*) malloc(sizeof(int));
1054 *p = 0;
1055}
1056
1057void testMallocWithParam_2(int **p) {
1058 *p = (int*) malloc(sizeof(int));
1059}
Anna Zaksb98c6fe2013-02-06 00:01:14 +00001060
1061// Pending on removal of the escaping on assignment to struct fields.
1062void testStructLeak() {
1063 StructWithPtr St;
1064 St.memP = malloc(12);
1065 return; // missing warning
1066}
1067
1068void localArrayTest() {
1069 char *p = (char*)malloc(12);
1070 char *ArrayL[12];
1071 ArrayL[0] = p;
1072} // missing warning
1073
1074void localStructTest() {
1075 StructWithPtr St;
1076 StructWithPtr *pSt = &St;
1077 pSt->memP = malloc(12);
1078} // missing warning
1079
Anna Zaks233e26a2013-02-07 23:05:43 +00001080void testPassConstPointerIndirectlyStruct() {
1081 struct HasPtr hp;
1082 hp.p = malloc(10);
1083 memcmp(&hp, &hp, sizeof(hp));
1084 return; // missing leak
1085}
1086
1087void testPassToSystemHeaderFunctionIndirectlyStruct() {
1088 SomeStruct ss;
1089 ss.p = malloc(1);
1090 fakeSystemHeaderCall(&ss);
1091} // missing leak
Anna Zaksb98c6fe2013-02-06 00:01:14 +00001092
Anna Zaks118aa752013-02-07 23:05:47 +00001093int *testOffsetAllocate(size_t size) {
1094 int *memoryBlock = (int *)malloc(size + sizeof(int));
1095 return &memoryBlock[1]; // no-warning
1096}
1097
1098void testOffsetDeallocate(int *memoryBlock) {
1099 free(&memoryBlock[-1]); // no-warning
1100}
1101
1102void testOffsetOfRegionFreed() {
1103 __int64_t * array = malloc(sizeof(__int64_t)*2);
1104 array += 1;
1105 free(&array[0]); // expected-warning{{Argument to free() is offset by 8 bytes from the start of memory allocated by malloc()}}
1106}
1107
1108void testOffsetOfRegionFreed2() {
1109 __int64_t *p = malloc(sizeof(__int64_t)*2);
1110 p += 1;
1111 free(p); // expected-warning{{Argument to free() is offset by 8 bytes from the start of memory allocated by malloc()}}
1112}
1113
1114void testOffsetOfRegionFreed3() {
1115 char *r = malloc(sizeof(char));
1116 r = r - 10;
1117 free(r); // expected-warning {{Argument to free() is offset by -10 bytes from the start of memory allocated by malloc()}}
1118}
1119
1120void testOffsetOfRegionFreedAfterFunctionCall() {
1121 int *p = malloc(sizeof(int)*2);
1122 p += 1;
1123 myfoo(p);
1124 free(p); // no-warning
1125}
1126
1127void testFixManipulatedPointerBeforeFree() {
1128 int * array = malloc(sizeof(int)*2);
1129 array += 1;
1130 free(&array[-1]); // no-warning
1131}
1132
1133void testFixManipulatedPointerBeforeFree2() {
1134 char *r = malloc(sizeof(char));
1135 r = r + 10;
1136 free(r-10); // no-warning
1137}
1138
1139void freeOffsetPointerPassedToFunction() {
1140 __int64_t *p = malloc(sizeof(__int64_t)*2);
1141 p[1] = 0;
1142 p += 1;
1143 myfooint(*p); // not passing the pointer, only a value pointed by pointer
1144 free(p); // expected-warning {{Argument to free() is offset by 8 bytes from the start of memory allocated by malloc()}}
1145}
1146
1147int arbitraryInt();
1148void freeUnknownOffsetPointer() {
1149 char *r = malloc(sizeof(char));
1150 r = r + arbitraryInt(); // unable to reason about what the offset might be
1151 free(r); // no-warning
1152}
1153
1154void testFreeNonMallocPointerWithNoOffset() {
1155 char c;
1156 char *r = &c;
1157 r = r + 10;
1158 free(r-10); // expected-warning {{Argument to free() is the address of the local variable 'c', which is not memory allocated by malloc()}}
1159}
1160
1161void testFreeNonMallocPointerWithOffset() {
1162 char c;
1163 char *r = &c;
1164 free(r+1); // expected-warning {{Argument to free() is the address of the local variable 'c', which is not memory allocated by malloc()}}
1165}
1166
1167void testOffsetZeroDoubleFree() {
1168 int *array = malloc(sizeof(int)*2);
1169 int *p = &array[0];
1170 free(p);
1171 free(&array[0]); // expected-warning{{Attempt to free released memory}}
1172}
1173
1174void testOffsetPassedToStrlen() {
1175 char * string = malloc(sizeof(char)*10);
1176 string += 1;
1177 int length = strlen(string); // expected-warning {{Memory is never released; potential leak of memory pointed to by 'string'}}
1178}
1179
1180void testOffsetPassedToStrlenThenFree() {
1181 char * string = malloc(sizeof(char)*10);
1182 string += 1;
1183 int length = strlen(string);
1184 free(string); // expected-warning {{Argument to free() is offset by 1 byte from the start of memory allocated by malloc()}}
1185}
1186
1187void testOffsetPassedAsConst() {
1188 char * string = malloc(sizeof(char)*10);
1189 string += 1;
1190 passConstPtr(string);
1191 free(string); // expected-warning {{Argument to free() is offset by 1 byte from the start of memory allocated by malloc()}}
1192}
Anna Zaksb98c6fe2013-02-06 00:01:14 +00001193