| 1 |
#include <stdlib.h> |
| 2 |
|
| 3 |
extern int foo (void); |
| 4 |
extern int bar (void); |
| 5 |
extern void could_free (void *); |
| 6 |
extern void cant_free (const void *); /* since it's a const void *. */ |
| 7 |
|
| 8 |
void test_1 (void) |
| 9 |
{ |
| 10 |
void *ptr = malloc (1024); |
| 11 |
free (ptr); |
| 12 |
free (ptr); /* { dg-warning "double-'free' of 'ptr'" } */ |
| 13 |
} |
| 14 |
|
| 15 |
void test_2 (void *ptr) |
| 16 |
{ |
| 17 |
free (ptr); |
| 18 |
free (ptr); /* { dg-warning "double-'free' of 'ptr'" } */ |
| 19 |
} |
| 20 |
|
| 21 |
void test_2a (void *ptr) |
| 22 |
{ |
| 23 |
__builtin_free (ptr); |
| 24 |
__builtin_free (ptr); /* { dg-warning "double-'free' of 'ptr'" } */ |
| 25 |
} |
| 26 |
|
| 27 |
int *test_3 (void) |
| 28 |
{ |
| 29 |
int *ptr = (int *)malloc (sizeof (int)); |
| 30 |
*ptr = 42; /* { dg-warning "dereference of possibly-NULL 'ptr' \\\[CWE-690\\\]" } */ |
| 31 |
return ptr; |
| 32 |
} |
| 33 |
|
| 34 |
int *test_3a (void) |
| 35 |
{ |
| 36 |
int *ptr = (int *)__builtin_malloc (sizeof (int)); |
| 37 |
*ptr = 42; /* { dg-warning "dereference of possibly-NULL 'ptr' \\\[CWE-690\\\]" } */ |
| 38 |
return ptr; |
| 39 |
} |
| 40 |
|
| 41 |
int *test_4 (void) |
| 42 |
{ |
| 43 |
int *ptr = (int *)malloc (sizeof (int)); |
| 44 |
if (ptr) |
| 45 |
*ptr = 42; |
| 46 |
else |
| 47 |
*ptr = 43; /* { dg-warning "dereference of NULL 'ptr' \\\[CWE-476\\\]" } */ |
| 48 |
return ptr; |
| 49 |
} |
| 50 |
|
| 51 |
int test_5 (int *ptr) |
| 52 |
{ |
| 53 |
free (ptr); |
| 54 |
return *ptr; /* { dg-warning "use after 'free' of 'ptr'" } */ |
| 55 |
} |
| 56 |
|
| 57 |
void test_6 (void *ptr) |
| 58 |
{ |
| 59 |
void *q; |
| 60 |
q = ptr; |
| 61 |
free (ptr); |
| 62 |
free (q); /* { dg-warning "double-'free' of 'q'" } */ |
| 63 |
/* The above case requires us to handle equivalence classes in |
| 64 |
state transitions. */ |
| 65 |
} |
| 66 |
|
| 67 |
void test_7 (void) |
| 68 |
{ |
| 69 |
void *ptr = malloc(4096); |
| 70 |
if (!ptr) |
| 71 |
return; |
| 72 |
__builtin_memset(ptr, 0, 4096); |
| 73 |
free(ptr); |
| 74 |
} |
| 75 |
|
| 76 |
void *test_8 (void) |
| 77 |
{ |
| 78 |
void *ptr = malloc(4096); |
| 79 |
if (!ptr) |
| 80 |
return NULL; |
| 81 |
__builtin_memset(ptr, 0, 4096); |
| 82 |
return ptr; |
| 83 |
/* This needs phi nodes to affect equivalence classes, or we get a false report |
| 84 |
of a leak. */ |
| 85 |
} |
| 86 |
|
| 87 |
void test_9 (void) |
| 88 |
{ |
| 89 |
void *ptr = malloc (1024); |
| 90 |
|
| 91 |
int i; |
| 92 |
for (i = 0; i < 1024; i++) |
| 93 |
free (ptr); /* { dg-warning "double-'free' of 'ptr'" } */ |
| 94 |
} |
| 95 |
|
| 96 |
void test_10 (void) |
| 97 |
{ |
| 98 |
void *ptr = malloc (1024); |
| 99 |
|
| 100 |
int i; |
| 101 |
for (i = 0; i < 1024; i++) |
| 102 |
foo (); |
| 103 |
|
| 104 |
free (ptr); |
| 105 |
free (ptr); /* { dg-warning "double-'free' of 'ptr'" } */ |
| 106 |
} |
| 107 |
|
| 108 |
void test_11 (void) |
| 109 |
{ |
| 110 |
void *ptr = malloc (1024); |
| 111 |
|
| 112 |
while (foo ()) |
| 113 |
bar (); |
| 114 |
|
| 115 |
free (ptr); |
| 116 |
free (ptr); /* { dg-warning "double-'free' of 'ptr'" } */ |
| 117 |
} |
| 118 |
|
| 119 |
void test_12 (void) |
| 120 |
{ |
| 121 |
void *ptr = malloc (1024); |
| 122 |
|
| 123 |
while (1) |
| 124 |
{ |
| 125 |
free (ptr); /* { dg-warning "infinite loop" } */ |
| 126 |
free (ptr); /* { dg-warning "double-'free' of 'ptr'" } */ |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
void test_13 (void) |
| 131 |
{ |
| 132 |
void *p = malloc (1024); /* { dg-message "allocated here" } */ |
| 133 |
void *q = malloc (1024); |
| 134 |
|
| 135 |
foo (); |
| 136 |
if (!q) |
| 137 |
{ |
| 138 |
free (q); |
| 139 |
return; /* { dg-warning "leak of 'p'" } */ |
| 140 |
} |
| 141 |
bar (); |
| 142 |
free (q); |
| 143 |
free (p); |
| 144 |
} |
| 145 |
|
| 146 |
void test_14 (void) |
| 147 |
{ |
| 148 |
void *p, *q; |
| 149 |
p = malloc (1024); |
| 150 |
if (!p) |
| 151 |
return; |
| 152 |
|
| 153 |
q = malloc (1024); |
| 154 |
if (!q) |
| 155 |
{ |
| 156 |
free (p); |
| 157 |
free (q); |
| 158 |
/* oops: missing "return". */ |
| 159 |
} |
| 160 |
bar (); |
| 161 |
free (q); /* Although this looks like a double-'free' of q, |
| 162 |
it's known to be NULL for the case where free is |
| 163 |
called twice on it. */ |
| 164 |
free (p); /* { dg-warning "double-'free' of 'p'" } */ |
| 165 |
} |
| 166 |
|
| 167 |
void test_15 (void) |
| 168 |
{ |
| 169 |
void *p = NULL, *q = NULL; |
| 170 |
|
| 171 |
p = malloc (1024); |
| 172 |
if (!p) |
| 173 |
goto fail; |
| 174 |
|
| 175 |
foo (); |
| 176 |
|
| 177 |
q = malloc (1024); |
| 178 |
if (!q) |
| 179 |
goto fail; |
| 180 |
|
| 181 |
bar (); |
| 182 |
|
| 183 |
fail: |
| 184 |
free (q); |
| 185 |
free (p); |
| 186 |
} |
| 187 |
|
| 188 |
void test_16 (void) |
| 189 |
{ |
| 190 |
void *p, *q; /* { dg-message "region created on stack here" } */ |
| 191 |
|
| 192 |
p = malloc (1024); |
| 193 |
if (!p) |
| 194 |
goto fail; |
| 195 |
|
| 196 |
foo (); |
| 197 |
|
| 198 |
q = malloc (1024); |
| 199 |
if (!q) |
| 200 |
goto fail; |
| 201 |
|
| 202 |
bar (); |
| 203 |
|
| 204 |
fail: |
| 205 |
free (q); /* { dg-warning "use of uninitialized value 'q'" } */ |
| 206 |
free (p); |
| 207 |
} |
| 208 |
|
| 209 |
void test_17 (void) |
| 210 |
{ |
| 211 |
void *ptr = malloc (1024); /* { dg-message "allocated here" } */ |
| 212 |
} /* { dg-warning "leak of 'ptr'" } */ |
| 213 |
|
| 214 |
void test_18 (void) |
| 215 |
{ |
| 216 |
void *ptr = malloc (64); /* { dg-message "allocated here" } */ |
| 217 |
ptr = NULL; /* { dg-warning "leak of 'ptr'" } */ |
| 218 |
} |
| 219 |
|
| 220 |
void test_19 (void) |
| 221 |
{ |
| 222 |
void *ptr = malloc (64); |
| 223 |
free (ptr); |
| 224 |
ptr = NULL; |
| 225 |
free (ptr); |
| 226 |
} |
| 227 |
|
| 228 |
void *global_ptr_20; |
| 229 |
|
| 230 |
void test_20 (void) |
| 231 |
{ |
| 232 |
global_ptr_20 = malloc (1024); |
| 233 |
} |
| 234 |
|
| 235 |
int *test_21 (int i) |
| 236 |
{ |
| 237 |
int *ptr = malloc (sizeof (int)); |
| 238 |
if (!ptr) |
| 239 |
abort (); |
| 240 |
*ptr = i; |
| 241 |
return ptr; |
| 242 |
} |
| 243 |
|
| 244 |
void test_22 (void) |
| 245 |
{ |
| 246 |
void *ptr = malloc (1024); |
| 247 |
|
| 248 |
int i; |
| 249 |
for (i = 5; i < 10; i++) |
| 250 |
foo (); |
| 251 |
|
| 252 |
free (ptr); |
| 253 |
free (ptr); /* { dg-warning "double-'free' of 'ptr'" } */ |
| 254 |
} |
| 255 |
|
| 256 |
int *test_23 (int n) |
| 257 |
{ |
| 258 |
int *ptr = (int *)calloc (n, sizeof (int)); |
| 259 |
ptr[0] = 42; /* { dg-warning "dereference of possibly-NULL 'ptr' \\\[CWE-690\\\]" } */ |
| 260 |
return ptr; |
| 261 |
} |
| 262 |
|
| 263 |
int *test_23a (int n) |
| 264 |
{ |
| 265 |
int *ptr = (int *)__builtin_calloc (n, sizeof (int)); |
| 266 |
ptr[0] = 42; /* { dg-warning "dereference of possibly-NULL 'ptr' \\\[CWE-690\\\]" } */ |
| 267 |
return ptr; |
| 268 |
} |
| 269 |
|
| 270 |
int test_24 (void) |
| 271 |
{ |
| 272 |
void *ptr = __builtin_alloca (sizeof (int)); /* { dg-message "region created on stack here" } */ |
| 273 |
free (ptr); /* { dg-warning "'free' of 'ptr' which points to memory on the stack \\\[CWE-590\\\]" } */ |
| 274 |
} |
| 275 |
|
| 276 |
int test_25 (void) |
| 277 |
{ |
| 278 |
char tmp[100]; /* { dg-message "region created on stack here" } */ |
| 279 |
void *p = tmp; |
| 280 |
free (p); /* { dg-warning "'free' of 'p' which points to memory on the stack \\\[CWE-590\\\]" } */ |
| 281 |
/* TODO: more precise messages here. */ |
| 282 |
} |
| 283 |
|
| 284 |
char global_buffer[100]; /* { dg-message "region created here" } */ |
| 285 |
|
| 286 |
int test_26 (void) |
| 287 |
{ |
| 288 |
void *p = global_buffer; |
| 289 |
free (p); /* { dg-warning "'free' of 'p' which points to memory not on the heap \\\[CWE-590\\\]" } */ |
| 290 |
/* TODO: more precise messages here. */ |
| 291 |
} |
| 292 |
|
| 293 |
struct coord { |
| 294 |
float x; |
| 295 |
float y; |
| 296 |
}; |
| 297 |
|
| 298 |
struct coord *test_27 (void) |
| 299 |
{ |
| 300 |
struct coord *p = (struct coord *) malloc (sizeof (struct coord)); /* { dg-message "this call could return NULL" } */ |
| 301 |
p->x = 0.f; /* { dg-warning "dereference of possibly-NULL 'p' \\\[CWE-690\\\]" } */ |
| 302 |
|
| 303 |
/* Only the first such usage should be reported: */ |
| 304 |
p->y = 0.f; |
| 305 |
|
| 306 |
return p; |
| 307 |
} |
| 308 |
|
| 309 |
struct coord *test_28 (void) |
| 310 |
{ |
| 311 |
struct coord *p = NULL; |
| 312 |
p->x = 0.f; /* { dg-warning "dereference of NULL 'p' \\\[CWE-476\\\]" } */ |
| 313 |
|
| 314 |
/* Only the first such usage should be reported: */ |
| 315 |
p->y = 0.f; |
| 316 |
|
| 317 |
return p; |
| 318 |
} |
| 319 |
|
| 320 |
struct link |
| 321 |
{ |
| 322 |
struct link *m_ptr; |
| 323 |
}; |
| 324 |
|
| 325 |
struct link *test_29 (void) |
| 326 |
{ |
| 327 |
struct link *res = (struct link *)malloc (sizeof (struct link)); |
| 328 |
if (!res) |
| 329 |
return NULL; |
| 330 |
res->m_ptr = (struct link *)malloc (sizeof (struct link)); |
| 331 |
return res; |
| 332 |
} |
| 333 |
|
| 334 |
struct link *test_29a (void) |
| 335 |
{ |
| 336 |
struct link *res = (struct link *)malloc (sizeof (struct link)); |
| 337 |
if (!res) |
| 338 |
return NULL; |
| 339 |
res->m_ptr = (struct link *)malloc (sizeof (struct link)); |
| 340 |
if (!res->m_ptr) |
| 341 |
{ |
| 342 |
free (res); |
| 343 |
return NULL; |
| 344 |
} |
| 345 |
res->m_ptr->m_ptr = (struct link *)malloc (sizeof (struct link)); |
| 346 |
return res; |
| 347 |
} |
| 348 |
|
| 349 |
/* Without consolidation by EC, this one shows two leaks: |
| 350 |
warning: leak of '<unknown>' |
| 351 |
warning: leak of 'tmp.m_ptr' |
| 352 |
We should only show the latter (favoring the most user-readable |
| 353 |
expression in the equivalence class). */ |
| 354 |
void test_30 (void) |
| 355 |
{ |
| 356 |
struct link tmp; |
| 357 |
tmp.m_ptr = (struct link *)malloc (sizeof (struct link)); /* { dg-message "allocated here" } */ |
| 358 |
} /* { dg-warning "leak of 'tmp.m_ptr'" } */ |
| 359 |
/* { dg-bogus "leak of '<unknown>'" "leak of unknown" { target *-*-* } .-1 } */ |
| 360 |
|
| 361 |
void test_31 (void) |
| 362 |
{ |
| 363 |
struct link tmp; |
| 364 |
void *ptr = malloc (sizeof (struct link)); /* { dg-message "allocated here" } */ |
| 365 |
tmp.m_ptr = (struct link *)ptr; |
| 366 |
} /* { dg-warning "leak of 'ptr'" } */ |
| 367 |
/* { dg-bogus "leak of 'tmp.m_ptr'" "" { target *-*-* } .-1 } */ |
| 368 |
|
| 369 |
void test_32 (void) |
| 370 |
{ |
| 371 |
void *ptr = malloc (1024); |
| 372 |
could_free (ptr); |
| 373 |
} /* { dg-bogus "leak" } */ |
| 374 |
|
| 375 |
void test_33 (void) |
| 376 |
{ |
| 377 |
void *ptr = malloc (1024); /* { dg-message "allocated here" } */ |
| 378 |
cant_free (ptr); |
| 379 |
} /* { dg-warning "leak of 'ptr'" } */ |
| 380 |
|
| 381 |
void test_34 (void) |
| 382 |
{ |
| 383 |
float *q; |
| 384 |
struct coord *p = malloc (sizeof (struct coord)); |
| 385 |
if (!p) |
| 386 |
return; |
| 387 |
p->x = 0.0f; |
| 388 |
q = &p->x; |
| 389 |
free (p); |
| 390 |
*q = 1.0f; /* { dg-warning "use after 'free' of 'q'" } */ |
| 391 |
}; |
| 392 |
|
| 393 |
int test_35 (void) |
| 394 |
{ |
| 395 |
void *ptr = malloc(4096); |
| 396 |
if (!ptr) |
| 397 |
return -1; |
| 398 |
__builtin_memset(ptr, 0, 4096); |
| 399 |
free(ptr); |
| 400 |
return 0; |
| 401 |
} |
| 402 |
|
| 403 |
void test_36 (void) |
| 404 |
{ |
| 405 |
void *ptr = malloc(4096); |
| 406 |
if (!ptr) |
| 407 |
return; |
| 408 |
__builtin_memset(ptr, 0, 4096); |
| 409 |
free(ptr); |
| 410 |
} |
| 411 |
|
| 412 |
void *test_37a (void) |
| 413 |
{ |
| 414 |
void *ptr = malloc(4096); /* { dg-message "this call could return NULL" } */ |
| 415 |
__builtin_memset(ptr, 0, 4096); /* { dg-warning "use of possibly-NULL 'ptr' where non-null expected \\\[CWE-690\\\]" } */ |
| 416 |
return ptr; |
| 417 |
} |
| 418 |
|
| 419 |
int test_37b (void) |
| 420 |
{ |
| 421 |
void *p = malloc(4096); |
| 422 |
void *q = malloc(4096); /* { dg-message "this call could return NULL" } */ |
| 423 |
if (p) { |
| 424 |
__builtin_memset(p, 0, 4096); /* Not a bug: checked */ |
| 425 |
} else { |
| 426 |
__builtin_memset(q, 0, 4096); /* { dg-warning "use of possibly-NULL 'q' where non-null expected \\\[CWE-690\\\]" } */ |
| 427 |
} |
| 428 |
free(p); |
| 429 |
free(q); |
| 430 |
return 0; |
| 431 |
} |
| 432 |
|
| 433 |
extern void might_use_ptr (void *ptr); |
| 434 |
|
| 435 |
void test_38(int i) |
| 436 |
{ |
| 437 |
void *p; |
| 438 |
|
| 439 |
p = malloc(1024); |
| 440 |
if (p) { |
| 441 |
free(p); |
| 442 |
might_use_ptr(p); /* { dg-warning "use after 'free' of 'p'" "" { xfail *-*-* } } */ |
| 443 |
// TODO: xfail |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
int * |
| 448 |
test_39 (int i) |
| 449 |
{ |
| 450 |
int *p = (int*)malloc(sizeof(int*)); /* { dg-message "this call could return NULL" } */ |
| 451 |
*p = i; /* { dg-warning "dereference of possibly-NULL 'p' \\\[CWE-690\\\]" } */ |
| 452 |
return p; |
| 453 |
} |
| 454 |
|
| 455 |
int * |
| 456 |
test_40 (int i) |
| 457 |
{ |
| 458 |
int *p = (int*)malloc(sizeof(int*)); /* { dg-message "region created on heap here" } */ |
| 459 |
i = *p; /* { dg-warning "dereference of possibly-NULL 'p' \\\[CWE-690\\\]" "possibly-null" } */ |
| 460 |
/* { dg-warning "use of uninitialized value '\\*p'" "uninit" { target *-*-*} .-1 } */ |
| 461 |
return p; |
| 462 |
} |
| 463 |
|
| 464 |
char * |
| 465 |
test_41 (int flag) |
| 466 |
{ |
| 467 |
char *buffer; |
| 468 |
|
| 469 |
if (flag) { |
| 470 |
buffer = (char*)malloc(4096); |
| 471 |
} else { |
| 472 |
buffer = NULL; |
| 473 |
} |
| 474 |
|
| 475 |
buffer[0] = 'a'; /* { dg-warning "dereference of possibly-NULL 'buffer' \\\[CWE-690\\\]" "possibly-NULL" } */ |
| 476 |
/* { dg-warning "dereference of NULL 'buffer' \\\[CWE-476\\\]" "NULL" { target *-*-* } .-1 } */ |
| 477 |
|
| 478 |
return buffer; |
| 479 |
} |
| 480 |
|
| 481 |
void test_42a (void) |
| 482 |
{ |
| 483 |
void *p = malloc (1024); /* { dg-message "allocated here" } */ |
| 484 |
free (p + 64); /* this could well corrupt the heap. */ |
| 485 |
/* TODO: ^^^ we should warn about this. */ |
| 486 |
} /* { dg-warning "leak of 'p'" } */ |
| 487 |
/* TODO: presumably we should complain about the bogus free, but then |
| 488 |
maybe not complain about the leak. */ |
| 489 |
// CWE-761: Free of Pointer not at Start of Buffer |
| 490 |
|
| 491 |
void test_42b (void) |
| 492 |
{ |
| 493 |
void *p = malloc (1024); /* { dg-message "allocated here" } */ |
| 494 |
free (p - 64); /* this could well corrupt the heap. */ |
| 495 |
/* TODO: ^^^ we should warn about this. */ |
| 496 |
} /* { dg-warning "leak of 'p'" } */ |
| 497 |
/* TODO: presumably we should complain about the bogus free, but then |
| 498 |
maybe not complain about the leak. */ |
| 499 |
// CWE-761: Free of Pointer not at Start of Buffer |
| 500 |
|
| 501 |
void test_42c (void) |
| 502 |
{ |
| 503 |
void *p = malloc (1024); |
| 504 |
void *q = p + 64; |
| 505 |
free (q - 64); /* this is probably OK. */ |
| 506 |
} /* { dg-bogus "leak of 'p'" } */ |
| 507 |
|
| 508 |
void * |
| 509 |
test_42d (void) |
| 510 |
{ |
| 511 |
void *p = malloc (1024); |
| 512 |
void *q = p + 64; |
| 513 |
return q; |
| 514 |
} /* { dg-bogus "leak of 'p'" } */ |
| 515 |
|
| 516 |
#if 0 |
| 517 |
void test_31 (void *p) |
| 518 |
{ |
| 519 |
void *q = realloc (p, 1024); |
| 520 |
free (p); /* FIXME: this is a double-'free'. */ |
| 521 |
free (q); |
| 522 |
} |
| 523 |
|
| 524 |
void test_32 (void) |
| 525 |
{ |
| 526 |
void *p = malloc (64); |
| 527 |
p = realloc (p, 1024); /* FIXME: this leaks if it fails. */ |
| 528 |
free (p); |
| 529 |
} |
| 530 |
#endif |
| 531 |
|
| 532 |
struct link global_link; |
| 533 |
|
| 534 |
void test_43 (void) |
| 535 |
{ |
| 536 |
global_link.m_ptr = malloc (sizeof (struct link)); /* { dg-message "allocated here" } */ |
| 537 |
global_link.m_ptr = NULL; /* { dg-warning "leak of 'global_link.m_ptr'" } */ |
| 538 |
} |
| 539 |
|
| 540 |
struct link *global_ptr; |
| 541 |
|
| 542 |
void test_44 (void) |
| 543 |
{ |
| 544 |
global_ptr = malloc (sizeof (struct link)); |
| 545 |
if (!global_ptr) |
| 546 |
return; |
| 547 |
global_ptr->m_ptr = malloc (sizeof (struct link)); /* { dg-message "allocated here" } */ |
| 548 |
free (global_ptr); /* { dg-warning "leak of '<unknown>'" } */ |
| 549 |
/* TODO: should be more precise than just '<unknown>'. */ |
| 550 |
} |
| 551 |
|
| 552 |
extern void might_take_ownership (void *ptr); |
| 553 |
|
| 554 |
void test_45 (void) |
| 555 |
{ |
| 556 |
void *p = malloc (1024); |
| 557 |
might_take_ownership (p); |
| 558 |
} |
| 559 |
|
| 560 |
void test_46 (void) |
| 561 |
{ |
| 562 |
struct link *p = (struct link *)malloc (sizeof (struct link)); |
| 563 |
if (!p) |
| 564 |
return; |
| 565 |
struct link *q = (struct link *)malloc (sizeof (struct link)); |
| 566 |
p->m_ptr = q; |
| 567 |
might_take_ownership (p); |
| 568 |
} |
| 569 |
|
| 570 |
extern int maybe_alloc (char **); |
| 571 |
|
| 572 |
int test_47 (void) |
| 573 |
{ |
| 574 |
char *p = ((void *)0); |
| 575 |
int p_size = 0; |
| 576 |
|
| 577 |
p = malloc (16); |
| 578 |
if (p) { |
| 579 |
free (p); |
| 580 |
} else { |
| 581 |
int retval = maybe_alloc (&p); /* this might write to "p". */ |
| 582 |
if (retval) |
| 583 |
return (retval); |
| 584 |
p_size = __builtin_strlen(p); /* { dg-bogus "non-null expected" } */ |
| 585 |
free (p); |
| 586 |
} |
| 587 |
return p_size; |
| 588 |
} |
| 589 |
|
| 590 |
void test_48 (void) |
| 591 |
{ |
| 592 |
int *p = NULL; /* { dg-message "'p' is NULL" } */ |
| 593 |
*p = 1; /* { dg-warning "dereference of NULL 'p' \\\[CWE-476\\\]" } */ |
| 594 |
} |
| 595 |
|
| 596 |
/* As test_48, but where the assignment of NULL is not at the start of a BB. */ |
| 597 |
|
| 598 |
int test_49 (int i) |
| 599 |
{ |
| 600 |
int *p; |
| 601 |
int x; |
| 602 |
|
| 603 |
x = i * 2; |
| 604 |
p = NULL; /* { dg-message "'p' is NULL" } */ |
| 605 |
*p = 1; /* { dg-warning "dereference of NULL 'p' \\\[CWE-476\\\]" } */ |
| 606 |
return x; |
| 607 |
} |
| 608 |
|
| 609 |
/* Free of function, and of label within function. */ |
| 610 |
|
| 611 |
void test_50a (void) |
| 612 |
{ |
| 613 |
} |
| 614 |
|
| 615 |
void test_50b (void) |
| 616 |
{ |
| 617 |
free (test_50a); /* { dg-warning "'free' of '&test_50a' which points to memory not on the heap \\\[CWE-590\\\]" } */ |
| 618 |
} |
| 619 |
|
| 620 |
void test_50c (void) |
| 621 |
{ |
| 622 |
my_label: |
| 623 |
free (&&my_label); /* { dg-warning "'free' of '&my_label' which points to memory not on the heap \\\[CWE-590\\\]" } */ |
| 624 |
} |
| 625 |
|
| 626 |
/* Double free after unconditional dereference. */ |
| 627 |
|
| 628 |
int test_51 (int *p) |
| 629 |
{ |
| 630 |
int result = *p; |
| 631 |
free (p); /* { dg-message "first 'free' here" } */ |
| 632 |
free (p); /* { dg-warning "double-'free' of 'p'" } */ |
| 633 |
return result; |
| 634 |
} |
| 635 |
|
| 636 |
/* { dg-prune-output "\\\[-Wfree-nonheap-object" } */ |