| 1 |
/* { dg-skip-if "requires hosted libstdc++ for stdlib size_t" { ! hostedlib } } */ |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
#include "analyzer-decls.h" |
| 5 |
|
| 6 |
static void __attribute__((noinline)) |
| 7 |
__analyzer_callee_1 (size_t inner_sz) |
| 8 |
{ |
| 9 |
void *p = __builtin_alloca (inner_sz); |
| 10 |
__analyzer_dump_capacity (p); /* { dg-warning "capacity: 'INIT_VAL\\(outer_sz_\[^\n\r\]*\\)'" } */ |
| 11 |
} |
| 12 |
|
| 13 |
void |
| 14 |
test_1 (int flag, size_t outer_sz) |
| 15 |
{ |
| 16 |
if (flag) |
| 17 |
__analyzer_callee_1 (outer_sz); |
| 18 |
|
| 19 |
/* Verify that we merge state; in particular, the dynamic size of "p" |
| 20 |
in the called frame should have been purged. */ |
| 21 |
__analyzer_dump_exploded_nodes (0); /* { dg-warning "1 processed enode" } */ |
| 22 |
} |
| 23 |
|
| 24 |
void |
| 25 |
test_2 (int flag, size_t sz) |
| 26 |
{ |
| 27 |
if (flag) |
| 28 |
{ |
| 29 |
void *p = malloc (sz); |
| 30 |
__analyzer_dump_capacity (p); /* { dg-warning "capacity: 'INIT_VAL\\(sz_\[^\n\r\]*\\)'" } */ |
| 31 |
free (p); |
| 32 |
/* The dynamic size of "p" should have been purged. */ |
| 33 |
__analyzer_dump_capacity (p); /* { dg-warning "capacity: 'UNKNOWN\\(sizetype\\)'" } */ |
| 34 |
} |
| 35 |
|
| 36 |
/* Verify that we merge state. */ |
| 37 |
__analyzer_dump_exploded_nodes (0); /* { dg-warning "1 processed enode" } */ |
| 38 |
} |
| 39 |
/* Verify that we purge state on the NULL branch when a malloc result is |
| 40 |
tested against NULL. */ |
| 41 |
|
| 42 |
void |
| 43 |
test_3 (size_t sz) |
| 44 |
{ |
| 45 |
void *p = malloc (sz); |
| 46 |
|
| 47 |
if (p) |
| 48 |
{ |
| 49 |
__analyzer_dump_capacity (p); /* { dg-warning "capacity: 'INIT_VAL\\(sz_\[^\n\r\]*\\)'" } */ |
| 50 |
} |
| 51 |
else |
| 52 |
{ |
| 53 |
/* The dynamic size of "p" should have been purged |
| 54 |
due to "p" being equal to NULL. */ |
| 55 |
__analyzer_dump_capacity (p); /* { dg-warning "capacity: 'UNKNOWN\\(sizetype\\)'" } */ |
| 56 |
} |
| 57 |
|
| 58 |
free (p); |
| 59 |
|
| 60 |
/* The dynamic size of "p" should have been purged. */ |
| 61 |
__analyzer_dump_capacity (p); /* { dg-warning "capacity: 'UNKNOWN\\(sizetype\\)'" } */ |
| 62 |
|
| 63 |
/* Verify that we merge state. */ |
| 64 |
__analyzer_dump_exploded_nodes (0); /* { dg-warning "1 processed enode" } */ |
| 65 |
} |
| 66 |
|
| 67 |
/* Verify that we purge dynamic extent of a pointer when it leaks. */ |
| 68 |
|
| 69 |
static void __attribute__((noinline)) |
| 70 |
__analyzer_callee_4 (size_t inner_sz) |
| 71 |
{ |
| 72 |
void *p = malloc (inner_sz); |
| 73 |
__analyzer_dump_capacity (p); /* { dg-warning "capacity: 'INIT_VAL\\(outer_sz_\[^\n\r\]*\\)'" } */ |
| 74 |
} /* { dg-warning "leak of 'p'" } */ |
| 75 |
|
| 76 |
void |
| 77 |
test_4 (int flag, size_t outer_sz) |
| 78 |
{ |
| 79 |
if (flag) |
| 80 |
__analyzer_callee_4 (outer_sz); |
| 81 |
|
| 82 |
/* Verify that we merge state. */ |
| 83 |
__analyzer_dump_exploded_nodes (0); /* { dg-warning "1 processed enode" } */ |
| 84 |
} |