| 1 |
/* Example of interprocedural detection of an uninitialized field |
| 2 |
in a heap-allocated struct. */ |
| 3 |
|
| 4 |
#include <stdlib.h> |
| 5 |
#include "analyzer-decls.h" |
| 6 |
|
| 7 |
struct foo |
| 8 |
{ |
| 9 |
int i; |
| 10 |
int j; |
| 11 |
int k; |
| 12 |
}; |
| 13 |
|
| 14 |
struct foo *__attribute__((noinline)) |
| 15 |
alloc_foo (int a, int b) |
| 16 |
{ |
| 17 |
struct foo *p = malloc (sizeof (struct foo)); /* { dg-message "region created on heap here" } */ |
| 18 |
if (!p) |
| 19 |
return NULL; |
| 20 |
p->i = a; |
| 21 |
p->k = b; |
| 22 |
return p; |
| 23 |
} |
| 24 |
|
| 25 |
void test_access_inited_fields (int x, int y, int z) |
| 26 |
{ |
| 27 |
struct foo *p = alloc_foo (x, z); |
| 28 |
if (!p) |
| 29 |
return; |
| 30 |
|
| 31 |
__analyzer_eval (p->i == x); /* { dg-warning "TRUE" } */ |
| 32 |
|
| 33 |
__analyzer_eval (p->k == z); /* { dg-warning "TRUE" } */ |
| 34 |
|
| 35 |
free (p); |
| 36 |
} |
| 37 |
|
| 38 |
void test_stop_after_accessing_uninit (int x, int y, int z) |
| 39 |
{ |
| 40 |
struct foo *p = alloc_foo (x, z); |
| 41 |
if (!p) |
| 42 |
return; |
| 43 |
|
| 44 |
__analyzer_eval (p->i == x); /* { dg-warning "TRUE" } */ |
| 45 |
|
| 46 |
__analyzer_eval (p->j == y); /* { dg-warning "use of uninitialized value '\\*p\\.j'" } */ |
| 47 |
|
| 48 |
__analyzer_dump_path (); /* { dg-bogus "path" } */ |
| 49 |
} |