Debian Codegraph @

gcc/testsuite/c-c++-common/analyzer/malloc-vs-local-1a.c

raw
1 /* { dg-additional-options "-fno-analyzer-call-summaries -fanalyzer-transitivity" } */
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; /* { dg-warning "dereference of possibly-NULL 'p'" } */
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 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
55
56
57 if (n > 10)
58 free (ptr); /* { dg-bogus "not on the heap" } */
59
60 return result; /* { dg-bogus "leak" } */
61 }
62
63 /* A simpler version of the above. */
64
65 int test_repeated_predicate_2 (int n)
66 {
67 int buf[10];
68 int *ptr;
69 int result;
70
71 if (n > 10)
72 ptr = (int *)malloc (sizeof (int) * n);
73 else
74 ptr = buf;
75
76 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
77
78 result = do_stuff_2 (ptr, n);
79
80 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
81
82 if (n > 10)
83 free (ptr); /* { dg-bogus "not on the heap" } */
84
85 return result; /* { dg-bogus "leak" } */
86 }
87
88 /* A predicate that sets a flag for the 2nd test. */
89
90 int test_explicit_flag (int n)
91 {
92 int buf[10];
93 int *ptr;
94 int result;
95 int need_to_free = 0;
96
97 if (n > 10)
98 {
99 ptr = (int *)malloc (sizeof (int) * n);
100 need_to_free = 1;
101 }
102 else
103 ptr = buf;
104
105 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
106
107 result = do_stuff (ptr, n);
108
109 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
110
111
112 if (need_to_free)
113 free (ptr); /* { dg-bogus "not on the heap" } */
114
115 return result; /* { dg-bogus "leak" } */
116 }
117
118 /* Pointer comparison. */
119
120 int test_pointer_comparison (int n)
121 {
122 int buf[10];
123 int *ptr;
124 int result;
125
126 if (n > 10)
127 ptr = (int *)malloc (sizeof (int) * n);
128 else
129 ptr = buf;
130
131 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
132
133 result = do_stuff (ptr, n);
134
135 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
136
137
138 if (ptr != buf)
139 free (ptr); /* { dg-bogus "not on the heap" } */
140
141 return result; /* { dg-bogus "leak" } */
142 }
143
144 /* Set a flag based on a conditional, then use it, then reuse the
145 conditional. */
146
147 int test_initial_flag (int n)
148 {
149 int buf[10];
150 int *ptr;
151 int result;
152 int on_heap = 0;
153
154 if (n > 10)
155 on_heap = 1;
156 else
157 on_heap = 0;
158
159 /* Due to state-merging, we lose the relationship between 'n > 10'
160 and 'on_heap' here; we have to rely on feasibility-checking
161 in the diagnostic_manager to reject the false warnings. */
162 __analyzer_dump_exploded_nodes (0); /* { dg-warning "1 processed enode" } */
163
164 if (on_heap)
165 ptr = (int *)malloc (sizeof (int) * n);
166 else
167 ptr = buf;
168
169 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
170
171 result = do_stuff (ptr, n);
172
173 __analyzer_dump_exploded_nodes (0); /* { dg-warning "3 processed enodes" } */
174 // FIXME: why 3 here?
175
176 if (n > 10)
177 free (ptr); /* { dg-bogus "not on the heap" } */
178
179 return result; /* { dg-bogus "leak" } */
180 }