| 1 |
extern void free (void *); |
| 2 |
|
| 3 |
struct foo |
| 4 |
{ |
| 5 |
int m_int; |
| 6 |
}; |
| 7 |
|
| 8 |
extern void foo_release (struct foo *); |
| 9 |
extern struct foo *foo_acquire (void) |
| 10 |
__attribute__ ((malloc (foo_release))); |
| 11 |
extern void use_foo (const struct foo *) |
| 12 |
__attribute__((nonnull)); |
| 13 |
|
| 14 |
extern struct foo *compatible_alloc (void) |
| 15 |
__attribute__ ((malloc (__builtin_free))); |
| 16 |
|
| 17 |
extern struct foo *compatible_alloc2 (void) |
| 18 |
__attribute__ ((malloc (free))); |
| 19 |
|
| 20 |
void test_1 (void) |
| 21 |
{ |
| 22 |
struct foo *p = foo_acquire (); |
| 23 |
foo_release (p); |
| 24 |
} |
| 25 |
|
| 26 |
void test_2 (void) |
| 27 |
{ |
| 28 |
struct foo *p = foo_acquire (); /* { dg-message "this call could return NULL" } */ |
| 29 |
p->m_int = 42; /* { dg-warning "dereference of possibly-NULL 'p'" } */ |
| 30 |
foo_release (p); |
| 31 |
} |
| 32 |
|
| 33 |
void test_2a (void) |
| 34 |
{ |
| 35 |
struct foo *p = foo_acquire (); /* { dg-message "this call could return NULL" } */ |
| 36 |
use_foo (p); /* { dg-warning "use of possibly-NULL 'p' where non-null expected" } */ |
| 37 |
foo_release (p); |
| 38 |
} |
| 39 |
|
| 40 |
void test_3 (void) |
| 41 |
{ |
| 42 |
struct foo *p = foo_acquire (); /* { dg-message "allocated here" } */ |
| 43 |
} /* { dg-warning "leak of 'p'" } */ |
| 44 |
|
| 45 |
void test_4 (struct foo *p) |
| 46 |
{ |
| 47 |
foo_release (p); |
| 48 |
foo_release (p); /* { dg-warning "double-'foo_release' of 'p'" } */ |
| 49 |
} |
| 50 |
|
| 51 |
void test_4a (void) |
| 52 |
{ |
| 53 |
struct foo *p = foo_acquire (); |
| 54 |
foo_release (p); |
| 55 |
foo_release (p); /* { dg-warning "double-'foo_release' of 'p'" } */ |
| 56 |
} |
| 57 |
|
| 58 |
void test_5 (void) |
| 59 |
{ |
| 60 |
struct foo *p = foo_acquire (); /* { dg-message "allocated here \\(expects deallocation with 'foo_release'\\)" } */ |
| 61 |
free (p); /* { dg-warning "'p' should have been deallocated with 'foo_release' but was deallocated with 'free'" } */ |
| 62 |
} |
| 63 |
|
| 64 |
void test_6 (struct foo *p) |
| 65 |
{ |
| 66 |
foo_release (p); |
| 67 |
free (p); // TODO: double-release warning! |
| 68 |
} |
| 69 |
|
| 70 |
void test_7 () |
| 71 |
{ |
| 72 |
struct foo f; |
| 73 |
foo_release (&f); /* { dg-warning "on the stack" "analyzer" } */ |
| 74 |
/* { dg-warning "'foo_release' called on unallocated object 'f'" "non-analyzer" { target *-*-* } .-1 } */ |
| 75 |
} |
| 76 |
|
| 77 |
int test_8 (struct foo *p) |
| 78 |
{ |
| 79 |
foo_release (p); |
| 80 |
return p->m_int; /* { dg-warning "use after 'foo_release' of 'p'" } */ |
| 81 |
} |
| 82 |
|
| 83 |
/* Recognize that __builtin_free and free are the same thing. */ |
| 84 |
void test_9 (void) |
| 85 |
{ |
| 86 |
struct foo *p = compatible_alloc (); |
| 87 |
free (p); |
| 88 |
} |
| 89 |
|
| 90 |
void test_10 (void) |
| 91 |
{ |
| 92 |
struct foo *p = compatible_alloc2 (); |
| 93 |
__builtin_free (p); |
| 94 |
} |