| 1 |
// RUN: %clang_analyze_cc1 -Wno-strict-prototypes -Wno-error=implicit-int -verify %s \ |
| 2 |
// RUN: -Wno-alloc-size \ |
| 3 |
// RUN: -analyzer-checker=core \ |
| 4 |
// RUN: -analyzer-checker=alpha.deadcode.UnreachableCode \ |
| 5 |
// RUN: -analyzer-checker=unix \ |
| 6 |
// RUN: -analyzer-checker=debug.ExprInspection \ |
| 7 |
// RUN: -analyzer-checker=optin.taint.TaintPropagation \ |
| 8 |
// RUN: -analyzer-checker=optin.taint.TaintedAlloc |
| 9 |
|
| 10 |
#include "Inputs/system-header-simulator.h" |
| 11 |
|
| 12 |
void clang_analyzer_eval(int); |
| 13 |
void clang_analyzer_dump(int); |
| 14 |
void clang_analyzer_dumpExtent(void *); |
| 15 |
|
| 16 |
// Without -fms-compatibility, wchar_t isn't a builtin type. MSVC defines |
| 17 |
// _WCHAR_T_DEFINED if wchar_t is available. Microsoft recommends that you use |
| 18 |
// the builtin type: "Using the typedef version can cause portability |
| 19 |
// problems", but we're ok here because we're not actually running anything. |
| 20 |
// Also of note is this cryptic warning: "The wchar_t type is not supported |
| 21 |
// when you compile C code". |
| 22 |
// |
| 23 |
// See the docs for more: |
| 24 |
// https://msdn.microsoft.com/en-us/library/dh8che7s.aspx |
| 25 |
#if !defined(_WCHAR_T_DEFINED) |
| 26 |
// "Microsoft implements wchar_t as a two-byte unsigned value" |
| 27 |
typedef unsigned short wchar_t; |
| 28 |
#define _WCHAR_T_DEFINED |
| 29 |
#endif // !defined(_WCHAR_T_DEFINED) |
| 30 |
|
| 31 |
typedef __typeof(sizeof(int)) size_t; |
| 32 |
void *malloc(size_t); |
| 33 |
void *alloca(size_t); |
| 34 |
void *valloc(size_t); |
| 35 |
void free(void *); |
| 36 |
void *realloc(void *ptr, size_t size); |
| 37 |
void *reallocf(void *ptr, size_t size); |
| 38 |
void *calloc(size_t nmemb, size_t size); |
| 39 |
char *strdup(const char *s); |
| 40 |
wchar_t *wcsdup(const wchar_t *s); |
| 41 |
char *strndup(const char *s, size_t n); |
| 42 |
int memcmp(const void *s1, const void *s2, size_t n); |
| 43 |
|
| 44 |
// Windows variants |
| 45 |
char *_strdup(const char *strSource); |
| 46 |
wchar_t *_wcsdup(const wchar_t *strSource); |
| 47 |
void *_alloca(size_t size); |
| 48 |
|
| 49 |
void myfoo(int *p); |
| 50 |
void myfooint(int p); |
| 51 |
char *fooRetPtr(void); |
| 52 |
|
| 53 |
void t1(void) { |
| 54 |
size_t size = 0; |
| 55 |
scanf("%zu", &size); |
| 56 |
int *p = malloc(size); // expected-warning{{malloc is called with a tainted (potentially attacker controlled) value}} |
| 57 |
free(p); |
| 58 |
} |
| 59 |
|
| 60 |
void t2(void) { |
| 61 |
size_t size = 0; |
| 62 |
scanf("%zu", &size); |
| 63 |
int *p = calloc(size,2); // expected-warning{{calloc is called with a tainted (potentially attacker controlled) value}} |
| 64 |
free(p); |
| 65 |
} |
| 66 |
|
| 67 |
void t3(void) { |
| 68 |
size_t size = 0; |
| 69 |
scanf("%zu", &size); |
| 70 |
if (1024 < size) |
| 71 |
return; |
| 72 |
int *p = malloc(size); // No warning expected as the the user input is bound |
| 73 |
free(p); |
| 74 |
} |
| 75 |
|
| 76 |
void t4(void) { |
| 77 |
size_t size = 0; |
| 78 |
int *p = malloc(sizeof(int)); |
| 79 |
scanf("%zu", &size); |
| 80 |
p = (int*) realloc((void*) p, size); // expected-warning{{realloc is called with a tainted (potentially attacker controlled) value}} |
| 81 |
free(p); |
| 82 |
} |
| 83 |
|
| 84 |
void t5(void) { |
| 85 |
size_t size = 0; |
| 86 |
int *p = alloca(sizeof(int)); |
| 87 |
scanf("%zu", &size); |
| 88 |
p = (int*) alloca(size); // expected-warning{{alloca is called with a tainted (potentially attacker controlled) value}} |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
void f1(void) { |
| 93 |
int *p = malloc(12); |
| 94 |
return; // expected-warning{{Potential leak of memory pointed to by 'p'}} |
| 95 |
} |
| 96 |
|
| 97 |
void f2(void) { |
| 98 |
int *p = malloc(12); |
| 99 |
free(p); |
| 100 |
free(p); // expected-warning{{Attempt to release already released memory}} |
| 101 |
} |
| 102 |
|
| 103 |
void f2_realloc_0(void) { |
| 104 |
int *p = malloc(12); |
| 105 |
realloc(p,0); |
| 106 |
realloc(p,0); // expected-warning{{Attempt to release already released memory}} |
| 107 |
} |
| 108 |
|
| 109 |
void f2_realloc_1(void) { |
| 110 |
int *p = malloc(12); |
| 111 |
int *q = realloc(p,0); // no-warning |
| 112 |
} |
| 113 |
|
| 114 |
void reallocNotNullPtr(unsigned sizeIn) { |
| 115 |
unsigned size = 12; |
| 116 |
char *p = (char*)malloc(size); |
| 117 |
if (p) { |
| 118 |
char *q = (char*)realloc(p, sizeIn); |
| 119 |
char x = *q; // expected-warning {{Potential leak of memory pointed to by 'q'}} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
void allocaTest(void) { |
| 124 |
int *p = alloca(sizeof(int)); |
| 125 |
} // no warn |
| 126 |
|
| 127 |
void winAllocaTest(void) { |
| 128 |
int *p = _alloca(sizeof(int)); |
| 129 |
} // no warn |
| 130 |
|
| 131 |
void allocaBuiltinTest(void) { |
| 132 |
int *p = __builtin_alloca(sizeof(int)); |
| 133 |
} // no warn |
| 134 |
|
| 135 |
int *realloctest1(void) { |
| 136 |
int *q = malloc(12); |
| 137 |
q = realloc(q, 20); |
| 138 |
return q; // no warning - returning the allocated value |
| 139 |
} |
| 140 |
|
| 141 |
// p should be freed if realloc fails. |
| 142 |
void reallocFails(void) { |
| 143 |
char *p = malloc(12); |
| 144 |
char *r = realloc(p, 12+1); |
| 145 |
if (!r) { |
| 146 |
free(p); |
| 147 |
} else { |
| 148 |
free(r); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
void reallocSizeZero1(void) { |
| 153 |
char *p = malloc(12); |
| 154 |
char *r = realloc(p, 0); |
| 155 |
if (!r) { |
| 156 |
free(p); // expected-warning {{Attempt to release already released memory}} |
| 157 |
} else { |
| 158 |
free(r); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
void reallocSizeZero2(void) { |
| 163 |
char *p = malloc(12); |
| 164 |
char *r = realloc(p, 0); |
| 165 |
if (!r) { |
| 166 |
free(p); // expected-warning {{Attempt to release already released memory}} |
| 167 |
} else { |
| 168 |
free(r); |
| 169 |
} |
| 170 |
free(p); // expected-warning {{Attempt to release already released memory}} |
| 171 |
} |
| 172 |
|
| 173 |
void reallocSizeZero3(void) { |
| 174 |
char *p = malloc(12); |
| 175 |
char *r = realloc(p, 0); |
| 176 |
free(r); |
| 177 |
} |
| 178 |
|
| 179 |
void reallocSizeZero4(void) { |
| 180 |
char *r = realloc(0, 0); |
| 181 |
free(r); |
| 182 |
} |
| 183 |
|
| 184 |
void reallocSizeZero5(void) { |
| 185 |
char *r = realloc(0, 0); |
| 186 |
} |
| 187 |
|
| 188 |
void reallocPtrZero1(void) { |
| 189 |
char *r = realloc(0, 12); |
| 190 |
} // expected-warning {{Potential leak of memory pointed to by 'r'}} |
| 191 |
|
| 192 |
void reallocPtrZero2(void) { |
| 193 |
char *r = realloc(0, 12); |
| 194 |
if (r) |
| 195 |
free(r); |
| 196 |
} |
| 197 |
|
| 198 |
void reallocPtrZero3(void) { |
| 199 |
char *r = realloc(0, 12); |
| 200 |
free(r); |
| 201 |
} |
| 202 |
|
| 203 |
void reallocRadar6337483_1(void) { |
| 204 |
char *buf = malloc(100); |
| 205 |
buf = (char*)realloc(buf, 0x1000000); |
| 206 |
if (!buf) { |
| 207 |
return;// expected-warning {{Potential leak of memory pointed to by}} |
| 208 |
} |
| 209 |
free(buf); |
| 210 |
} |
| 211 |
|
| 212 |
void reallocRadar6337483_2(void) { |
| 213 |
char *buf = malloc(100); |
| 214 |
char *buf2 = (char*)realloc(buf, 0x1000000); |
| 215 |
if (!buf2) { |
| 216 |
; |
| 217 |
} else { |
| 218 |
free(buf2); |
| 219 |
} |
| 220 |
} // expected-warning {{Potential leak of memory pointed to by}} |
| 221 |
|
| 222 |
void reallocRadar6337483_3(void) { |
| 223 |
char * buf = malloc(100); |
| 224 |
char * tmp; |
| 225 |
tmp = (char*)realloc(buf, 0x1000000); |
| 226 |
if (!tmp) { |
| 227 |
free(buf); |
| 228 |
return; |
| 229 |
} |
| 230 |
buf = tmp; |
| 231 |
free(buf); |
| 232 |
} |
| 233 |
|
| 234 |
void reallocRadar6337483_4(void) { |
| 235 |
char *buf = malloc(100); |
| 236 |
char *buf2 = (char*)realloc(buf, 0x1000000); |
| 237 |
if (!buf2) { |
| 238 |
return; // expected-warning {{Potential leak of memory pointed to by}} |
| 239 |
} else { |
| 240 |
free(buf2); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
int *reallocfTest1(void) { |
| 245 |
int *q = malloc(12); |
| 246 |
q = reallocf(q, 20); |
| 247 |
return q; // no warning - returning the allocated value |
| 248 |
} |
| 249 |
|
| 250 |
void reallocfRadar6337483_4(void) { |
| 251 |
char *buf = malloc(100); |
| 252 |
char *buf2 = (char*)reallocf(buf, 0x1000000); |
| 253 |
if (!buf2) { |
| 254 |
return; // no warning - reallocf frees even on failure |
| 255 |
} else { |
| 256 |
free(buf2); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
void reallocfRadar6337483_3(void) { |
| 261 |
char * buf = malloc(100); |
| 262 |
char * tmp; |
| 263 |
tmp = (char*)reallocf(buf, 0x1000000); |
| 264 |
if (!tmp) { |
| 265 |
free(buf); // expected-warning {{Attempt to release already released memory}} |
| 266 |
return; |
| 267 |
} |
| 268 |
buf = tmp; |
| 269 |
free(buf); |
| 270 |
} |
| 271 |
|
| 272 |
void reallocfPtrZero1(void) { |
| 273 |
char *r = reallocf(0, 12); |
| 274 |
} // expected-warning {{Potential leak of memory pointed to by}} |
| 275 |
|
| 276 |
//------------------- Check usage of zero-allocated memory --------------------- |
| 277 |
void CheckUseZeroAllocatedNoWarn1(void) { |
| 278 |
int *p = malloc(0); |
| 279 |
free(p); // no warning |
| 280 |
} |
| 281 |
|
| 282 |
void CheckUseZeroAllocatedNoWarn2(void) { |
| 283 |
int *p = alloca(0); // no warning |
| 284 |
} |
| 285 |
|
| 286 |
void CheckUseZeroWinAllocatedNoWarn2(void) { |
| 287 |
int *p = _alloca(0); // no warning |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
void CheckUseZeroAllocatedNoWarn3(void) { |
| 292 |
int *p = malloc(0); |
| 293 |
int *q = realloc(p, 8); // no warning |
| 294 |
free(q); |
| 295 |
} |
| 296 |
|
| 297 |
void CheckUseZeroAllocatedNoWarn4(void) { |
| 298 |
int *p = realloc(0, 8); |
| 299 |
*p = 1; // no warning |
| 300 |
free(p); |
| 301 |
} |
| 302 |
|
| 303 |
void CheckUseZeroAllocated1(void) { |
| 304 |
int *p = malloc(0); |
| 305 |
*p = 1; // expected-warning {{Use of memory allocated with size zero}} |
| 306 |
free(p); |
| 307 |
} |
| 308 |
|
| 309 |
char CheckUseZeroAllocated2(void) { |
| 310 |
// NOTE: The `AllocaRegion` that models the return value of `alloca()` |
| 311 |
// doesn't have an associated symbol, so the current implementation of |
| 312 |
// `MallocChecker::checkUseZeroAllocated()` cannot provide warnings for it. |
| 313 |
// However, other checkers like core.uninitialized.UndefReturn (that |
| 314 |
// activates in these TCs) or the array bound checkers provide more generic, |
| 315 |
// but still sufficient warnings in these cases, so I think it isn't |
| 316 |
// important to cover this in MallocChecker. |
| 317 |
char *p = alloca(0); |
| 318 |
return *p; // expected-warning {{Undefined or garbage value returned to caller}} |
| 319 |
} |
| 320 |
|
| 321 |
char CheckUseZeroWinAllocated2(void) { |
| 322 |
// Note: Same situation as `CheckUseZeroAllocated2()`. |
| 323 |
char *p = _alloca(0); |
| 324 |
return *p; // expected-warning {{Undefined or garbage value returned to caller}} |
| 325 |
} |
| 326 |
|
| 327 |
void UseZeroAllocated(int *p) { |
| 328 |
if (p) |
| 329 |
*p = 7; // expected-warning {{Use of memory allocated with size zero}} |
| 330 |
} |
| 331 |
void CheckUseZeroAllocated3(void) { |
| 332 |
int *p = malloc(0); |
| 333 |
UseZeroAllocated(p); |
| 334 |
} |
| 335 |
|
| 336 |
void f(char); |
| 337 |
void CheckUseZeroAllocated4(void) { |
| 338 |
char *p = valloc(0); |
| 339 |
f(*p); // expected-warning {{Use of memory allocated with size zero}} |
| 340 |
free(p); |
| 341 |
} |
| 342 |
|
| 343 |
void CheckUseZeroAllocated5(void) { |
| 344 |
int *p = calloc(0, 2); |
| 345 |
*p = 1; // expected-warning {{Use of memory allocated with size zero}} |
| 346 |
free(p); |
| 347 |
} |
| 348 |
|
| 349 |
void CheckUseZeroAllocated6(void) { |
| 350 |
int *p = calloc(2, 0); |
| 351 |
*p = 1; // expected-warning {{Use of memory allocated with size zero}} |
| 352 |
free(p); |
| 353 |
} |
| 354 |
|
| 355 |
void CheckUseZeroAllocated7(void) { |
| 356 |
int *p = realloc(0, 0); |
| 357 |
*p = 1; // expected-warning {{Use of memory allocated with size zero}} |
| 358 |
free(p); |
| 359 |
} |
| 360 |
|
| 361 |
void CheckUseZeroAllocated8(void) { |
| 362 |
int *p = malloc(8); |
| 363 |
int *q = realloc(p, 0); |
| 364 |
*q = 1; // expected-warning {{Use of memory allocated with size zero}} |
| 365 |
free(q); |
| 366 |
} |
| 367 |
|
| 368 |
void CheckUseZeroAllocated9(void) { |
| 369 |
int *p = realloc(0, 0); |
| 370 |
int *q = realloc(p, 0); |
| 371 |
*q = 1; // expected-warning {{Use of memory allocated with size zero}} |
| 372 |
free(q); |
| 373 |
} |
| 374 |
|
| 375 |
void CheckUseZeroAllocatedPathNoWarn(_Bool b) { |
| 376 |
int s = 0; |
| 377 |
if (b) |
| 378 |
s= 10; |
| 379 |
|
| 380 |
char *p = malloc(s); |
| 381 |
|
| 382 |
if (b) |
| 383 |
*p = 1; // no warning |
| 384 |
|
| 385 |
free(p); |
| 386 |
} |
| 387 |
|
| 388 |
void CheckUseZeroAllocatedPathWarn(_Bool b) { |
| 389 |
int s = 10; |
| 390 |
if (b) |
| 391 |
s= 0; |
| 392 |
|
| 393 |
char *p = malloc(s); |
| 394 |
|
| 395 |
if (b) |
| 396 |
*p = 1; // expected-warning {{Use of memory allocated with size zero}} |
| 397 |
|
| 398 |
free(p); |
| 399 |
} |
| 400 |
|
| 401 |
void CheckUseZeroReallocatedPathNoWarn(_Bool b) { |
| 402 |
int s = 0; |
| 403 |
if (b) |
| 404 |
s= 10; |
| 405 |
|
| 406 |
char *p = malloc(8); |
| 407 |
char *q = realloc(p, s); |
| 408 |
|
| 409 |
if (b) |
| 410 |
*q = 1; // no warning |
| 411 |
|
| 412 |
free(q); |
| 413 |
} |
| 414 |
|
| 415 |
void CheckUseZeroReallocatedPathWarn(_Bool b) { |
| 416 |
int s = 10; |
| 417 |
if (b) |
| 418 |
s= 0; |
| 419 |
|
| 420 |
char *p = malloc(8); |
| 421 |
char *q = realloc(p, s); |
| 422 |
|
| 423 |
if (b) |
| 424 |
*q = 1; // expected-warning {{Use of memory allocated with size zero}} |
| 425 |
|
| 426 |
free(q); |
| 427 |
} |
| 428 |
|
| 429 |
// This case tests that storing malloc'ed memory to a static variable which is |
| 430 |
// then returned is not leaked. In the absence of known contracts for functions |
| 431 |
// or inter-procedural analysis, this is a conservative answer. |
| 432 |
int *f3(void) { |
| 433 |
static int *p = 0; |
| 434 |
p = malloc(12); |
| 435 |
return p; // no-warning |
| 436 |
} |
| 437 |
|
| 438 |
// This case tests that storing malloc'ed memory to a static global variable |
| 439 |
// which is then returned is not leaked. In the absence of known contracts for |
| 440 |
// functions or inter-procedural analysis, this is a conservative answer. |
| 441 |
static int *p_f4 = 0; |
| 442 |
int *f4(void) { |
| 443 |
p_f4 = malloc(12); |
| 444 |
return p_f4; // no-warning |
| 445 |
} |
| 446 |
|
| 447 |
int *f5(void) { |
| 448 |
int *q = malloc(12); |
| 449 |
q = realloc(q, 20); |
| 450 |
return q; // no-warning |
| 451 |
} |
| 452 |
|
| 453 |
void f6(void) { |
| 454 |
int *p = malloc(12); |
| 455 |
if (!p) |
| 456 |
return; // no-warning |
| 457 |
else |
| 458 |
free(p); |
| 459 |
} |
| 460 |
|
| 461 |
void f6_realloc(void) { |
| 462 |
int *p = malloc(12); |
| 463 |
if (!p) |
| 464 |
return; // no-warning |
| 465 |
else |
| 466 |
realloc(p,0); |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
char *doit2(void); |
| 471 |
void pr6069(void) { |
| 472 |
char *buf = doit2(); |
| 473 |
free(buf); |
| 474 |
} |
| 475 |
|
| 476 |
void pr6293(void) { |
| 477 |
free(0); |
| 478 |
} |
| 479 |
|
| 480 |
void f7(void) { |
| 481 |
char *x = (char*) malloc(4); |
| 482 |
free(x); |
| 483 |
x[0] = 'a'; // expected-warning{{Use of memory after it is released}} |
| 484 |
} |
| 485 |
|
| 486 |
void f8(void) { |
| 487 |
char *x = (char*) malloc(4); |
| 488 |
free(x); |
| 489 |
char *y = strndup(x, 4); // expected-warning{{Use of memory after it is released}} |
| 490 |
} |
| 491 |
|
| 492 |
void f7_realloc(void) { |
| 493 |
char *x = (char*) malloc(4); |
| 494 |
realloc(x,0); |
| 495 |
x[0] = 'a'; // expected-warning{{Use of memory after it is released}} |
| 496 |
} |
| 497 |
|
| 498 |
void mallocCastToVoid(void) { |
| 499 |
void *p = malloc(2); |
| 500 |
const void *cp = p; // not crash |
| 501 |
free(p); |
| 502 |
} |
| 503 |
|
| 504 |
void mallocCastToFP(void) { |
| 505 |
void *p = malloc(2); |
| 506 |
void (*fp)(void) = p; // not crash |
| 507 |
free(p); |
| 508 |
} |
| 509 |
|
| 510 |
// This tests that 'malloc()' buffers are undefined by default |
| 511 |
char mallocGarbage (void) { |
| 512 |
char *buf = malloc(2); |
| 513 |
char result = buf[1]; // expected-warning{{uninitialized}} |
| 514 |
free(buf); |
| 515 |
return result; |
| 516 |
} |
| 517 |
|
| 518 |
// This tests that calloc() buffers need to be freed |
| 519 |
void callocNoFree (void) { |
| 520 |
char *buf = calloc(2,2); |
| 521 |
return; // expected-warning{{Potential leak of memory pointed to by 'buf'}} |
| 522 |
} |
| 523 |
|
| 524 |
// These test that calloc() buffers are zeroed by default |
| 525 |
char callocZeroesGood (void) { |
| 526 |
char *buf = calloc(2,2); |
| 527 |
char result = buf[3]; // no-warning |
| 528 |
if (buf[1] == 0) { |
| 529 |
free(buf); |
| 530 |
} |
| 531 |
return result; // no-warning |
| 532 |
} |
| 533 |
|
| 534 |
char callocZeroesBad (void) { |
| 535 |
char *buf = calloc(2,2); |
| 536 |
char result = buf[3]; // no-warning |
| 537 |
if (buf[1] != 0) { |
| 538 |
free(buf); // expected-warning{{never executed}} |
| 539 |
} |
| 540 |
return result; // expected-warning{{Potential leak of memory pointed to by 'buf'}} |
| 541 |
} |
| 542 |
|
| 543 |
void nullFree(void) { |
| 544 |
int *p = 0; |
| 545 |
free(p); // no warning - a nop |
| 546 |
} |
| 547 |
|
| 548 |
void paramFree(int *p) { |
| 549 |
myfoo(p); |
| 550 |
free(p); // no warning |
| 551 |
myfoo(p); // expected-warning {{Use of memory after it is released}} |
| 552 |
} |
| 553 |
|
| 554 |
void allocaFree(void) { |
| 555 |
int *p = alloca(sizeof(int)); |
| 556 |
free(p); // expected-warning {{Memory allocated by 'alloca()' should not be deallocated}} |
| 557 |
} |
| 558 |
|
| 559 |
void allocaFreeBuiltin(void) { |
| 560 |
int *p = __builtin_alloca(sizeof(int)); |
| 561 |
free(p); // expected-warning {{Memory allocated by 'alloca()' should not be deallocated}} |
| 562 |
} |
| 563 |
|
| 564 |
void allocaFreeBuiltinAlign(void) { |
| 565 |
int *p = __builtin_alloca_with_align(sizeof(int), 64); |
| 566 |
free(p); // expected-warning {{Memory allocated by 'alloca()' should not be deallocated}} |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
int* mallocEscapeRet(void) { |
| 571 |
int *p = malloc(12); |
| 572 |
return p; // no warning |
| 573 |
} |
| 574 |
|
| 575 |
void mallocEscapeFoo(void) { |
| 576 |
int *p = malloc(12); |
| 577 |
myfoo(p); |
| 578 |
return; // no warning |
| 579 |
} |
| 580 |
|
| 581 |
void mallocEscapeFree(void) { |
| 582 |
int *p = malloc(12); |
| 583 |
myfoo(p); |
| 584 |
free(p); |
| 585 |
} |
| 586 |
|
| 587 |
void mallocEscapeFreeFree(void) { |
| 588 |
int *p = malloc(12); |
| 589 |
myfoo(p); |
| 590 |
free(p); |
| 591 |
free(p); // expected-warning{{Attempt to release already released memory}} |
| 592 |
} |
| 593 |
|
| 594 |
void mallocEscapeFreeUse(void) { |
| 595 |
int *p = malloc(12); |
| 596 |
myfoo(p); |
| 597 |
free(p); |
| 598 |
myfoo(p); // expected-warning{{Use of memory after it is released}} |
| 599 |
} |
| 600 |
|
| 601 |
int *myalloc(void); |
| 602 |
void myalloc2(int **p); |
| 603 |
|
| 604 |
void mallocEscapeFreeCustomAlloc(void) { |
| 605 |
int *p = malloc(12); |
| 606 |
myfoo(p); |
| 607 |
free(p); |
| 608 |
p = myalloc(); |
| 609 |
free(p); // no warning |
| 610 |
} |
| 611 |
|
| 612 |
void mallocEscapeFreeCustomAlloc2(void) { |
| 613 |
int *p = malloc(12); |
| 614 |
myfoo(p); |
| 615 |
free(p); |
| 616 |
myalloc2(&p); |
| 617 |
free(p); // no warning |
| 618 |
} |
| 619 |
|
| 620 |
void mallocBindFreeUse(void) { |
| 621 |
int *x = malloc(12); |
| 622 |
int *y = x; |
| 623 |
free(y); |
| 624 |
myfoo(x); // expected-warning{{Use of memory after it is released}} |
| 625 |
} |
| 626 |
|
| 627 |
void mallocEscapeMalloc(void) { |
| 628 |
int *p = malloc(12); |
| 629 |
myfoo(p); |
| 630 |
p = malloc(12); |
| 631 |
} // expected-warning{{Potential leak of memory pointed to by}} |
| 632 |
|
| 633 |
void mallocMalloc(void) { |
| 634 |
int *p = malloc(12); |
| 635 |
p = malloc(12); |
| 636 |
} // expected-warning {{Potential leak of memory pointed to by}}\ |
| 637 |
// expected-warning {{Potential leak of memory pointed to by}} |
| 638 |
|
| 639 |
void mallocFreeMalloc(void) { |
| 640 |
int *p = malloc(12); |
| 641 |
free(p); |
| 642 |
p = malloc(12); |
| 643 |
free(p); |
| 644 |
} |
| 645 |
|
| 646 |
void mallocFreeUse_params(void) { |
| 647 |
int *p = malloc(12); |
| 648 |
free(p); |
| 649 |
myfoo(p); //expected-warning{{Use of memory after it is released}} |
| 650 |
} |
| 651 |
|
| 652 |
void mallocFreeUse_params2(void) { |
| 653 |
int *p = malloc(12); |
| 654 |
free(p); |
| 655 |
myfooint(*p); //expected-warning{{Use of memory after it is released}} |
| 656 |
} |
| 657 |
|
| 658 |
void mallocFailedOrNot(void) { |
| 659 |
int *p = malloc(12); |
| 660 |
if (!p) |
| 661 |
free(p); |
| 662 |
else |
| 663 |
free(p); |
| 664 |
} |
| 665 |
|
| 666 |
struct StructWithInt { |
| 667 |
int g; |
| 668 |
}; |
| 669 |
|
| 670 |
int *mallocReturnFreed(void) { |
| 671 |
int *p = malloc(12); |
| 672 |
free(p); |
| 673 |
return p; // expected-warning {{Use of memory after it is released}} |
| 674 |
} |
| 675 |
|
| 676 |
int useAfterFreeStruct(void) { |
| 677 |
struct StructWithInt *px= malloc(sizeof(struct StructWithInt)); |
| 678 |
px->g = 5; |
| 679 |
free(px); |
| 680 |
return px->g; // expected-warning {{Use of memory after it is released}} |
| 681 |
} |
| 682 |
|
| 683 |
void nonSymbolAsFirstArg(int *pp, struct StructWithInt *p); |
| 684 |
|
| 685 |
void mallocEscapeFooNonSymbolArg(void) { |
| 686 |
struct StructWithInt *p = malloc(sizeof(struct StructWithInt)); |
| 687 |
nonSymbolAsFirstArg(&p->g, p); |
| 688 |
return; // no warning |
| 689 |
} |
| 690 |
|
| 691 |
void mallocFailedOrNotLeak(void) { |
| 692 |
int *p = malloc(12); |
| 693 |
if (p == 0) |
| 694 |
return; // no warning |
| 695 |
else |
| 696 |
return; // expected-warning {{Potential leak of memory pointed to by}} |
| 697 |
} |
| 698 |
|
| 699 |
void mallocAssignment(void) { |
| 700 |
char *p = malloc(12); |
| 701 |
p = fooRetPtr(); |
| 702 |
} // expected-warning {{leak}} |
| 703 |
|
| 704 |
int vallocTest(void) { |
| 705 |
char *mem = valloc(12); |
| 706 |
return 0; // expected-warning {{Potential leak of memory pointed to by}} |
| 707 |
} |
| 708 |
|
| 709 |
void vallocEscapeFreeUse(void) { |
| 710 |
int *p = valloc(12); |
| 711 |
myfoo(p); |
| 712 |
free(p); |
| 713 |
myfoo(p); // expected-warning{{Use of memory after it is released}} |
| 714 |
} |
| 715 |
|
| 716 |
int *Gl; |
| 717 |
struct GlStTy { |
| 718 |
int *x; |
| 719 |
}; |
| 720 |
|
| 721 |
struct GlStTy GlS = {0}; |
| 722 |
|
| 723 |
void GlobalFree(void) { |
| 724 |
free(Gl); |
| 725 |
} |
| 726 |
|
| 727 |
void GlobalMalloc(void) { |
| 728 |
Gl = malloc(12); |
| 729 |
} |
| 730 |
|
| 731 |
void GlobalStructMalloc(void) { |
| 732 |
int *a = malloc(12); |
| 733 |
GlS.x = a; |
| 734 |
} |
| 735 |
|
| 736 |
void GlobalStructMallocFree(void) { |
| 737 |
int *a = malloc(12); |
| 738 |
GlS.x = a; |
| 739 |
free(GlS.x); |
| 740 |
} |
| 741 |
|
| 742 |
char *ArrayG[12]; |
| 743 |
|
| 744 |
void globalArrayTest(void) { |
| 745 |
char *p = (char*)malloc(12); |
| 746 |
ArrayG[0] = p; |
| 747 |
} |
| 748 |
|
| 749 |
// Make sure that we properly handle a pointer stored into a local struct/array. |
| 750 |
typedef struct _StructWithPtr { |
| 751 |
int *memP; |
| 752 |
} StructWithPtr; |
| 753 |
|
| 754 |
static StructWithPtr arrOfStructs[10]; |
| 755 |
|
| 756 |
void testMalloc(void) { |
| 757 |
int *x = malloc(12); |
| 758 |
StructWithPtr St; |
| 759 |
St.memP = x; |
| 760 |
arrOfStructs[0] = St; // no-warning |
| 761 |
} |
| 762 |
|
| 763 |
StructWithPtr testMalloc2(void) { |
| 764 |
int *x = malloc(12); |
| 765 |
StructWithPtr St; |
| 766 |
St.memP = x; |
| 767 |
return St; // no-warning |
| 768 |
} |
| 769 |
|
| 770 |
int *testMalloc3(void) { |
| 771 |
int *x = malloc(12); |
| 772 |
int *y = x; |
| 773 |
return y; // no-warning |
| 774 |
} |
| 775 |
|
| 776 |
void testStructLeak(void) { |
| 777 |
StructWithPtr St; |
| 778 |
St.memP = malloc(12); |
| 779 |
return; // expected-warning {{Potential leak of memory pointed to by 'St.memP'}} |
| 780 |
} |
| 781 |
|
| 782 |
void testElemRegion1(void) { |
| 783 |
char *x = (void*)malloc(2); |
| 784 |
int *ix = (int*)x; |
| 785 |
free(&(x[0])); |
| 786 |
} |
| 787 |
|
| 788 |
void testElemRegion2(int **pp) { |
| 789 |
int *p = malloc(12); |
| 790 |
*pp = p; |
| 791 |
free(pp[0]); |
| 792 |
} |
| 793 |
|
| 794 |
void testElemRegion3(int **pp) { |
| 795 |
int *p = malloc(12); |
| 796 |
*pp = p; |
| 797 |
free(*pp); |
| 798 |
} |
| 799 |
// Region escape testing. |
| 800 |
|
| 801 |
unsigned takePtrToPtr(int **p); |
| 802 |
void PassTheAddrOfAllocatedData(int f) { |
| 803 |
int *p = malloc(12); |
| 804 |
// We don't know what happens after the call. Should stop tracking here. |
| 805 |
if (takePtrToPtr(&p)) |
| 806 |
f++; |
| 807 |
free(p); // no warning |
| 808 |
} |
| 809 |
|
| 810 |
struct X { |
| 811 |
int *p; |
| 812 |
}; |
| 813 |
unsigned takePtrToStruct(struct X *s); |
| 814 |
int ** foo2(int *g, int f) { |
| 815 |
int *p = malloc(12); |
| 816 |
struct X *px= malloc(sizeof(struct X)); |
| 817 |
px->p = p; |
| 818 |
// We don't know what happens after this call. Should not track px nor p. |
| 819 |
if (takePtrToStruct(px)) |
| 820 |
f++; |
| 821 |
free(p); |
| 822 |
return 0; |
| 823 |
} |
| 824 |
|
| 825 |
struct X* RegInvalidationDetect1(struct X *s2) { |
| 826 |
struct X *px= malloc(sizeof(struct X)); |
| 827 |
px->p = 0; |
| 828 |
px = s2; |
| 829 |
return px; // expected-warning {{Potential leak of memory pointed to by}} |
| 830 |
} |
| 831 |
|
| 832 |
struct X* RegInvalidationGiveUp1(void) { |
| 833 |
int *p = malloc(12); |
| 834 |
struct X *px= malloc(sizeof(struct X)); |
| 835 |
px->p = p; |
| 836 |
return px; |
| 837 |
} |
| 838 |
|
| 839 |
int **RegInvalidationDetect2(int **pp) { |
| 840 |
int *p = malloc(12); |
| 841 |
pp = &p; |
| 842 |
pp++; |
| 843 |
return 0;// expected-warning {{Potential leak of memory pointed to by}} |
| 844 |
} |
| 845 |
|
| 846 |
extern void exit(int) __attribute__ ((__noreturn__)); |
| 847 |
void mallocExit(int *g) { |
| 848 |
struct xx *p = malloc(12); |
| 849 |
if (g != 0) |
| 850 |
exit(1); |
| 851 |
free(p); |
| 852 |
return; |
| 853 |
} |
| 854 |
|
| 855 |
extern void __assert_fail (__const char *__assertion, __const char *__file, |
| 856 |
unsigned int __line, __const char *__function) |
| 857 |
__attribute__ ((__noreturn__)); |
| 858 |
#define assert(expr) \ |
| 859 |
((expr) ? (void)(0) : __assert_fail (#expr, __FILE__, __LINE__, __func__)) |
| 860 |
void mallocAssert(int *g) { |
| 861 |
struct xx *p = malloc(12); |
| 862 |
|
| 863 |
assert(g != 0); |
| 864 |
free(p); |
| 865 |
return; |
| 866 |
} |
| 867 |
|
| 868 |
void doNotInvalidateWhenPassedToSystemCalls(char *s) { |
| 869 |
char *p = malloc(12); |
| 870 |
strlen(p); |
| 871 |
strcpy(p, s); |
| 872 |
strcpy(s, p); |
| 873 |
strcpy(p, p); |
| 874 |
memcpy(p, s, 1); |
| 875 |
memcpy(s, p, 1); |
| 876 |
memcpy(p, p, 1); |
| 877 |
} // expected-warning {{leak}} |
| 878 |
|
| 879 |
// Treat source buffer contents as escaped. |
| 880 |
void escapeSourceContents(char *s) { |
| 881 |
char *p = malloc(12); |
| 882 |
memcpy(s, &p, 12); // no warning |
| 883 |
|
| 884 |
void *p1 = malloc(7); |
| 885 |
char *a; |
| 886 |
memcpy(&a, &p1, sizeof a); |
| 887 |
// FIXME: No warning due to limitations imposed by current modelling of |
| 888 |
// 'memcpy' (regions metadata is not copied). |
| 889 |
|
| 890 |
int *ptrs[2]; |
| 891 |
int *allocated = (int *)malloc(4); |
| 892 |
memcpy(&ptrs[0], &allocated, sizeof(int *)); |
| 893 |
// FIXME: No warning due to limitations imposed by current modelling of |
| 894 |
// 'memcpy' (regions metadata is not copied). |
| 895 |
} |
| 896 |
|
| 897 |
void invalidateDestinationContents(void) { |
| 898 |
int *null = 0; |
| 899 |
int *p = (int *)malloc(4); |
| 900 |
memcpy(&p, &null, sizeof(int *)); |
| 901 |
|
| 902 |
int *ptrs1[2]; // expected-warning {{Potential leak of memory pointed to by}} |
| 903 |
ptrs1[0] = (int *)malloc(4); |
| 904 |
memcpy(ptrs1, &null, sizeof(int *)); |
| 905 |
|
| 906 |
int *ptrs2[2]; // expected-warning {{Potential memory leak}} |
| 907 |
ptrs2[0] = (int *)malloc(4); |
| 908 |
memcpy(&ptrs2[1], &null, sizeof(int *)); |
| 909 |
|
| 910 |
int *ptrs3[2]; // expected-warning {{Potential memory leak}} |
| 911 |
ptrs3[0] = (int *)malloc(4); |
| 912 |
memcpy(&ptrs3[0], &null, sizeof(int *)); |
| 913 |
} // expected-warning {{Potential memory leak}} |
| 914 |
|
| 915 |
// Rely on the CString checker evaluation of the strcpy API to convey that the result of strcpy is equal to p. |
| 916 |
void symbolLostWithStrcpy(char *s) { |
| 917 |
char *p = malloc(12); |
| 918 |
p = strcpy(p, s); |
| 919 |
free(p); |
| 920 |
} |
| 921 |
|
| 922 |
|
| 923 |
// The same test as the one above, but with what is actually generated on a mac. |
| 924 |
static __inline char * |
| 925 |
__inline_strcpy_chk (char *restrict __dest, const char *restrict __src) |
| 926 |
{ |
| 927 |
return __builtin___strcpy_chk (__dest, __src, __builtin_object_size (__dest, 2 > 1)); |
| 928 |
} |
| 929 |
|
| 930 |
void symbolLostWithStrcpy_InlineStrcpyVersion(char *s) { |
| 931 |
char *p = malloc(12); |
| 932 |
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)); |
| 933 |
free(p); |
| 934 |
} |
| 935 |
|
| 936 |
// Here we are returning a pointer one past the allocated value. An idiom which |
| 937 |
// can be used for implementing special malloc. The correct uses of this might |
| 938 |
// be rare enough so that we could keep this as a warning. |
| 939 |
static void *specialMalloc(int n){ |
| 940 |
int *p; |
| 941 |
p = malloc( n+8 ); |
| 942 |
if( p ){ |
| 943 |
p[0] = n; |
| 944 |
p++; |
| 945 |
} |
| 946 |
return p; |
| 947 |
} |
| 948 |
|
| 949 |
// Potentially, the user could free the struct by performing pointer arithmetic on the return value. |
| 950 |
// This is a variation of the specialMalloc issue, though probably would be more rare in correct code. |
| 951 |
int *specialMallocWithStruct(void) { |
| 952 |
struct StructWithInt *px= malloc(sizeof(struct StructWithInt)); |
| 953 |
return &(px->g); |
| 954 |
} |
| 955 |
|
| 956 |
// Test various allocation/deallocation functions. |
| 957 |
void testStrdup(const char *s, unsigned validIndex) { |
| 958 |
char *s2 = strdup(s); |
| 959 |
s2[validIndex + 1] = 'b'; |
| 960 |
} // expected-warning {{Potential leak of memory pointed to by}} |
| 961 |
|
| 962 |
void testWinStrdup(const char *s, unsigned validIndex) { |
| 963 |
char *s2 = _strdup(s); |
| 964 |
s2[validIndex + 1] = 'b'; |
| 965 |
} // expected-warning {{Potential leak of memory pointed to by}} |
| 966 |
|
| 967 |
void testWcsdup(const wchar_t *s, unsigned validIndex) { |
| 968 |
wchar_t *s2 = wcsdup(s); |
| 969 |
s2[validIndex + 1] = 'b'; |
| 970 |
} // expected-warning {{Potential leak of memory pointed to by}} |
| 971 |
|
| 972 |
void testWinWcsdup(const wchar_t *s, unsigned validIndex) { |
| 973 |
wchar_t *s2 = _wcsdup(s); |
| 974 |
s2[validIndex + 1] = 'b'; |
| 975 |
} // expected-warning {{Potential leak of memory pointed to by}} |
| 976 |
|
| 977 |
int testStrndup(const char *s, unsigned validIndex, unsigned size) { |
| 978 |
char *s2 = strndup(s, size); |
| 979 |
s2 [validIndex + 1] = 'b'; |
| 980 |
if (s2[validIndex] != 'a') |
| 981 |
return 0; |
| 982 |
else |
| 983 |
return 1;// expected-warning {{Potential leak of memory pointed to by}} |
| 984 |
} |
| 985 |
|
| 986 |
void testStrdupContentIsDefined(const char *s, unsigned validIndex) { |
| 987 |
char *s2 = strdup(s); |
| 988 |
char result = s2[1];// no warning |
| 989 |
free(s2); |
| 990 |
} |
| 991 |
|
| 992 |
void testWinStrdupContentIsDefined(const char *s, unsigned validIndex) { |
| 993 |
char *s2 = _strdup(s); |
| 994 |
char result = s2[1];// no warning |
| 995 |
free(s2); |
| 996 |
} |
| 997 |
|
| 998 |
void testWcsdupContentIsDefined(const wchar_t *s, unsigned validIndex) { |
| 999 |
wchar_t *s2 = wcsdup(s); |
| 1000 |
wchar_t result = s2[1];// no warning |
| 1001 |
free(s2); |
| 1002 |
} |
| 1003 |
|
| 1004 |
void testWinWcsdupContentIsDefined(const wchar_t *s, unsigned validIndex) { |
| 1005 |
wchar_t *s2 = _wcsdup(s); |
| 1006 |
wchar_t result = s2[1];// no warning |
| 1007 |
free(s2); |
| 1008 |
} |
| 1009 |
|
| 1010 |
// ---------------------------------------------------------------------------- |
| 1011 |
// Test the system library functions to which the pointer can escape. |
| 1012 |
// This tests false positive suppression. |
| 1013 |
|
| 1014 |
// For now, we assume memory passed to pthread_specific escapes. |
| 1015 |
// TODO: We could check that if a new pthread binding is set, the existing |
| 1016 |
// binding must be freed; otherwise, a memory leak can occur. |
| 1017 |
void testPthereadSpecificEscape(pthread_key_t key) { |
| 1018 |
void *buf = malloc(12); |
| 1019 |
pthread_setspecific(key, buf); // no warning |
| 1020 |
} |
| 1021 |
|
| 1022 |
// PR12101: Test funopen(). |
| 1023 |
static int releasePtr(void *_ctx) { |
| 1024 |
free(_ctx); |
| 1025 |
return 0; |
| 1026 |
} |
| 1027 |
FILE *useFunOpen(void) { |
| 1028 |
void *ctx = malloc(sizeof(int)); |
| 1029 |
FILE *f = funopen(ctx, 0, 0, 0, releasePtr); // no warning |
| 1030 |
if (f == 0) { |
| 1031 |
free(ctx); |
| 1032 |
} |
| 1033 |
return f; |
| 1034 |
} |
| 1035 |
FILE *useFunOpenNoReleaseFunction(void) { |
| 1036 |
void *ctx = malloc(sizeof(int)); |
| 1037 |
FILE *f = funopen(ctx, 0, 0, 0, 0); |
| 1038 |
if (f == 0) { |
| 1039 |
free(ctx); |
| 1040 |
} |
| 1041 |
return f; // expected-warning{{leak}} |
| 1042 |
} |
| 1043 |
|
| 1044 |
static int readNothing(void *_ctx, char *buf, int size) { |
| 1045 |
return 0; |
| 1046 |
} |
| 1047 |
FILE *useFunOpenReadNoRelease(void) { |
| 1048 |
void *ctx = malloc(sizeof(int)); |
| 1049 |
FILE *f = funopen(ctx, readNothing, 0, 0, 0); |
| 1050 |
if (f == 0) { |
| 1051 |
free(ctx); |
| 1052 |
} |
| 1053 |
return f; // expected-warning{{leak}} |
| 1054 |
} |
| 1055 |
|
| 1056 |
// Test setbuf, setvbuf. |
| 1057 |
int my_main_no_warning(void) { |
| 1058 |
char *p = malloc(100); |
| 1059 |
setvbuf(stdout, p, 0, 100); |
| 1060 |
return 0; |
| 1061 |
} |
| 1062 |
int my_main_no_warning2(void) { |
| 1063 |
char *p = malloc(100); |
| 1064 |
setbuf(__stdoutp, p); |
| 1065 |
return 0; |
| 1066 |
} |
| 1067 |
int my_main_warn(FILE *f) { |
| 1068 |
char *p = malloc(100); |
| 1069 |
setvbuf(f, p, 0, 100); |
| 1070 |
return 0;// expected-warning {{leak}} |
| 1071 |
} |
| 1072 |
|
| 1073 |
// some people use stack allocated memory as an optimization to avoid |
| 1074 |
// a heap allocation for small work sizes. This tests the analyzer's |
| 1075 |
// understanding that the malloc'ed memory is not the same as stackBuffer. |
| 1076 |
void radar10978247(int myValueSize) { |
| 1077 |
char stackBuffer[128]; |
| 1078 |
char *buffer; |
| 1079 |
|
| 1080 |
if (myValueSize <= sizeof(stackBuffer)) |
| 1081 |
buffer = stackBuffer; |
| 1082 |
else |
| 1083 |
buffer = malloc(myValueSize); |
| 1084 |
|
| 1085 |
// do stuff with the buffer |
| 1086 |
if (buffer != stackBuffer) |
| 1087 |
free(buffer); |
| 1088 |
} |
| 1089 |
|
| 1090 |
void radar10978247_positive(int myValueSize) { |
| 1091 |
char stackBuffer[128]; |
| 1092 |
char *buffer; |
| 1093 |
|
| 1094 |
if (myValueSize <= sizeof(stackBuffer)) |
| 1095 |
buffer = stackBuffer; |
| 1096 |
else |
| 1097 |
buffer = malloc(myValueSize); |
| 1098 |
|
| 1099 |
// do stuff with the buffer |
| 1100 |
if (buffer == stackBuffer) |
| 1101 |
return; |
| 1102 |
else |
| 1103 |
return; // expected-warning {{leak}} |
| 1104 |
} |
| 1105 |
// Previously this triggered a false positive because 'malloc()' is known to |
| 1106 |
// return uninitialized memory and the binding of 'o' to 'p->n' was not getting |
| 1107 |
// propertly handled. Now we report a leak. |
| 1108 |
struct rdar11269741_a_t { |
| 1109 |
struct rdar11269741_b_t { |
| 1110 |
int m; |
| 1111 |
} n; |
| 1112 |
}; |
| 1113 |
|
| 1114 |
int rdar11269741(struct rdar11269741_b_t o) |
| 1115 |
{ |
| 1116 |
struct rdar11269741_a_t *p = (struct rdar11269741_a_t *) malloc(sizeof(*p)); |
| 1117 |
p->n = o; |
| 1118 |
return p->n.m; // expected-warning {{leak}} |
| 1119 |
} |
| 1120 |
|
| 1121 |
// Pointer arithmetic, returning an ElementRegion. |
| 1122 |
void *radar11329382(unsigned bl) { |
| 1123 |
void *ptr = malloc (16); |
| 1124 |
ptr = ptr + (2 - bl); |
| 1125 |
return ptr; // no warning |
| 1126 |
} |
| 1127 |
|
| 1128 |
void __assert_rtn(const char *, const char *, int, const char *) __attribute__((__noreturn__)); |
| 1129 |
int strcmp(const char *, const char *); |
| 1130 |
char *a (void); |
| 1131 |
void radar11270219(void) { |
| 1132 |
char *x = a(), *y = a(); |
| 1133 |
(__builtin_expect(!(x && y), 0) ? __assert_rtn(__func__, "/Users/zaks/tmp/ex.c", 24, "x && y") : (void)0); |
| 1134 |
strcmp(x, y); // no warning |
| 1135 |
} |
| 1136 |
|
| 1137 |
void radar_11358224_test_double_assign_ints_positive_2(void) |
| 1138 |
{ |
| 1139 |
void *ptr = malloc(16); |
| 1140 |
ptr = ptr; |
| 1141 |
} // expected-warning {{leak}} |
| 1142 |
|
| 1143 |
// Assume that functions which take a function pointer can free memory even if |
| 1144 |
// they are defined in system headers and take the const pointer to the |
| 1145 |
// allocated memory. |
| 1146 |
int const_ptr_and_callback(int, const char*, int n, void(*)(void*)); |
| 1147 |
void r11160612_1(void) { |
| 1148 |
char *x = malloc(12); |
| 1149 |
const_ptr_and_callback(0, x, 12, free); // no - warning |
| 1150 |
} |
| 1151 |
|
| 1152 |
// Null is passed as callback. |
| 1153 |
void r11160612_2(void) { |
| 1154 |
char *x = malloc(12); |
| 1155 |
const_ptr_and_callback(0, x, 12, 0); |
| 1156 |
} // expected-warning {{leak}} |
| 1157 |
|
| 1158 |
// Callback is passed to a function defined in a system header. |
| 1159 |
void r11160612_4(void) { |
| 1160 |
char *x = malloc(12); |
| 1161 |
sqlite3_bind_text_my(0, x, 12, free); // no - warning |
| 1162 |
} |
| 1163 |
|
| 1164 |
// Passing callbacks in a struct. |
| 1165 |
void r11160612_5(StWithCallback St) { |
| 1166 |
void *x = malloc(12); |
| 1167 |
dealocateMemWhenDoneByVal(x, St); |
| 1168 |
} |
| 1169 |
void r11160612_6(StWithCallback St) { |
| 1170 |
void *x = malloc(12); |
| 1171 |
dealocateMemWhenDoneByRef(&St, x); |
| 1172 |
} |
| 1173 |
|
| 1174 |
int mySub(int, int); |
| 1175 |
int myAdd(int, int); |
| 1176 |
int fPtr(unsigned cond, int x) { |
| 1177 |
return (cond ? mySub : myAdd)(x, x); |
| 1178 |
} |
| 1179 |
|
| 1180 |
// Test anti-aliasing. |
| 1181 |
|
| 1182 |
void dependsOnValueOfPtr(int *g, unsigned f) { |
| 1183 |
int *p; |
| 1184 |
|
| 1185 |
if (f) { |
| 1186 |
p = g; |
| 1187 |
} else { |
| 1188 |
p = malloc(12); |
| 1189 |
} |
| 1190 |
|
| 1191 |
if (p != g) |
| 1192 |
free(p); |
| 1193 |
else |
| 1194 |
return; // no warning |
| 1195 |
return; |
| 1196 |
} |
| 1197 |
|
| 1198 |
int CMPRegionHeapToStack(void) { |
| 1199 |
int x = 0; |
| 1200 |
int *x1 = malloc(8); |
| 1201 |
int *x2 = &x; |
| 1202 |
clang_analyzer_eval(x1 == x2); // expected-warning{{FALSE}} |
| 1203 |
free(x1); |
| 1204 |
return x; |
| 1205 |
} |
| 1206 |
|
| 1207 |
int CMPRegionHeapToHeap2(void) { |
| 1208 |
int x = 0; |
| 1209 |
int *x1 = malloc(8); |
| 1210 |
int *x2 = malloc(8); |
| 1211 |
int *x4 = x1; |
| 1212 |
int *x5 = x2; |
| 1213 |
clang_analyzer_eval(x4 == x5); // expected-warning{{FALSE}} |
| 1214 |
free(x1); |
| 1215 |
free(x2); |
| 1216 |
return x; |
| 1217 |
} |
| 1218 |
|
| 1219 |
int CMPRegionHeapToHeap(void) { |
| 1220 |
int x = 0; |
| 1221 |
int *x1 = malloc(8); |
| 1222 |
int *x4 = x1; |
| 1223 |
if (x1 == x4) { |
| 1224 |
free(x1); |
| 1225 |
return 5/x; // expected-warning{{Division by zero}} |
| 1226 |
} |
| 1227 |
return x;// expected-warning{{This statement is never executed}} |
| 1228 |
} |
| 1229 |
|
| 1230 |
int HeapAssignment(void) { |
| 1231 |
int m = 0; |
| 1232 |
int *x = malloc(4); |
| 1233 |
int *y = x; |
| 1234 |
*x = 5; |
| 1235 |
clang_analyzer_eval(*x != *y); // expected-warning{{FALSE}} |
| 1236 |
free(x); |
| 1237 |
return 0; |
| 1238 |
} |
| 1239 |
|
| 1240 |
int *retPtr(void); |
| 1241 |
int *retPtrMightAlias(int *x); |
| 1242 |
int cmpHeapAllocationToUnknown(void) { |
| 1243 |
int zero = 0; |
| 1244 |
int *yBefore = retPtr(); |
| 1245 |
int *m = malloc(8); |
| 1246 |
int *yAfter = retPtrMightAlias(m); |
| 1247 |
clang_analyzer_eval(yBefore == m); // expected-warning{{FALSE}} |
| 1248 |
clang_analyzer_eval(yAfter == m); // expected-warning{{FALSE}} |
| 1249 |
free(m); |
| 1250 |
return 0; |
| 1251 |
} |
| 1252 |
|
| 1253 |
void localArrayTest(void) { |
| 1254 |
char *p = (char*)malloc(12); |
| 1255 |
char *ArrayL[12]; |
| 1256 |
ArrayL[0] = p; |
| 1257 |
} // expected-warning {{leak}} |
| 1258 |
|
| 1259 |
void localStructTest(void) { |
| 1260 |
StructWithPtr St; |
| 1261 |
StructWithPtr *pSt = &St; |
| 1262 |
pSt->memP = malloc(12); |
| 1263 |
} // expected-warning{{Potential leak of memory pointed to by}} |
| 1264 |
|
| 1265 |
#ifdef __INTPTR_TYPE__ |
| 1266 |
// Test double assignment through integers. |
| 1267 |
typedef __INTPTR_TYPE__ intptr_t; |
| 1268 |
typedef unsigned __INTPTR_TYPE__ uintptr_t; |
| 1269 |
|
| 1270 |
static intptr_t glob; |
| 1271 |
void test_double_assign_ints(void) |
| 1272 |
{ |
| 1273 |
void *ptr = malloc (16); // no-warning |
| 1274 |
glob = (intptr_t)(uintptr_t)ptr; |
| 1275 |
} |
| 1276 |
|
| 1277 |
void test_double_assign_ints_positive(void) |
| 1278 |
{ |
| 1279 |
void *ptr = malloc(16); |
| 1280 |
(void*)(intptr_t)(uintptr_t)ptr; // expected-warning {{unused}} |
| 1281 |
} // expected-warning {{leak}} |
| 1282 |
#endif |
| 1283 |
|
| 1284 |
void testCGContextNoLeak(void) |
| 1285 |
{ |
| 1286 |
void *ptr = malloc(16); |
| 1287 |
CGContextRef context = CGBitmapContextCreate(ptr); |
| 1288 |
|
| 1289 |
// Because you can get the data back out like this, even much later, |
| 1290 |
// CGBitmapContextCreate is one of our "stop-tracking" exceptions. |
| 1291 |
free(CGBitmapContextGetData(context)); |
| 1292 |
} |
| 1293 |
|
| 1294 |
void testCGContextLeak(void) |
| 1295 |
{ |
| 1296 |
void *ptr = malloc(16); |
| 1297 |
CGContextRef context = CGBitmapContextCreate(ptr); |
| 1298 |
// However, this time we're just leaking the data, because the context |
| 1299 |
// object doesn't escape and it hasn't been freed in this function. |
| 1300 |
} |
| 1301 |
|
| 1302 |
// Allow xpc context to escape. |
| 1303 |
// TODO: Would be great if we checked that the finalize_connection_context actually releases it. |
| 1304 |
static void finalize_connection_context(void *ctx) { |
| 1305 |
int *context = ctx; |
| 1306 |
free(context); |
| 1307 |
} |
| 1308 |
void foo (xpc_connection_t peer) { |
| 1309 |
int *ctx = calloc(1, sizeof(int)); |
| 1310 |
xpc_connection_set_context(peer, ctx); |
| 1311 |
xpc_connection_set_finalizer_f(peer, finalize_connection_context); |
| 1312 |
xpc_connection_resume(peer); |
| 1313 |
} |
| 1314 |
|
| 1315 |
// Make sure we catch errors when we free in a function which does not allocate memory. |
| 1316 |
void freeButNoMalloc(int *p, int x){ |
| 1317 |
if (x) { |
| 1318 |
free(p); |
| 1319 |
//user forgot a return here. |
| 1320 |
} |
| 1321 |
free(p); // expected-warning {{Attempt to release already released memory}} |
| 1322 |
} |
| 1323 |
|
| 1324 |
struct HasPtr { |
| 1325 |
char *p; |
| 1326 |
}; |
| 1327 |
|
| 1328 |
char* reallocButNoMalloc(struct HasPtr *a, int c, int size) { |
| 1329 |
int *s; |
| 1330 |
char *b = realloc(a->p, size); |
| 1331 |
char *m = realloc(a->p, size); // expected-warning {{Attempt to release already released memory}} |
| 1332 |
// We don't expect a use-after-free for a->P here because the warning above |
| 1333 |
// is a sink. |
| 1334 |
return a->p; // no-warning |
| 1335 |
} |
| 1336 |
|
| 1337 |
// We should not warn in this case since the caller will presumably free a->p in all cases. |
| 1338 |
int reallocButNoMallocPR13674(struct HasPtr *a, int c, int size) { |
| 1339 |
int *s; |
| 1340 |
char *b = realloc(a->p, size); |
| 1341 |
if (b == 0) |
| 1342 |
return -1; |
| 1343 |
a->p = b; |
| 1344 |
return 0; |
| 1345 |
} |
| 1346 |
|
| 1347 |
// Test realloc with no visible malloc. |
| 1348 |
void *test(void *ptr) { |
| 1349 |
void *newPtr = realloc(ptr, 4); |
| 1350 |
if (newPtr == 0) { |
| 1351 |
if (ptr) |
| 1352 |
free(ptr); // no-warning |
| 1353 |
} |
| 1354 |
return newPtr; |
| 1355 |
} |
| 1356 |
|
| 1357 |
|
| 1358 |
char *testLeakWithinReturn(char *str) { |
| 1359 |
return strdup(strdup(str)); // expected-warning{{leak}} |
| 1360 |
} |
| 1361 |
|
| 1362 |
char *testWinLeakWithinReturn(char *str) { |
| 1363 |
return _strdup(_strdup(str)); // expected-warning{{leak}} |
| 1364 |
} |
| 1365 |
|
| 1366 |
wchar_t *testWinWideLeakWithinReturn(wchar_t *str) { |
| 1367 |
return _wcsdup(_wcsdup(str)); // expected-warning{{leak}} |
| 1368 |
} |
| 1369 |
|
| 1370 |
void passConstPtr(const char * ptr); |
| 1371 |
|
| 1372 |
void testPassConstPointer(void) { |
| 1373 |
char * string = malloc(sizeof(char)*10); |
| 1374 |
passConstPtr(string); |
| 1375 |
return; // expected-warning {{leak}} |
| 1376 |
} |
| 1377 |
|
| 1378 |
void testPassConstPointerIndirectly(void) { |
| 1379 |
char *p = malloc(1); |
| 1380 |
p++; |
| 1381 |
memcmp(p, p, sizeof(&p)); |
| 1382 |
return; // expected-warning {{leak}} |
| 1383 |
} |
| 1384 |
|
| 1385 |
void testPassConstPointerIndirectlyStruct(void) { |
| 1386 |
struct HasPtr hp; |
| 1387 |
hp.p = malloc(10); |
| 1388 |
memcmp(&hp, &hp, sizeof(hp)); |
| 1389 |
return; // expected-warning {{Potential leak of memory pointed to by 'hp.p'}} |
| 1390 |
} |
| 1391 |
|
| 1392 |
void testPassToSystemHeaderFunctionIndirectlyStruct(void) { |
| 1393 |
SomeStruct ss; |
| 1394 |
ss.p = malloc(1); |
| 1395 |
fakeSystemHeaderCall(&ss); // invalidates ss, making ss.p unreachable |
| 1396 |
// Technically a false negative here -- we know the system function won't free |
| 1397 |
// ss.p, but nothing else will either! |
| 1398 |
} // no-warning |
| 1399 |
|
| 1400 |
void testPassToSystemHeaderFunctionIndirectlyStructFree(void) { |
| 1401 |
SomeStruct ss; |
| 1402 |
ss.p = malloc(1); |
| 1403 |
fakeSystemHeaderCall(&ss); // invalidates ss, making ss.p unreachable |
| 1404 |
free(ss.p); |
| 1405 |
} // no-warning |
| 1406 |
|
| 1407 |
void testPassToSystemHeaderFunctionIndirectlyArray(void) { |
| 1408 |
int *p[1]; |
| 1409 |
p[0] = malloc(sizeof(int)); |
| 1410 |
fakeSystemHeaderCallIntPtr(p); // invalidates p, making p[0] unreachable |
| 1411 |
// Technically a false negative here -- we know the system function won't free |
| 1412 |
// p[0], but nothing else will either! |
| 1413 |
} // no-warning |
| 1414 |
|
| 1415 |
void testPassToSystemHeaderFunctionIndirectlyArrayFree(void) { |
| 1416 |
int *p[1]; |
| 1417 |
p[0] = malloc(sizeof(int)); |
| 1418 |
fakeSystemHeaderCallIntPtr(p); // invalidates p, making p[0] unreachable |
| 1419 |
free(p[0]); |
| 1420 |
} // no-warning |
| 1421 |
|
| 1422 |
int *testOffsetAllocate(size_t size) { |
| 1423 |
int *memoryBlock = (int *)malloc(size + sizeof(int)); |
| 1424 |
return &memoryBlock[1]; // no-warning |
| 1425 |
} |
| 1426 |
|
| 1427 |
void testOffsetDeallocate(int *memoryBlock) { |
| 1428 |
free(&memoryBlock[-1]); // no-warning |
| 1429 |
} |
| 1430 |
|
| 1431 |
void testOffsetOfRegionFreed(void) { |
| 1432 |
__int64_t * array = malloc(sizeof(__int64_t)*2); |
| 1433 |
array += 1; |
| 1434 |
free(&array[0]); // expected-warning{{Argument to 'free()' is offset by 8 bytes from the start of memory allocated by 'malloc()'}} |
| 1435 |
} |
| 1436 |
|
| 1437 |
void testOffsetOfRegionFreed2(void) { |
| 1438 |
__int64_t *p = malloc(sizeof(__int64_t)*2); |
| 1439 |
p += 1; |
| 1440 |
free(p); // expected-warning{{Argument to 'free()' is offset by 8 bytes from the start of memory allocated by 'malloc()'}} |
| 1441 |
} |
| 1442 |
|
| 1443 |
void testOffsetOfRegionFreed3(void) { |
| 1444 |
char *r = malloc(sizeof(char)); |
| 1445 |
r = r - 10; |
| 1446 |
free(r); // expected-warning {{Argument to 'free()' is offset by -10 bytes from the start of memory allocated by 'malloc()'}} |
| 1447 |
} |
| 1448 |
|
| 1449 |
void testOffsetOfRegionFreedAfterFunctionCall(void) { |
| 1450 |
int *p = malloc(sizeof(int)*2); |
| 1451 |
p += 1; |
| 1452 |
myfoo(p); |
| 1453 |
free(p); // expected-warning{{Argument to 'free()' is offset by 4 bytes from the start of memory allocated by 'malloc()'}} |
| 1454 |
} |
| 1455 |
|
| 1456 |
void testFixManipulatedPointerBeforeFree(void) { |
| 1457 |
int * array = malloc(sizeof(int)*2); |
| 1458 |
array += 1; |
| 1459 |
free(&array[-1]); // no-warning |
| 1460 |
} |
| 1461 |
|
| 1462 |
void testFixManipulatedPointerBeforeFree2(void) { |
| 1463 |
char *r = malloc(sizeof(char)); |
| 1464 |
r = r + 10; |
| 1465 |
free(r-10); // no-warning |
| 1466 |
} |
| 1467 |
|
| 1468 |
void freeOffsetPointerPassedToFunction(void) { |
| 1469 |
__int64_t *p = malloc(sizeof(__int64_t)*2); |
| 1470 |
p[1] = 0; |
| 1471 |
p += 1; |
| 1472 |
myfooint(*p); // not passing the pointer, only a value pointed by pointer |
| 1473 |
free(p); // expected-warning {{Argument to 'free()' is offset by 8 bytes from the start of memory allocated by 'malloc()'}} |
| 1474 |
} |
| 1475 |
|
| 1476 |
int arbitraryInt(void); |
| 1477 |
void freeUnknownOffsetPointer(void) { |
| 1478 |
char *r = malloc(sizeof(char)); |
| 1479 |
r = r + arbitraryInt(); // unable to reason about what the offset might be |
| 1480 |
free(r); // no-warning |
| 1481 |
} |
| 1482 |
|
| 1483 |
void testFreeNonMallocPointerWithNoOffset(void) { |
| 1484 |
char c; |
| 1485 |
char *r = &c; |
| 1486 |
r = r + 10; |
| 1487 |
free(r-10); // expected-warning {{Argument to 'free()' is the address of the local variable 'c', which is not memory allocated by 'malloc()'}} |
| 1488 |
} |
| 1489 |
|
| 1490 |
void testFreeNonMallocPointerWithOffset(void) { |
| 1491 |
char c; |
| 1492 |
char *r = &c; |
| 1493 |
free(r+1); // expected-warning {{Argument to 'free()' is the address of the local variable 'c', which is not memory allocated by 'malloc()'}} |
| 1494 |
} |
| 1495 |
|
| 1496 |
void testOffsetZeroDoubleFree(void) { |
| 1497 |
int *array = malloc(sizeof(int)*2); |
| 1498 |
int *p = &array[0]; |
| 1499 |
free(p); |
| 1500 |
free(&array[0]); // expected-warning{{Attempt to release already released memory}} |
| 1501 |
} |
| 1502 |
|
| 1503 |
void testOffsetPassedToStrlen(void) { |
| 1504 |
char * string = malloc(sizeof(char)*10); |
| 1505 |
string += 1; |
| 1506 |
int length = strlen(string); // expected-warning {{Potential leak of memory pointed to by 'string'}} |
| 1507 |
} |
| 1508 |
|
| 1509 |
void testOffsetPassedToStrlenThenFree(void) { |
| 1510 |
char * string = malloc(sizeof(char)*10); |
| 1511 |
string += 1; |
| 1512 |
int length = strlen(string); |
| 1513 |
free(string); // expected-warning {{Argument to 'free()' is offset by 1 byte from the start of memory allocated by 'malloc()'}} |
| 1514 |
} |
| 1515 |
|
| 1516 |
void testOffsetPassedAsConst(void) { |
| 1517 |
char * string = malloc(sizeof(char)*10); |
| 1518 |
string += 1; |
| 1519 |
passConstPtr(string); |
| 1520 |
free(string); // expected-warning {{Argument to 'free()' is offset by 1 byte from the start of memory allocated by 'malloc()'}} |
| 1521 |
} |
| 1522 |
|
| 1523 |
char **_vectorSegments; |
| 1524 |
int _nVectorSegments; |
| 1525 |
|
| 1526 |
void poolFreeC(void* s) { |
| 1527 |
free(s); // no-warning |
| 1528 |
} |
| 1529 |
void freeMemory(void) { |
| 1530 |
while (_nVectorSegments) { |
| 1531 |
poolFreeC(_vectorSegments[_nVectorSegments++]); |
| 1532 |
} |
| 1533 |
} |
| 1534 |
|
| 1535 |
// PR16730 |
| 1536 |
void testReallocEscaped(void **memory) { |
| 1537 |
*memory = malloc(47); |
| 1538 |
char *new_memory = realloc(*memory, 47); |
| 1539 |
if (new_memory != 0) { |
| 1540 |
*memory = new_memory; |
| 1541 |
} |
| 1542 |
} |
| 1543 |
|
| 1544 |
// PR16558 |
| 1545 |
void *smallocNoWarn(size_t size) { |
| 1546 |
if (size == 0) { |
| 1547 |
return malloc(1); // this branch is never called |
| 1548 |
} |
| 1549 |
else { |
| 1550 |
return malloc(size); |
| 1551 |
} |
| 1552 |
} |
| 1553 |
|
| 1554 |
char *dupstrNoWarn(const char *s) { |
| 1555 |
const int len = strlen(s); |
| 1556 |
char *p = (char*) smallocNoWarn(len + 1); |
| 1557 |
strcpy(p, s); // no-warning |
| 1558 |
return p; |
| 1559 |
} |
| 1560 |
|
| 1561 |
void *smallocWarn(size_t size) { |
| 1562 |
if (size == 2) { |
| 1563 |
return malloc(1); |
| 1564 |
} |
| 1565 |
else { |
| 1566 |
return malloc(size); |
| 1567 |
} |
| 1568 |
} |
| 1569 |
|
| 1570 |
int *radar15580979(void) { |
| 1571 |
int *data = (int *)malloc(32); |
| 1572 |
int *p = data ?: (int*)malloc(32); // no warning |
| 1573 |
return p; |
| 1574 |
} |
| 1575 |
|
| 1576 |
// Some data structures may hold onto the pointer and free it later. |
| 1577 |
void testEscapeThroughSystemCallTakingVoidPointer1(void *queue) { |
| 1578 |
int *data = (int *)malloc(32); |
| 1579 |
fake_insque(queue, data); // no warning |
| 1580 |
} |
| 1581 |
|
| 1582 |
void testEscapeThroughSystemCallTakingVoidPointer2(fake_rb_tree_t *rbt) { |
| 1583 |
int *data = (int *)malloc(32); |
| 1584 |
fake_rb_tree_init(rbt, data); |
| 1585 |
} //expected-warning{{Potential leak}} |
| 1586 |
|
| 1587 |
void testEscapeThroughSystemCallTakingVoidPointer3(fake_rb_tree_t *rbt) { |
| 1588 |
int *data = (int *)malloc(32); |
| 1589 |
fake_rb_tree_init(rbt, data); |
| 1590 |
fake_rb_tree_insert_node(rbt, data); // no warning |
| 1591 |
} |
| 1592 |
|
| 1593 |
struct IntAndPtr { |
| 1594 |
int x; |
| 1595 |
int *p; |
| 1596 |
}; |
| 1597 |
|
| 1598 |
void constEscape(const void *ptr); |
| 1599 |
|
| 1600 |
void testConstEscapeThroughAnotherField(void) { |
| 1601 |
struct IntAndPtr s; |
| 1602 |
s.p = malloc(sizeof(int)); |
| 1603 |
constEscape(&(s.x)); // could free s->p! |
| 1604 |
} // no-warning |
| 1605 |
|
| 1606 |
// PR15623 |
| 1607 |
int testNoCheckerDataPropagationFromLogicalOpOperandToOpResult(void) { |
| 1608 |
char *param = malloc(10); |
| 1609 |
char *value = malloc(10); |
| 1610 |
int ok = (param && value); |
| 1611 |
free(param); |
| 1612 |
free(value); |
| 1613 |
// Previously we ended up with 'Use of memory after it is released' on return. |
| 1614 |
return ok; // no warning |
| 1615 |
} |
| 1616 |
|
| 1617 |
void (*fnptr)(int); |
| 1618 |
void freeIndirectFunctionPtr(void) { |
| 1619 |
void *p = (void *)fnptr; |
| 1620 |
free(p); // expected-warning {{Argument to 'free()' is a function pointer}} |
| 1621 |
} |
| 1622 |
|
| 1623 |
void freeFunctionPtr(void) { |
| 1624 |
free((void *)fnptr); |
| 1625 |
// expected-warning@-1{{Argument to 'free()' is a function pointer}} |
| 1626 |
// expected-warning@-2{{attempt to call free on non-heap object '(void *)fnptr'}} |
| 1627 |
} |
| 1628 |
|
| 1629 |
void allocateSomeMemory(void *offendingParameter, void **ptr) { |
| 1630 |
*ptr = malloc(1); |
| 1631 |
} |
| 1632 |
|
| 1633 |
void testNoCrashOnOffendingParameter(void) { |
| 1634 |
// "extern" is necessary to avoid unrelated warnings |
| 1635 |
// on passing uninitialized value. |
| 1636 |
extern void *offendingParameter; |
| 1637 |
void* ptr; |
| 1638 |
allocateSomeMemory(offendingParameter, &ptr); |
| 1639 |
} // expected-warning {{Potential leak of memory pointed to by 'ptr'}} |
| 1640 |
|
| 1641 |
|
| 1642 |
// Test a false positive caused by a bug in liveness analysis. |
| 1643 |
struct A { |
| 1644 |
int *buf; |
| 1645 |
}; |
| 1646 |
struct B { |
| 1647 |
struct A *a; |
| 1648 |
}; |
| 1649 |
void livenessBugRealloc(struct A *a) { |
| 1650 |
a->buf = realloc(a->buf, sizeof(int)); // no-warning |
| 1651 |
} |
| 1652 |
void testLivenessBug(struct B *in_b) { |
| 1653 |
struct B *b = in_b; |
| 1654 |
livenessBugRealloc(b->a); |
| 1655 |
((void) 0); // An attempt to trick liveness analysis. |
| 1656 |
livenessBugRealloc(b->a); |
| 1657 |
} |
| 1658 |
|
| 1659 |
struct ListInfo { |
| 1660 |
struct ListInfo *next; |
| 1661 |
}; |
| 1662 |
|
| 1663 |
struct ConcreteListItem { |
| 1664 |
struct ListInfo li; |
| 1665 |
int i; |
| 1666 |
}; |
| 1667 |
|
| 1668 |
void list_add(struct ListInfo *list, struct ListInfo *item); |
| 1669 |
|
| 1670 |
void testCStyleListItems(struct ListInfo *list) { |
| 1671 |
struct ConcreteListItem *x = malloc(sizeof(struct ConcreteListItem)); |
| 1672 |
list_add(list, &x->li); // will free 'x'. |
| 1673 |
} |
| 1674 |
|
| 1675 |
// MEM34-C. Only free memory allocated dynamically |
| 1676 |
// Second non-compliant example. |
| 1677 |
// https://wiki.sei.cmu.edu/confluence/display/c/MEM34-C.+Only+free+memory+allocated+dynamically |
| 1678 |
enum { BUFSIZE = 256 }; |
| 1679 |
|
| 1680 |
void MEM34_C(void) { |
| 1681 |
char buf[BUFSIZE]; |
| 1682 |
char *p = (char *)realloc(buf, 2 * BUFSIZE); |
| 1683 |
// expected-warning@-1{{Argument to 'realloc()' is the address of the local \ |
| 1684 |
variable 'buf', which is not memory allocated by 'malloc()' [unix.Malloc]}} |
| 1685 |
if (p == NULL) { |
| 1686 |
/* Handle error */ |
| 1687 |
} |
| 1688 |
} |
| 1689 |
|
| 1690 |
(*crash_a)(); // expected-warning{{type specifier missing}} |
| 1691 |
// A CallEvent without a corresponding FunctionDecl. |
| 1692 |
crash_b() { crash_a(); return 0; } // no-crash |
| 1693 |
// expected-warning@-1{{type specifier missing}} |
| 1694 |
|
| 1695 |
long *global_a; |
| 1696 |
void realloc_crash(void) { |
| 1697 |
long *c = global_a; |
| 1698 |
c--; |
| 1699 |
realloc(c, 8); // no-crash |
| 1700 |
} // expected-warning{{Potential memory leak [unix.Malloc]}} |
| 1701 |
|
| 1702 |
// ---------------------------------------------------------------------------- |
| 1703 |
// False negatives. |
| 1704 |
|
| 1705 |
void testMallocWithParam(int **p) { |
| 1706 |
*p = (int*) malloc(sizeof(int)); |
| 1707 |
*p = 0; // FIXME: should warn here |
| 1708 |
} |
| 1709 |
|
| 1710 |
void testMallocWithParam_2(int **p) { |
| 1711 |
*p = (int*) malloc(sizeof(int)); // no-warning |
| 1712 |
} |
| 1713 |
|
| 1714 |
void testPassToSystemHeaderFunctionIndirectly(void) { |
| 1715 |
int *p = malloc(4); |
| 1716 |
p++; |
| 1717 |
fakeSystemHeaderCallInt(p); |
| 1718 |
// FIXME: This is a leak: if we think a system function won't free p, it |
| 1719 |
// won't free (p-1) either. |
| 1720 |
} |
| 1721 |
|
| 1722 |
void testMallocIntoMalloc(void) { |
| 1723 |
StructWithPtr *s = malloc(sizeof(StructWithPtr)); |
| 1724 |
s->memP = malloc(sizeof(int)); |
| 1725 |
free(s); |
| 1726 |
} // FIXME: should warn here |
| 1727 |
|
| 1728 |
int conjure(void); |
| 1729 |
void testExtent(void) { |
| 1730 |
int x = conjure(); |
| 1731 |
clang_analyzer_dump(x); |
| 1732 |
// expected-warning-re@-1 {{{{^conj_\$[[:digit:]]+{int, LC[[:digit:]]+, S[[:digit:]]+, #1}}}}}} |
| 1733 |
int *p = (int *)malloc(x); |
| 1734 |
clang_analyzer_dumpExtent(p); |
| 1735 |
// expected-warning-re@-1 {{{{^conj_\$[[:digit:]]+{int, LC[[:digit:]]+, S[[:digit:]]+, #1}}}}}} |
| 1736 |
free(p); |
| 1737 |
} |
| 1738 |
|
| 1739 |
void gh149754(void *p) { |
| 1740 |
// This testcase demonstrates an unusual situation where a certain symbol |
| 1741 |
// (the value of `p`) is released (more precisely, transitions from |
| 1742 |
// untracked state to Released state) twice within the same bug path because |
| 1743 |
// the `EvalAssume` callback resets it to untracked state after the first |
| 1744 |
// time when it is released. This caused the failure of an assertion, which |
| 1745 |
// was since then removed for the codebase. |
| 1746 |
if (!realloc(p, 8)) { |
| 1747 |
realloc(p, 8); |
| 1748 |
free(p); // expected-warning {{Attempt to release already released memory}} |
| 1749 |
} |
| 1750 |
// expected-warning@+1 {{Potential memory leak}} |
| 1751 |
} |