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