| 1 |
#include <stdlib.h> |
| 2 |
|
| 3 |
struct st |
| 4 |
{ |
| 5 |
void *m_f; |
| 6 |
}; |
| 7 |
|
| 8 |
struct node |
| 9 |
{ |
| 10 |
struct node *m_next; |
| 11 |
}; |
| 12 |
|
| 13 |
extern void unknown_fn (void *); |
| 14 |
extern void const_unknown_fn (const void *); |
| 15 |
|
| 16 |
void |
| 17 |
test_1 (struct st *p, struct st *q) |
| 18 |
{ |
| 19 |
p->m_f = malloc (1024); |
| 20 |
q->m_f = NULL; /* { dg-bogus "leak" } */ |
| 21 |
free (p->m_f); |
| 22 |
} |
| 23 |
|
| 24 |
void |
| 25 |
test_2 (void) |
| 26 |
{ |
| 27 |
struct st s; |
| 28 |
s.m_f = malloc (1024); |
| 29 |
unknown_fn (&s); |
| 30 |
free (s.m_f); |
| 31 |
} |
| 32 |
|
| 33 |
void |
| 34 |
test_3 (void) |
| 35 |
{ |
| 36 |
struct st s; |
| 37 |
s.m_f = malloc (1024); |
| 38 |
const_unknown_fn (&s); |
| 39 |
free (s.m_f); |
| 40 |
} |
| 41 |
|
| 42 |
void |
| 43 |
test_4 (void) |
| 44 |
{ |
| 45 |
struct st s; |
| 46 |
s.m_f = malloc (1024); |
| 47 |
unknown_fn (&s); |
| 48 |
} /* { dg-bogus "leak" } */ |
| 49 |
|
| 50 |
void |
| 51 |
test_5 (void) |
| 52 |
{ |
| 53 |
struct st s; |
| 54 |
s.m_f = malloc (1024); |
| 55 |
/* s is const, but the pointer could still be freed; hence not a leak. */ |
| 56 |
const_unknown_fn (&s); |
| 57 |
} /* { dg-bogus "leak" } */ |
| 58 |
|
| 59 |
void |
| 60 |
test_6 (void) |
| 61 |
{ |
| 62 |
struct st s; |
| 63 |
s.m_f = malloc (1024); |
| 64 |
} /* { dg-warning "leak" } */ |
| 65 |
|
| 66 |
struct st |
| 67 |
test_7 (void) |
| 68 |
{ |
| 69 |
struct st s; |
| 70 |
s.m_f = malloc (1024); |
| 71 |
return s; |
| 72 |
} /* { dg-bogus "leak" } */ |
| 73 |
|
| 74 |
struct node * |
| 75 |
test_8 (void) |
| 76 |
{ |
| 77 |
struct node *n1 = malloc (sizeof (struct node)); |
| 78 |
if (!n1) |
| 79 |
return NULL; |
| 80 |
n1->m_next = malloc (sizeof (struct node)); |
| 81 |
return n1; |
| 82 |
} |
| 83 |
|
| 84 |
void |
| 85 |
test_9 (void) |
| 86 |
{ |
| 87 |
struct node *n1 = malloc (sizeof (struct node)); |
| 88 |
if (!n1) |
| 89 |
return; |
| 90 |
n1->m_next = malloc (sizeof (struct node)); |
| 91 |
/* Could free n1 and n1->m_next. */ |
| 92 |
unknown_fn (n1); |
| 93 |
} |
| 94 |
|
| 95 |
void |
| 96 |
test_10 (void) |
| 97 |
{ |
| 98 |
struct node *n1 = malloc (sizeof (struct node)); |
| 99 |
if (!n1) |
| 100 |
return; |
| 101 |
n1->m_next = malloc (sizeof (struct node)); |
| 102 |
/* Could free n1->m_next, but not n1. */ |
| 103 |
const_unknown_fn (n1); /* { dg-warning "leak of 'n1'" } */ |
| 104 |
} |
| 105 |
|
| 106 |
void |
| 107 |
test_11 (void) |
| 108 |
{ |
| 109 |
struct node *n1 = malloc (sizeof (struct node)); |
| 110 |
if (!n1) |
| 111 |
return; |
| 112 |
n1->m_next = malloc (sizeof (struct node)); |
| 113 |
/* Could free n1->m_next, but not n1. */ |
| 114 |
unknown_fn (n1->m_next); /* { dg-warning "leak of 'n1'" } */ |
| 115 |
} |
| 116 |
|
| 117 |
void |
| 118 |
test_12a (void) |
| 119 |
{ |
| 120 |
int *ip = malloc (sizeof (int)); |
| 121 |
*ip = 42; /* { dg-warning "dereference of possibly-NULL 'ip'" } */ |
| 122 |
free (ip); |
| 123 |
} |
| 124 |
|
| 125 |
void |
| 126 |
test_12b (void) |
| 127 |
{ |
| 128 |
int *ip = malloc (sizeof (int)); |
| 129 |
unknown_fn (ip); |
| 130 |
/* Might not be a null-deref, as unknown_fn could abort on NULL. */ |
| 131 |
*ip = 42; |
| 132 |
free (ip); |
| 133 |
} |
| 134 |
|
| 135 |
void |
| 136 |
test_12c (void) |
| 137 |
{ |
| 138 |
int *ip = malloc (sizeof (int)); |
| 139 |
/* Might not be a null-deref, as const_unknown_fn could abort on NULL. |
| 140 |
Right now we don't have a great way of handling this. */ |
| 141 |
const_unknown_fn (ip); |
| 142 |
*ip = 42; /* { dg-bogus "dereference of possibly-NULL 'ip'" "" { xfail *-*-* } } */ |
| 143 |
free (ip); |
| 144 |
} |