Ted Kremenek | 033a07e | 2011-08-03 23:14:55 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=core,experimental.deadcode.UnreachableCode,experimental.core.CastSize,experimental.unix.Malloc -analyzer-store=region -verify %s |
Anna Zaks | 15d0ae1 | 2012-02-11 23:46:36 +0000 | [diff] [blame] | 2 | #include "system-header-simulator.h" |
| 3 | |
Eli Friedman | 2f00552 | 2009-11-14 04:23:25 +0000 | [diff] [blame] | 4 | typedef __typeof(sizeof(int)) size_t; |
Ted Kremenek | c360775 | 2009-11-13 20:03:22 +0000 | [diff] [blame] | 5 | void *malloc(size_t); |
Anna Zaks | b16ce45 | 2012-02-15 00:11:22 +0000 | [diff] [blame] | 6 | void *valloc(size_t); |
Ted Kremenek | c360775 | 2009-11-13 20:03:22 +0000 | [diff] [blame] | 7 | void free(void *); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 8 | void *realloc(void *ptr, size_t size); |
Anna Zaks | 40add29 | 2012-02-15 00:11:25 +0000 | [diff] [blame^] | 9 | void *reallocf(void *ptr, size_t size); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 10 | void *calloc(size_t nmemb, size_t size); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 11 | |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 12 | void myfoo(int *p); |
| 13 | void myfooint(int p); |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 14 | |
| 15 | void f1() { |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 16 | int *p = malloc(12); |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 17 | return; // expected-warning{{Allocated memory never released. Potential memory leak.}} |
| 18 | } |
| 19 | |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 20 | void f2() { |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 21 | int *p = malloc(12); |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 22 | free(p); |
| 23 | free(p); // expected-warning{{Try to free a memory block that has been released}} |
| 24 | } |
Ted Kremenek | c764d4b | 2009-11-13 20:00:28 +0000 | [diff] [blame] | 25 | |
Lenny Maiorani | 4d8d803 | 2011-04-27 14:49:29 +0000 | [diff] [blame] | 26 | void f2_realloc_0() { |
| 27 | int *p = malloc(12); |
| 28 | realloc(p,0); |
| 29 | realloc(p,0); // expected-warning{{Try to free a memory block that has been released}} |
| 30 | } |
| 31 | |
| 32 | void f2_realloc_1() { |
| 33 | int *p = malloc(12); |
Zhongxing Xu | d56763f | 2011-09-01 04:53:59 +0000 | [diff] [blame] | 34 | int *q = realloc(p,0); // no-warning |
Lenny Maiorani | 4d8d803 | 2011-04-27 14:49:29 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 37 | void reallocNotNullPtr(unsigned sizeIn) { |
| 38 | unsigned size = 12; |
| 39 | char *p = (char*)malloc(size); |
| 40 | if (p) { |
| 41 | char *q = (char*)realloc(p, sizeIn); |
| 42 | char x = *q; // expected-warning {{Allocated memory never released.}} |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | int *realloctest1() { |
| 47 | int *q = malloc(12); |
| 48 | q = realloc(q, 20); |
| 49 | return q; // no warning - returning the allocated value |
| 50 | } |
| 51 | |
| 52 | // p should be freed if realloc fails. |
| 53 | void reallocFails() { |
| 54 | char *p = malloc(12); |
| 55 | char *r = realloc(p, 12+1); |
| 56 | if (!r) { |
| 57 | free(p); |
| 58 | } else { |
| 59 | free(r); |
| 60 | } |
| 61 | } |
| 62 | |
Anna Zaks | 30838b9 | 2012-02-13 20:57:07 +0000 | [diff] [blame] | 63 | void reallocSizeZero1() { |
| 64 | char *p = malloc(12); |
| 65 | char *r = realloc(p, 0); |
| 66 | if (!r) { |
| 67 | free(p); |
| 68 | } else { |
| 69 | free(r); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void reallocSizeZero2() { |
| 74 | char *p = malloc(12); |
| 75 | char *r = realloc(p, 0); |
| 76 | if (!r) { |
| 77 | free(p); |
| 78 | } else { |
| 79 | free(r); |
| 80 | } |
| 81 | free(p); // expected-warning {{Try to free a memory block that has been released}} |
| 82 | } |
| 83 | |
| 84 | void reallocSizeZero3() { |
| 85 | char *p = malloc(12); |
| 86 | char *r = realloc(p, 0); |
| 87 | free(r); |
| 88 | } |
| 89 | |
| 90 | void reallocSizeZero4() { |
| 91 | char *r = realloc(0, 0); |
| 92 | free(r); |
| 93 | } |
| 94 | |
| 95 | void reallocSizeZero5() { |
| 96 | char *r = realloc(0, 0); |
| 97 | } |
| 98 | |
| 99 | void reallocPtrZero1() { |
| 100 | char *r = realloc(0, 12); // expected-warning {{Allocated memory never released.}} |
| 101 | } |
| 102 | |
| 103 | void reallocPtrZero2() { |
| 104 | char *r = realloc(0, 12); |
| 105 | if (r) |
| 106 | free(r); |
| 107 | } |
| 108 | |
| 109 | void reallocPtrZero3() { |
| 110 | char *r = realloc(0, 12); |
| 111 | free(r); |
| 112 | } |
| 113 | |
Anna Zaks | b276bd9 | 2012-02-14 00:26:13 +0000 | [diff] [blame] | 114 | void reallocRadar6337483_1() { |
| 115 | char *buf = malloc(100); |
| 116 | buf = (char*)realloc(buf, 0x1000000); |
| 117 | if (!buf) { |
| 118 | return;// expected-warning {{Allocated memory never released.}} |
| 119 | } |
| 120 | free(buf); |
| 121 | } |
| 122 | |
| 123 | void reallocRadar6337483_2() { |
| 124 | char *buf = malloc(100); |
| 125 | char *buf2 = (char*)realloc(buf, 0x1000000); |
| 126 | if (!buf2) { // expected-warning {{Allocated memory never released.}} |
| 127 | ; |
| 128 | } else { |
| 129 | free(buf2); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | void reallocRadar6337483_3() { |
| 134 | char * buf = malloc(100); |
| 135 | char * tmp; |
| 136 | tmp = (char*)realloc(buf, 0x1000000); |
| 137 | if (!tmp) { |
| 138 | free(buf); |
| 139 | return; |
| 140 | } |
| 141 | buf = tmp; |
| 142 | free(buf); |
| 143 | } |
| 144 | |
| 145 | void reallocRadar6337483_4() { |
| 146 | char *buf = malloc(100); |
| 147 | char *buf2 = (char*)realloc(buf, 0x1000000); |
| 148 | if (!buf2) { |
| 149 | return; // expected-warning {{Allocated memory never released.}} |
| 150 | } else { |
| 151 | free(buf2); |
| 152 | } |
| 153 | } |
| 154 | |
Anna Zaks | 40add29 | 2012-02-15 00:11:25 +0000 | [diff] [blame^] | 155 | int *reallocfTest1() { |
| 156 | int *q = malloc(12); |
| 157 | q = reallocf(q, 20); |
| 158 | return q; // no warning - returning the allocated value |
| 159 | } |
| 160 | |
| 161 | void reallocfRadar6337483_4() { |
| 162 | char *buf = malloc(100); |
| 163 | char *buf2 = (char*)reallocf(buf, 0x1000000); |
| 164 | if (!buf2) { |
| 165 | return; // no warning - reallocf frees even on failure |
| 166 | } else { |
| 167 | free(buf2); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | void reallocfRadar6337483_3() { |
| 172 | char * buf = malloc(100); |
| 173 | char * tmp; |
| 174 | tmp = (char*)reallocf(buf, 0x1000000); |
| 175 | if (!tmp) { |
| 176 | free(buf); // expected-warning {{Try to free a memory block that has been released}} |
| 177 | return; |
| 178 | } |
| 179 | buf = tmp; |
| 180 | free(buf); |
| 181 | } |
| 182 | |
| 183 | void reallocfPtrZero1() { |
| 184 | char *r = reallocf(0, 12); // expected-warning {{Allocated memory never released.}} |
| 185 | } |
| 186 | |
| 187 | |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 188 | // This case tests that storing malloc'ed memory to a static variable which is |
| 189 | // then returned is not leaked. In the absence of known contracts for functions |
| 190 | // or inter-procedural analysis, this is a conservative answer. |
Ted Kremenek | c764d4b | 2009-11-13 20:00:28 +0000 | [diff] [blame] | 191 | int *f3() { |
| 192 | static int *p = 0; |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 193 | p = malloc(12); |
Zhongxing Xu | 4985e3e | 2009-11-17 08:58:18 +0000 | [diff] [blame] | 194 | return p; // no-warning |
Ted Kremenek | c764d4b | 2009-11-13 20:00:28 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 197 | // This case tests that storing malloc'ed memory to a static global variable |
| 198 | // which is then returned is not leaked. In the absence of known contracts for |
| 199 | // functions or inter-procedural analysis, this is a conservative answer. |
Ted Kremenek | c764d4b | 2009-11-13 20:00:28 +0000 | [diff] [blame] | 200 | static int *p_f4 = 0; |
| 201 | int *f4() { |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 202 | p_f4 = malloc(12); |
Zhongxing Xu | 4985e3e | 2009-11-17 08:58:18 +0000 | [diff] [blame] | 203 | return p_f4; // no-warning |
Ted Kremenek | c764d4b | 2009-11-13 20:00:28 +0000 | [diff] [blame] | 204 | } |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 205 | |
| 206 | int *f5() { |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 207 | int *q = malloc(12); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 208 | q = realloc(q, 20); |
| 209 | return q; // no-warning |
| 210 | } |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 211 | |
| 212 | void f6() { |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 213 | int *p = malloc(12); |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 214 | if (!p) |
| 215 | return; // no-warning |
| 216 | else |
| 217 | free(p); |
| 218 | } |
Zhongxing Xu | 425c7ed | 2010-01-18 04:01:40 +0000 | [diff] [blame] | 219 | |
Lenny Maiorani | 4d8d803 | 2011-04-27 14:49:29 +0000 | [diff] [blame] | 220 | void f6_realloc() { |
| 221 | int *p = malloc(12); |
| 222 | if (!p) |
| 223 | return; // no-warning |
| 224 | else |
| 225 | realloc(p,0); |
| 226 | } |
| 227 | |
| 228 | |
Zhongxing Xu | 425c7ed | 2010-01-18 04:01:40 +0000 | [diff] [blame] | 229 | char *doit2(); |
| 230 | void pr6069() { |
| 231 | char *buf = doit2(); |
| 232 | free(buf); |
| 233 | } |
Zhongxing Xu | 181cc3d | 2010-02-14 06:49:48 +0000 | [diff] [blame] | 234 | |
| 235 | void pr6293() { |
| 236 | free(0); |
| 237 | } |
Zhongxing Xu | c802378 | 2010-03-10 04:58:55 +0000 | [diff] [blame] | 238 | |
| 239 | void f7() { |
| 240 | char *x = (char*) malloc(4); |
| 241 | free(x); |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 242 | x[0] = 'a'; // expected-warning{{Use of dynamically allocated memory after it is freed.}} |
Zhongxing Xu | c802378 | 2010-03-10 04:58:55 +0000 | [diff] [blame] | 243 | } |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 244 | |
Lenny Maiorani | 4d8d803 | 2011-04-27 14:49:29 +0000 | [diff] [blame] | 245 | void f7_realloc() { |
| 246 | char *x = (char*) malloc(4); |
| 247 | realloc(x,0); |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 248 | x[0] = 'a'; // expected-warning{{Use of dynamically allocated memory after it is freed.}} |
Lenny Maiorani | 4d8d803 | 2011-04-27 14:49:29 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 251 | void PR6123() { |
| 252 | int *x = malloc(11); // expected-warning{{Cast a region whose size is not a multiple of the destination type size.}} |
| 253 | } |
| 254 | |
| 255 | void PR7217() { |
| 256 | int *buf = malloc(2); // expected-warning{{Cast a region whose size is not a multiple of the destination type size.}} |
| 257 | buf[1] = 'c'; // not crash |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 258 | } |
Jordy Rose | c580f2e | 2010-06-20 04:30:57 +0000 | [diff] [blame] | 259 | |
| 260 | void mallocCastToVoid() { |
| 261 | void *p = malloc(2); |
| 262 | const void *cp = p; // not crash |
| 263 | free(p); |
| 264 | } |
| 265 | |
| 266 | void mallocCastToFP() { |
| 267 | void *p = malloc(2); |
| 268 | void (*fp)() = p; // not crash |
| 269 | free(p); |
| 270 | } |
| 271 | |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 272 | // This tests that malloc() buffers are undefined by default |
| 273 | char mallocGarbage () { |
| 274 | char *buf = malloc(2); |
| 275 | char result = buf[1]; // expected-warning{{undefined}} |
| 276 | free(buf); |
| 277 | return result; |
| 278 | } |
| 279 | |
| 280 | // This tests that calloc() buffers need to be freed |
| 281 | void callocNoFree () { |
| 282 | char *buf = calloc(2,2); |
| 283 | return; // expected-warning{{never released}} |
| 284 | } |
| 285 | |
| 286 | // These test that calloc() buffers are zeroed by default |
| 287 | char callocZeroesGood () { |
| 288 | char *buf = calloc(2,2); |
| 289 | char result = buf[3]; // no-warning |
| 290 | if (buf[1] == 0) { |
| 291 | free(buf); |
| 292 | } |
| 293 | return result; // no-warning |
| 294 | } |
| 295 | |
| 296 | char callocZeroesBad () { |
| 297 | char *buf = calloc(2,2); |
| 298 | char result = buf[3]; // no-warning |
| 299 | if (buf[1] != 0) { |
Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 300 | free(buf); // expected-warning{{never executed}} |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 301 | } |
| 302 | return result; // expected-warning{{never released}} |
| 303 | } |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 304 | |
| 305 | void nullFree() { |
| 306 | int *p = 0; |
| 307 | free(p); // no warning - a nop |
| 308 | } |
| 309 | |
| 310 | void paramFree(int *p) { |
| 311 | myfoo(p); |
| 312 | free(p); // no warning |
| 313 | myfoo(p); // TODO: This should be a warning. |
| 314 | } |
| 315 | |
| 316 | int* mallocEscapeRet() { |
| 317 | int *p = malloc(12); |
| 318 | return p; // no warning |
| 319 | } |
| 320 | |
| 321 | void mallocEscapeFoo() { |
| 322 | int *p = malloc(12); |
| 323 | myfoo(p); |
| 324 | return; // no warning |
| 325 | } |
| 326 | |
| 327 | void mallocEscapeFree() { |
| 328 | int *p = malloc(12); |
| 329 | myfoo(p); |
| 330 | free(p); |
| 331 | } |
| 332 | |
| 333 | void mallocEscapeFreeFree() { |
| 334 | int *p = malloc(12); |
| 335 | myfoo(p); |
| 336 | free(p); |
| 337 | free(p); // expected-warning{{Try to free a memory block that has been released}} |
| 338 | } |
| 339 | |
| 340 | void mallocEscapeFreeUse() { |
| 341 | int *p = malloc(12); |
| 342 | myfoo(p); |
| 343 | free(p); |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 344 | myfoo(p); // expected-warning{{Use of dynamically allocated memory after it is freed.}} |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | int *myalloc(); |
| 348 | void myalloc2(int **p); |
| 349 | |
| 350 | void mallocEscapeFreeCustomAlloc() { |
| 351 | int *p = malloc(12); |
| 352 | myfoo(p); |
| 353 | free(p); |
| 354 | p = myalloc(); |
| 355 | free(p); // no warning |
| 356 | } |
| 357 | |
| 358 | void mallocEscapeFreeCustomAlloc2() { |
| 359 | int *p = malloc(12); |
| 360 | myfoo(p); |
| 361 | free(p); |
| 362 | myalloc2(&p); |
| 363 | free(p); // no warning |
| 364 | } |
| 365 | |
| 366 | void mallocBindFreeUse() { |
| 367 | int *x = malloc(12); |
| 368 | int *y = x; |
| 369 | free(y); |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 370 | myfoo(x); // expected-warning{{Use of dynamically allocated memory after it is freed.}} |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | void mallocEscapeMalloc() { |
| 374 | int *p = malloc(12); |
| 375 | myfoo(p); |
| 376 | p = malloc(12); // expected-warning{{Allocated memory never released. Potential memory leak.}} |
| 377 | } |
| 378 | |
| 379 | void mallocMalloc() { |
| 380 | int *p = malloc(12); |
| 381 | p = malloc(12); // expected-warning{{Allocated memory never released. Potential memory leak}} |
| 382 | } |
| 383 | |
| 384 | void mallocFreeMalloc() { |
| 385 | int *p = malloc(12); |
| 386 | free(p); |
| 387 | p = malloc(12); |
| 388 | free(p); |
| 389 | } |
| 390 | |
Anna Zaks | cdfec5e | 2012-02-09 06:25:47 +0000 | [diff] [blame] | 391 | void mallocFreeUse_params() { |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 392 | int *p = malloc(12); |
| 393 | free(p); |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 394 | myfoo(p); //expected-warning{{Use of dynamically allocated memory after it is freed}} |
Anna Zaks | 15d0ae1 | 2012-02-11 23:46:36 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | void mallocFreeUse_params2() { |
| 398 | int *p = malloc(12); |
| 399 | free(p); |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 400 | myfooint(*p); //expected-warning{{Use of dynamically allocated memory after it is freed}} |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 403 | void mallocFailedOrNot() { |
| 404 | int *p = malloc(12); |
| 405 | if (!p) |
| 406 | free(p); |
| 407 | else |
| 408 | free(p); |
| 409 | } |
| 410 | |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 411 | struct StructWithInt { |
| 412 | int g; |
| 413 | }; |
Anna Zaks | 0860cd0 | 2012-02-11 21:44:39 +0000 | [diff] [blame] | 414 | |
| 415 | int *mallocReturnFreed() { |
| 416 | int *p = malloc(12); |
| 417 | free(p); |
| 418 | return p; // expected-warning {{Use of dynamically allocated}} |
| 419 | } |
| 420 | |
| 421 | int useAfterFreeStruct() { |
| 422 | struct StructWithInt *px= malloc(sizeof(struct StructWithInt)); |
| 423 | px->g = 5; |
| 424 | free(px); |
| 425 | return px->g; // expected-warning {{Use of dynamically allocated}} |
| 426 | } |
| 427 | |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 428 | void nonSymbolAsFirstArg(int *pp, struct StructWithInt *p); |
| 429 | |
| 430 | void mallocEscapeFooNonSymbolArg() { |
| 431 | struct StructWithInt *p = malloc(sizeof(struct StructWithInt)); |
| 432 | nonSymbolAsFirstArg(&p->g, p); |
| 433 | return; // no warning |
| 434 | } |
| 435 | |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 436 | void mallocFailedOrNotLeak() { |
| 437 | int *p = malloc(12); |
| 438 | if (p == 0) |
| 439 | return; // no warning |
| 440 | else |
| 441 | return; // expected-warning {{Allocated memory never released. Potential memory leak.}} |
| 442 | } |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 443 | |
Anna Zaks | b16ce45 | 2012-02-15 00:11:22 +0000 | [diff] [blame] | 444 | int vallocTest() { |
| 445 | char *mem = valloc(12); |
| 446 | return 0; // expected-warning {{Allocated memory never released. Potential memory leak.}} |
| 447 | } |
| 448 | |
| 449 | void vallocEscapeFreeUse() { |
| 450 | int *p = valloc(12); |
| 451 | myfoo(p); |
| 452 | free(p); |
| 453 | myfoo(p); // expected-warning{{Use of dynamically allocated memory after it is freed.}} |
| 454 | } |
| 455 | |
Anna Zaks | cdfec5e | 2012-02-09 06:25:47 +0000 | [diff] [blame] | 456 | int *Gl; |
| 457 | struct GlStTy { |
| 458 | int *x; |
| 459 | }; |
| 460 | |
| 461 | struct GlStTy GlS = {0}; |
| 462 | |
| 463 | void GlobalFree() { |
| 464 | free(Gl); |
| 465 | } |
| 466 | |
| 467 | void GlobalMalloc() { |
| 468 | Gl = malloc(12); |
| 469 | } |
| 470 | |
| 471 | void GlobalStructMalloc() { |
| 472 | int *a = malloc(12); |
| 473 | GlS.x = a; |
| 474 | } |
| 475 | |
| 476 | void GlobalStructMallocFree() { |
| 477 | int *a = malloc(12); |
| 478 | GlS.x = a; |
| 479 | free(GlS.x); |
| 480 | } |
Anna Zaks | f8b1c31 | 2012-02-10 01:11:03 +0000 | [diff] [blame] | 481 | |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 482 | // Region escape testing. |
| 483 | |
| 484 | unsigned takePtrToPtr(int **p); |
| 485 | void PassTheAddrOfAllocatedData(int f) { |
| 486 | int *p = malloc(12); |
| 487 | // We don't know what happens after the call. Should stop tracking here. |
| 488 | if (takePtrToPtr(&p)) |
| 489 | f++; |
| 490 | free(p); // no warning |
| 491 | } |
| 492 | |
| 493 | struct X { |
| 494 | int *p; |
| 495 | }; |
| 496 | unsigned takePtrToStruct(struct X *s); |
| 497 | int ** foo2(int *g, int f) { |
| 498 | int *p = malloc(12); |
| 499 | struct X *px= malloc(sizeof(struct X)); |
| 500 | px->p = p; |
| 501 | // We don't know what happens after this call. Should not track px nor p. |
| 502 | if (takePtrToStruct(px)) |
| 503 | f++; |
| 504 | free(p); |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | struct X* RegInvalidationDetect1(struct X *s2) { |
| 509 | struct X *px= malloc(sizeof(struct X)); |
| 510 | px->p = 0; |
| 511 | px = s2; |
| 512 | return px; // expected-warning {{Allocated memory never released. Potential memory leak.}} |
| 513 | } |
| 514 | |
| 515 | struct X* RegInvalidationGiveUp1() { |
| 516 | int *p = malloc(12); |
| 517 | struct X *px= malloc(sizeof(struct X)); |
| 518 | px->p = p; |
| 519 | return px; |
| 520 | } |
| 521 | |
| 522 | int **RegInvalidationDetect2(int **pp) { |
| 523 | int *p = malloc(12); |
| 524 | pp = &p; |
| 525 | pp++; |
| 526 | return 0;// expected-warning {{Allocated memory never released. Potential memory leak.}} |
| 527 | } |
Anna Zaks | f8b1c31 | 2012-02-10 01:11:03 +0000 | [diff] [blame] | 528 | |
Anna Zaks | f8b1c31 | 2012-02-10 01:11:03 +0000 | [diff] [blame] | 529 | extern void exit(int) __attribute__ ((__noreturn__)); |
| 530 | void mallocExit(int *g) { |
| 531 | struct xx *p = malloc(12); |
Anna Zaks | da04677 | 2012-02-11 21:02:40 +0000 | [diff] [blame] | 532 | if (g != 0) |
| 533 | exit(1); |
Anna Zaks | f8b1c31 | 2012-02-10 01:11:03 +0000 | [diff] [blame] | 534 | free(p); |
| 535 | return; |
| 536 | } |
| 537 | |
Anna Zaks | f8b1c31 | 2012-02-10 01:11:03 +0000 | [diff] [blame] | 538 | extern void __assert_fail (__const char *__assertion, __const char *__file, |
| 539 | unsigned int __line, __const char *__function) |
| 540 | __attribute__ ((__noreturn__)); |
| 541 | #define assert(expr) \ |
| 542 | ((expr) ? (void)(0) : __assert_fail (#expr, __FILE__, __LINE__, __func__)) |
| 543 | void mallocAssert(int *g) { |
| 544 | struct xx *p = malloc(12); |
| 545 | |
Anna Zaks | da04677 | 2012-02-11 21:02:40 +0000 | [diff] [blame] | 546 | assert(g != 0); |
Anna Zaks | f8b1c31 | 2012-02-10 01:11:03 +0000 | [diff] [blame] | 547 | free(p); |
| 548 | return; |
| 549 | } |
| 550 | |
Anna Zaks | 15d0ae1 | 2012-02-11 23:46:36 +0000 | [diff] [blame] | 551 | void doNotInvalidateWhenPassedToSystemCalls(char *s) { |
| 552 | char *p = malloc(12); |
| 553 | strlen(p); |
| 554 | strcpy(p, s); // expected-warning {{leak}} |
| 555 | } |
| 556 | |
Anna Zaks | da04677 | 2012-02-11 21:02:40 +0000 | [diff] [blame] | 557 | // Below are the known false positives. |
| 558 | |
Anna Zaks | f8b1c31 | 2012-02-10 01:11:03 +0000 | [diff] [blame] | 559 | // TODO: There should be no warning here. This one might be difficult to get rid of. |
| 560 | void dependsOnValueOfPtr(int *g, unsigned f) { |
| 561 | int *p; |
| 562 | |
| 563 | if (f) { |
| 564 | p = g; |
| 565 | } else { |
| 566 | p = malloc(12); |
| 567 | } |
| 568 | |
| 569 | if (p != g) |
| 570 | free(p); |
| 571 | else |
| 572 | return; // expected-warning{{Allocated memory never released. Potential memory leak}} |
| 573 | return; |
| 574 | } |
| 575 | |
| 576 | // TODO: Should this be a warning? |
| 577 | // Here we are returning a pointer one past the allocated value. An idiom which |
| 578 | // can be used for implementing special malloc. The correct uses of this might |
| 579 | // be rare enough so that we could keep this as a warning. |
| 580 | static void *specialMalloc(int n){ |
| 581 | int *p; |
| 582 | p = malloc( n+8 ); |
| 583 | if( p ){ |
| 584 | p[0] = n; |
| 585 | p++; |
| 586 | } |
| 587 | return p;// expected-warning {{Allocated memory never released. Potential memory leak.}} |
| 588 | } |