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 |
Eli Friedman | 2f00552 | 2009-11-14 04:23:25 +0000 | [diff] [blame] | 2 | typedef __typeof(sizeof(int)) size_t; |
Ted Kremenek | c360775 | 2009-11-13 20:03:22 +0000 | [diff] [blame] | 3 | void *malloc(size_t); |
| 4 | void free(void *); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 5 | void *realloc(void *ptr, size_t size); |
| 6 | void *calloc(size_t nmemb, size_t size); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 7 | |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 8 | void myfoo(int *p); |
| 9 | void myfooint(int p); |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 10 | |
| 11 | void f1() { |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 12 | int *p = malloc(12); |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 13 | return; // expected-warning{{Allocated memory never released. Potential memory leak.}} |
| 14 | } |
| 15 | |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 16 | void f2() { |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 17 | int *p = malloc(12); |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 18 | free(p); |
| 19 | free(p); // expected-warning{{Try to free a memory block that has been released}} |
| 20 | } |
Ted Kremenek | c764d4b | 2009-11-13 20:00:28 +0000 | [diff] [blame] | 21 | |
Lenny Maiorani | 4d8d803 | 2011-04-27 14:49:29 +0000 | [diff] [blame] | 22 | void f2_realloc_0() { |
| 23 | int *p = malloc(12); |
| 24 | realloc(p,0); |
| 25 | realloc(p,0); // expected-warning{{Try to free a memory block that has been released}} |
| 26 | } |
| 27 | |
| 28 | void f2_realloc_1() { |
| 29 | int *p = malloc(12); |
Zhongxing Xu | d56763f | 2011-09-01 04:53:59 +0000 | [diff] [blame] | 30 | int *q = realloc(p,0); // no-warning |
Lenny Maiorani | 4d8d803 | 2011-04-27 14:49:29 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 33 | // This case tests that storing malloc'ed memory to a static variable which is |
| 34 | // then returned is not leaked. In the absence of known contracts for functions |
| 35 | // or inter-procedural analysis, this is a conservative answer. |
Ted Kremenek | c764d4b | 2009-11-13 20:00:28 +0000 | [diff] [blame] | 36 | int *f3() { |
| 37 | static int *p = 0; |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 38 | p = malloc(12); |
Zhongxing Xu | 4985e3e | 2009-11-17 08:58:18 +0000 | [diff] [blame] | 39 | return p; // no-warning |
Ted Kremenek | c764d4b | 2009-11-13 20:00:28 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 42 | // This case tests that storing malloc'ed memory to a static global variable |
| 43 | // which is then returned is not leaked. In the absence of known contracts for |
| 44 | // functions or inter-procedural analysis, this is a conservative answer. |
Ted Kremenek | c764d4b | 2009-11-13 20:00:28 +0000 | [diff] [blame] | 45 | static int *p_f4 = 0; |
| 46 | int *f4() { |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 47 | p_f4 = malloc(12); |
Zhongxing Xu | 4985e3e | 2009-11-17 08:58:18 +0000 | [diff] [blame] | 48 | return p_f4; // no-warning |
Ted Kremenek | c764d4b | 2009-11-13 20:00:28 +0000 | [diff] [blame] | 49 | } |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 50 | |
| 51 | int *f5() { |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 52 | int *q = malloc(12); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 53 | q = realloc(q, 20); |
| 54 | return q; // no-warning |
| 55 | } |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 56 | |
| 57 | void f6() { |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 58 | int *p = malloc(12); |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 59 | if (!p) |
| 60 | return; // no-warning |
| 61 | else |
| 62 | free(p); |
| 63 | } |
Zhongxing Xu | 425c7ed | 2010-01-18 04:01:40 +0000 | [diff] [blame] | 64 | |
Lenny Maiorani | 4d8d803 | 2011-04-27 14:49:29 +0000 | [diff] [blame] | 65 | void f6_realloc() { |
| 66 | int *p = malloc(12); |
| 67 | if (!p) |
| 68 | return; // no-warning |
| 69 | else |
| 70 | realloc(p,0); |
| 71 | } |
| 72 | |
| 73 | |
Zhongxing Xu | 425c7ed | 2010-01-18 04:01:40 +0000 | [diff] [blame] | 74 | char *doit2(); |
| 75 | void pr6069() { |
| 76 | char *buf = doit2(); |
| 77 | free(buf); |
| 78 | } |
Zhongxing Xu | 181cc3d | 2010-02-14 06:49:48 +0000 | [diff] [blame] | 79 | |
| 80 | void pr6293() { |
| 81 | free(0); |
| 82 | } |
Zhongxing Xu | c802378 | 2010-03-10 04:58:55 +0000 | [diff] [blame] | 83 | |
| 84 | void f7() { |
| 85 | char *x = (char*) malloc(4); |
| 86 | free(x); |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 87 | 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] | 88 | } |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 89 | |
Lenny Maiorani | 4d8d803 | 2011-04-27 14:49:29 +0000 | [diff] [blame] | 90 | void f7_realloc() { |
| 91 | char *x = (char*) malloc(4); |
| 92 | realloc(x,0); |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 93 | 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] | 94 | } |
| 95 | |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 96 | void PR6123() { |
| 97 | int *x = malloc(11); // expected-warning{{Cast a region whose size is not a multiple of the destination type size.}} |
| 98 | } |
| 99 | |
| 100 | void PR7217() { |
| 101 | int *buf = malloc(2); // expected-warning{{Cast a region whose size is not a multiple of the destination type size.}} |
| 102 | buf[1] = 'c'; // not crash |
Zhongxing Xu | ab28099 | 2010-05-25 04:59:19 +0000 | [diff] [blame] | 103 | } |
Jordy Rose | c580f2e | 2010-06-20 04:30:57 +0000 | [diff] [blame] | 104 | |
| 105 | void mallocCastToVoid() { |
| 106 | void *p = malloc(2); |
| 107 | const void *cp = p; // not crash |
| 108 | free(p); |
| 109 | } |
| 110 | |
| 111 | void mallocCastToFP() { |
| 112 | void *p = malloc(2); |
| 113 | void (*fp)() = p; // not crash |
| 114 | free(p); |
| 115 | } |
| 116 | |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 117 | // This tests that malloc() buffers are undefined by default |
| 118 | char mallocGarbage () { |
| 119 | char *buf = malloc(2); |
| 120 | char result = buf[1]; // expected-warning{{undefined}} |
| 121 | free(buf); |
| 122 | return result; |
| 123 | } |
| 124 | |
| 125 | // This tests that calloc() buffers need to be freed |
| 126 | void callocNoFree () { |
| 127 | char *buf = calloc(2,2); |
| 128 | return; // expected-warning{{never released}} |
| 129 | } |
| 130 | |
| 131 | // These test that calloc() buffers are zeroed by default |
| 132 | char callocZeroesGood () { |
| 133 | char *buf = calloc(2,2); |
| 134 | char result = buf[3]; // no-warning |
| 135 | if (buf[1] == 0) { |
| 136 | free(buf); |
| 137 | } |
| 138 | return result; // no-warning |
| 139 | } |
| 140 | |
| 141 | char callocZeroesBad () { |
| 142 | char *buf = calloc(2,2); |
| 143 | char result = buf[3]; // no-warning |
| 144 | if (buf[1] != 0) { |
Tom Care | c4b5bd8 | 2010-07-23 23:04:53 +0000 | [diff] [blame] | 145 | free(buf); // expected-warning{{never executed}} |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 146 | } |
| 147 | return result; // expected-warning{{never released}} |
| 148 | } |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 149 | |
| 150 | void nullFree() { |
| 151 | int *p = 0; |
| 152 | free(p); // no warning - a nop |
| 153 | } |
| 154 | |
| 155 | void paramFree(int *p) { |
| 156 | myfoo(p); |
| 157 | free(p); // no warning |
| 158 | myfoo(p); // TODO: This should be a warning. |
| 159 | } |
| 160 | |
| 161 | int* mallocEscapeRet() { |
| 162 | int *p = malloc(12); |
| 163 | return p; // no warning |
| 164 | } |
| 165 | |
| 166 | void mallocEscapeFoo() { |
| 167 | int *p = malloc(12); |
| 168 | myfoo(p); |
| 169 | return; // no warning |
| 170 | } |
| 171 | |
| 172 | void mallocEscapeFree() { |
| 173 | int *p = malloc(12); |
| 174 | myfoo(p); |
| 175 | free(p); |
| 176 | } |
| 177 | |
| 178 | void mallocEscapeFreeFree() { |
| 179 | int *p = malloc(12); |
| 180 | myfoo(p); |
| 181 | free(p); |
| 182 | free(p); // expected-warning{{Try to free a memory block that has been released}} |
| 183 | } |
| 184 | |
| 185 | void mallocEscapeFreeUse() { |
| 186 | int *p = malloc(12); |
| 187 | myfoo(p); |
| 188 | free(p); |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 189 | 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] | 190 | } |
| 191 | |
| 192 | int *myalloc(); |
| 193 | void myalloc2(int **p); |
| 194 | |
| 195 | void mallocEscapeFreeCustomAlloc() { |
| 196 | int *p = malloc(12); |
| 197 | myfoo(p); |
| 198 | free(p); |
| 199 | p = myalloc(); |
| 200 | free(p); // no warning |
| 201 | } |
| 202 | |
| 203 | void mallocEscapeFreeCustomAlloc2() { |
| 204 | int *p = malloc(12); |
| 205 | myfoo(p); |
| 206 | free(p); |
| 207 | myalloc2(&p); |
| 208 | free(p); // no warning |
| 209 | } |
| 210 | |
| 211 | void mallocBindFreeUse() { |
| 212 | int *x = malloc(12); |
| 213 | int *y = x; |
| 214 | free(y); |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 215 | 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] | 216 | } |
| 217 | |
| 218 | void mallocEscapeMalloc() { |
| 219 | int *p = malloc(12); |
| 220 | myfoo(p); |
| 221 | p = malloc(12); // expected-warning{{Allocated memory never released. Potential memory leak.}} |
| 222 | } |
| 223 | |
| 224 | void mallocMalloc() { |
| 225 | int *p = malloc(12); |
| 226 | p = malloc(12); // expected-warning{{Allocated memory never released. Potential memory leak}} |
| 227 | } |
| 228 | |
| 229 | void mallocFreeMalloc() { |
| 230 | int *p = malloc(12); |
| 231 | free(p); |
| 232 | p = malloc(12); |
| 233 | free(p); |
| 234 | } |
| 235 | |
Anna Zaks | cdfec5e | 2012-02-09 06:25:47 +0000 | [diff] [blame] | 236 | void mallocFreeUse_params() { |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 237 | int *p = malloc(12); |
| 238 | free(p); |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 239 | myfoo(p); //expected-warning{{Use of dynamically allocated memory after it is freed}} |
| 240 | 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] | 241 | } |
| 242 | |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 243 | void mallocFailedOrNot() { |
| 244 | int *p = malloc(12); |
| 245 | if (!p) |
| 246 | free(p); |
| 247 | else |
| 248 | free(p); |
| 249 | } |
| 250 | |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 251 | struct StructWithInt { |
| 252 | int g; |
| 253 | }; |
Anna Zaks | 0860cd0 | 2012-02-11 21:44:39 +0000 | [diff] [blame^] | 254 | |
| 255 | int *mallocReturnFreed() { |
| 256 | int *p = malloc(12); |
| 257 | free(p); |
| 258 | return p; // expected-warning {{Use of dynamically allocated}} |
| 259 | } |
| 260 | |
| 261 | int useAfterFreeStruct() { |
| 262 | struct StructWithInt *px= malloc(sizeof(struct StructWithInt)); |
| 263 | px->g = 5; |
| 264 | free(px); |
| 265 | return px->g; // expected-warning {{Use of dynamically allocated}} |
| 266 | } |
| 267 | |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 268 | void nonSymbolAsFirstArg(int *pp, struct StructWithInt *p); |
| 269 | |
| 270 | void mallocEscapeFooNonSymbolArg() { |
| 271 | struct StructWithInt *p = malloc(sizeof(struct StructWithInt)); |
| 272 | nonSymbolAsFirstArg(&p->g, p); |
| 273 | return; // no warning |
| 274 | } |
| 275 | |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 276 | void mallocFailedOrNotLeak() { |
| 277 | int *p = malloc(12); |
| 278 | if (p == 0) |
| 279 | return; // no warning |
| 280 | else |
| 281 | return; // expected-warning {{Allocated memory never released. Potential memory leak.}} |
| 282 | } |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 283 | |
Anna Zaks | cdfec5e | 2012-02-09 06:25:47 +0000 | [diff] [blame] | 284 | int *Gl; |
| 285 | struct GlStTy { |
| 286 | int *x; |
| 287 | }; |
| 288 | |
| 289 | struct GlStTy GlS = {0}; |
| 290 | |
| 291 | void GlobalFree() { |
| 292 | free(Gl); |
| 293 | } |
| 294 | |
| 295 | void GlobalMalloc() { |
| 296 | Gl = malloc(12); |
| 297 | } |
| 298 | |
| 299 | void GlobalStructMalloc() { |
| 300 | int *a = malloc(12); |
| 301 | GlS.x = a; |
| 302 | } |
| 303 | |
| 304 | void GlobalStructMallocFree() { |
| 305 | int *a = malloc(12); |
| 306 | GlS.x = a; |
| 307 | free(GlS.x); |
| 308 | } |
Anna Zaks | f8b1c31 | 2012-02-10 01:11:03 +0000 | [diff] [blame] | 309 | |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 310 | // Region escape testing. |
| 311 | |
| 312 | unsigned takePtrToPtr(int **p); |
| 313 | void PassTheAddrOfAllocatedData(int f) { |
| 314 | int *p = malloc(12); |
| 315 | // We don't know what happens after the call. Should stop tracking here. |
| 316 | if (takePtrToPtr(&p)) |
| 317 | f++; |
| 318 | free(p); // no warning |
| 319 | } |
| 320 | |
| 321 | struct X { |
| 322 | int *p; |
| 323 | }; |
| 324 | unsigned takePtrToStruct(struct X *s); |
| 325 | int ** foo2(int *g, int f) { |
| 326 | int *p = malloc(12); |
| 327 | struct X *px= malloc(sizeof(struct X)); |
| 328 | px->p = p; |
| 329 | // We don't know what happens after this call. Should not track px nor p. |
| 330 | if (takePtrToStruct(px)) |
| 331 | f++; |
| 332 | free(p); |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | struct X* RegInvalidationDetect1(struct X *s2) { |
| 337 | struct X *px= malloc(sizeof(struct X)); |
| 338 | px->p = 0; |
| 339 | px = s2; |
| 340 | return px; // expected-warning {{Allocated memory never released. Potential memory leak.}} |
| 341 | } |
| 342 | |
| 343 | struct X* RegInvalidationGiveUp1() { |
| 344 | int *p = malloc(12); |
| 345 | struct X *px= malloc(sizeof(struct X)); |
| 346 | px->p = p; |
| 347 | return px; |
| 348 | } |
| 349 | |
| 350 | int **RegInvalidationDetect2(int **pp) { |
| 351 | int *p = malloc(12); |
| 352 | pp = &p; |
| 353 | pp++; |
| 354 | return 0;// expected-warning {{Allocated memory never released. Potential memory leak.}} |
| 355 | } |
Anna Zaks | f8b1c31 | 2012-02-10 01:11:03 +0000 | [diff] [blame] | 356 | |
Anna Zaks | f8b1c31 | 2012-02-10 01:11:03 +0000 | [diff] [blame] | 357 | extern void exit(int) __attribute__ ((__noreturn__)); |
| 358 | void mallocExit(int *g) { |
| 359 | struct xx *p = malloc(12); |
Anna Zaks | da04677 | 2012-02-11 21:02:40 +0000 | [diff] [blame] | 360 | if (g != 0) |
| 361 | exit(1); |
Anna Zaks | f8b1c31 | 2012-02-10 01:11:03 +0000 | [diff] [blame] | 362 | free(p); |
| 363 | return; |
| 364 | } |
| 365 | |
Anna Zaks | f8b1c31 | 2012-02-10 01:11:03 +0000 | [diff] [blame] | 366 | extern void __assert_fail (__const char *__assertion, __const char *__file, |
| 367 | unsigned int __line, __const char *__function) |
| 368 | __attribute__ ((__noreturn__)); |
| 369 | #define assert(expr) \ |
| 370 | ((expr) ? (void)(0) : __assert_fail (#expr, __FILE__, __LINE__, __func__)) |
| 371 | void mallocAssert(int *g) { |
| 372 | struct xx *p = malloc(12); |
| 373 | |
Anna Zaks | da04677 | 2012-02-11 21:02:40 +0000 | [diff] [blame] | 374 | assert(g != 0); |
Anna Zaks | f8b1c31 | 2012-02-10 01:11:03 +0000 | [diff] [blame] | 375 | free(p); |
| 376 | return; |
| 377 | } |
| 378 | |
Anna Zaks | da04677 | 2012-02-11 21:02:40 +0000 | [diff] [blame] | 379 | // Below are the known false positives. |
| 380 | |
Anna Zaks | f8b1c31 | 2012-02-10 01:11:03 +0000 | [diff] [blame] | 381 | // TODO: There should be no warning here. |
Anna Zaks | f8b1c31 | 2012-02-10 01:11:03 +0000 | [diff] [blame] | 382 | void reallocFails(int *g, int f) { |
| 383 | char *p = malloc(12); |
| 384 | char *r = realloc(p, 12+1); |
| 385 | if (!r) { |
| 386 | free(p); // expected-warning {{Try to free a memory block that has been released}} |
| 387 | } else { |
| 388 | free(r); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | // TODO: There should be no warning here. This one might be difficult to get rid of. |
| 393 | void dependsOnValueOfPtr(int *g, unsigned f) { |
| 394 | int *p; |
| 395 | |
| 396 | if (f) { |
| 397 | p = g; |
| 398 | } else { |
| 399 | p = malloc(12); |
| 400 | } |
| 401 | |
| 402 | if (p != g) |
| 403 | free(p); |
| 404 | else |
| 405 | return; // expected-warning{{Allocated memory never released. Potential memory leak}} |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | // TODO: Should this be a warning? |
| 410 | // Here we are returning a pointer one past the allocated value. An idiom which |
| 411 | // can be used for implementing special malloc. The correct uses of this might |
| 412 | // be rare enough so that we could keep this as a warning. |
| 413 | static void *specialMalloc(int n){ |
| 414 | int *p; |
| 415 | p = malloc( n+8 ); |
| 416 | if( p ){ |
| 417 | p[0] = n; |
| 418 | p++; |
| 419 | } |
| 420 | return p;// expected-warning {{Allocated memory never released. Potential memory leak.}} |
| 421 | } |