Debian Codegraph @

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

raw
1 /* { dg-skip-if "requires hosted libstdc++ for stdlib malloc" { ! hostedlib } } */
2
3 #include <stdlib.h>
4 #include "analyzer-decls.h"
5
6 extern int foo (int);
7
8 static int __attribute__((noinline))
9 do_stuff_2 (int *p, int n)
10 {
11 return 0;
12 }
13
14 /* As malloc-vs-local.c, but hand-inlining the logic. */
15
16 /* Repeated (n > 10) predicate. */
17
18 int test_repeated_predicate_1 (int n)
19 {
20 int buf[10];
21 int *ptr;
22 int result;
23
24 if (n > 10)
25 ptr = (int *)malloc (sizeof (int) * n);
26 else
27 ptr = buf;
28
29 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
30
31 {
32 int *p = ptr;
33 int sum = 0;
34 int i;
35 for (i = 0; i < n; i++)
36 p[i] = i; /* { dg-warning "dereference of possibly-NULL" } */
37 for (i = 0; i < n; i++)
38 sum += foo (p[i]); /* { dg-bogus "uninitialized" } */
39 result = sum;
40 }
41
42 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
43
44 if (n > 10)
45 free (ptr); /* { dg-bogus "not on the heap" } */
46
47 return result; /* { dg-bogus "leak" } */
48 }
49
50 /* As above, but with just one loop. */
51
52 int test_repeated_predicate_1a (int n)
53 {
54 int buf[10];
55 int *ptr;
56 int result;
57
58 if (n > 10)
59 ptr = (int *)malloc (sizeof (int) * n);
60 else
61 ptr = buf;
62
63 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
64
65 {
66 int *p = ptr;
67 int sum = 0;
68 int i;
69 for (i = 0; i < n; i++)
70 p[i] = i; /* { dg-warning "dereference of possibly-NULL" } */
71 result = sum;
72 }
73
74 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
75
76 if (n > 10)
77 free (ptr); /* { dg-bogus "not on the heap" } */
78
79 return result; /* { dg-bogus "leak" } */
80 }
81
82 /* A simpler version of the above. */
83
84 int test_repeated_predicate_2 (int n)
85 {
86 int buf[10];
87 int *ptr;
88 int result;
89
90 if (n > 10)
91 ptr = (int *)malloc (sizeof (int) * n);
92 else
93 ptr = buf;
94
95 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
96
97 result = do_stuff_2 (ptr, n);
98
99 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
100
101 if (n > 10)
102 free (ptr); /* { dg-bogus "not on the heap" } */
103
104 return result; /* { dg-bogus "leak" } */
105 }
106
107 /* A predicate that sets a flag for the 2nd test. */
108
109 int test_explicit_flag (int n)
110 {
111 int buf[10];
112 int *ptr;
113 int result;
114 int need_to_free = 0;
115
116 if (n > 10)
117 {
118 ptr = (int *)malloc (sizeof (int) * n);
119 need_to_free = 1;
120 }
121 else
122 ptr = buf;
123
124 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
125
126 {
127 int *p = ptr;
128 int sum = 0;
129 int i;
130 for (i = 0; i < n; i++)
131 p[i] = i; /* { dg-warning "dereference of possibly-NULL" } */
132 for (i = 0; i < n; i++)
133 sum += foo (p[i]); /* { dg-bogus "uninitialized" } */
134 result = sum;
135 }
136
137 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
138
139 if (need_to_free)
140 free (ptr); /* { dg-bogus "not on the heap" } */
141
142 return result; /* { dg-bogus "leak" } */
143 }
144
145 /* Pointer comparison. */
146
147 int test_pointer_comparison (int n)
148 {
149 int buf[10];
150 int *ptr;
151 int result;
152
153 if (n > 10)
154 ptr = (int *)malloc (sizeof (int) * n);
155 else
156 ptr = buf;
157
158 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
159
160 {
161 int *p = ptr;
162 int sum = 0;
163 int i;
164 for (i = 0; i < n; i++)
165 p[i] = i; /* { dg-warning "dereference of possibly-NULL" } */
166 for (i = 0; i < n; i++)
167 sum += foo (p[i]); /* { dg-bogus "uninitialized" } */
168 result = sum;
169 }
170
171 __analyzer_dump_exploded_nodes (0); /* { dg-warning "2 processed enodes" } */
172
173 if (ptr != buf)
174 free (ptr); /* { dg-bogus "not on the heap" } */
175
176 return result; /* { dg-bogus "leak" } */
177 }