| 1 |
/* { dg-additional-options "-fanalyzer-call-summaries" } */ |
| 2 |
/* { dg-skip-if "requires hosted libstdc++ for stdlib malloc" { ! hostedlib } } */ |
| 3 |
|
| 4 |
#include <stdlib.h> |
| 5 |
#include "analyzer-decls.h" |
| 6 |
|
| 7 |
extern int foo (int); |
| 8 |
|
| 9 |
static int __attribute__((noinline)) |
| 10 |
do_stuff (int *p, int n) |
| 11 |
{ |
| 12 |
int sum = 0; |
| 13 |
int i; |
| 14 |
for (i = 0; i < n; i++) |
| 15 |
p[i] = i; |
| 16 |
for (i = 0; i < n; i++) |
| 17 |
sum += foo (p[i]); /* { dg-bogus "uninitialized" } */ |
| 18 |
return sum; |
| 19 |
} |
| 20 |
|
| 21 |
static int __attribute__((noinline)) |
| 22 |
do_stuff_2 (int *p, int n) |
| 23 |
{ |
| 24 |
return 0; |
| 25 |
} |
| 26 |
|
| 27 |
/* Various examples of functions that use either a malloc buffer |
| 28 |
or a local buffer, do something, then conditionally free the |
| 29 |
buffer, tracking whether "free" is necessary in various |
| 30 |
ways. |
| 31 |
|
| 32 |
In each case, there ought to be only two paths through the function, |
| 33 |
not four. */ |
| 34 |
|
| 35 |
/* Repeated (n > 10) predicate. */ |
| 36 |
|
| 37 |
int test_repeated_predicate_1 (int n) |
| 38 |
{ |
| 39 |
int buf[10]; |
| 40 |
int *ptr; |
| 41 |
int result; |
| 42 |
|
| 43 |
if (n > 10) |
| 44 |
ptr = (int *)malloc (sizeof (int) * n); |
| 45 |
else |
| 46 |
ptr = buf; |
| 47 |
|
| 48 |
__analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */ |
| 49 |
|
| 50 |
result = do_stuff (ptr, n); |
| 51 |
|
| 52 |
__analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */ |
| 53 |
|
| 54 |
if (n > 10) |
| 55 |
free (ptr); /* { dg-bogus "not on the heap" } */ |
| 56 |
|
| 57 |
return result; /* { dg-bogus "leak" } */ |
| 58 |
} |
| 59 |
|
| 60 |
/* A simpler version of the above. */ |
| 61 |
|
| 62 |
int test_repeated_predicate_2 (int n) |
| 63 |
{ |
| 64 |
int buf[10]; |
| 65 |
int *ptr; |
| 66 |
int result; |
| 67 |
|
| 68 |
if (n > 10) |
| 69 |
ptr = (int *)malloc (sizeof (int) * n); |
| 70 |
else |
| 71 |
ptr = buf; |
| 72 |
|
| 73 |
__analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */ |
| 74 |
|
| 75 |
result = do_stuff_2 (ptr, n); |
| 76 |
|
| 77 |
__analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */ |
| 78 |
|
| 79 |
if (n > 10) |
| 80 |
free (ptr); /* { dg-bogus "not on the heap" } */ |
| 81 |
|
| 82 |
return result; /* { dg-bogus "leak" } */ |
| 83 |
} |
| 84 |
|
| 85 |
/* A predicate that sets a flag for the 2nd test. */ |
| 86 |
|
| 87 |
int test_explicit_flag (int n) |
| 88 |
{ |
| 89 |
int buf[10]; |
| 90 |
int *ptr; |
| 91 |
int result; |
| 92 |
int need_to_free = 0; |
| 93 |
|
| 94 |
if (n > 10) |
| 95 |
{ |
| 96 |
ptr = (int *)malloc (sizeof (int) * n); |
| 97 |
need_to_free = 1; |
| 98 |
} |
| 99 |
else |
| 100 |
ptr = buf; |
| 101 |
|
| 102 |
__analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */ |
| 103 |
|
| 104 |
result = do_stuff (ptr, n); |
| 105 |
|
| 106 |
__analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */ |
| 107 |
|
| 108 |
if (need_to_free) |
| 109 |
free (ptr); /* { dg-bogus "not on the heap" } */ |
| 110 |
|
| 111 |
return result; /* { dg-bogus "leak" } */ |
| 112 |
} |
| 113 |
|
| 114 |
/* Pointer comparison. */ |
| 115 |
|
| 116 |
int test_pointer_comparison (int n) |
| 117 |
{ |
| 118 |
int buf[10]; |
| 119 |
int *ptr; |
| 120 |
int result; |
| 121 |
|
| 122 |
if (n > 10) |
| 123 |
ptr = (int *)malloc (sizeof (int) * n); |
| 124 |
else |
| 125 |
ptr = buf; |
| 126 |
|
| 127 |
__analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */ |
| 128 |
|
| 129 |
result = do_stuff (ptr, n); |
| 130 |
|
| 131 |
__analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */ |
| 132 |
|
| 133 |
if (ptr != buf) |
| 134 |
free (ptr); /* { dg-bogus "not on the heap" } */ |
| 135 |
|
| 136 |
return result; /* { dg-bogus "leak" } */ |
| 137 |
} |
| 138 |
|
| 139 |
/* Set a flag based on a conditional, then use it, then reuse the |
| 140 |
conditional. */ |
| 141 |
|
| 142 |
int test_initial_flag (int n) |
| 143 |
{ |
| 144 |
int buf[10]; |
| 145 |
int *ptr; |
| 146 |
int result; |
| 147 |
int on_heap = 0; |
| 148 |
|
| 149 |
if (n > 10) |
| 150 |
on_heap = 1; |
| 151 |
else |
| 152 |
on_heap = 0; |
| 153 |
|
| 154 |
/* Due to state-merging, we lose the relationship between 'n > 10' |
| 155 |
and 'on_heap' here; we have to rely on feasibility-checking |
| 156 |
in the diagnostic_manager to reject the false warnings. */ |
| 157 |
__analyzer_dump_exploded_nodes (0); /* { dg-warning "1 processed enode" } */ |
| 158 |
|
| 159 |
if (on_heap) |
| 160 |
ptr = (int *)malloc (sizeof (int) * n); |
| 161 |
else |
| 162 |
ptr = buf; |
| 163 |
|
| 164 |
__analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */ |
| 165 |
|
| 166 |
result = do_stuff (ptr, n); |
| 167 |
|
| 168 |
__analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */ |
| 169 |
|
| 170 |
if (n > 10) |
| 171 |
free (ptr); /* { dg-bogus "not on the heap" } */ |
| 172 |
|
| 173 |
return result; /* { dg-bogus "leak" } */ |
| 174 |
} |