Debian Codegraph @

gcc/testsuite/gcc.dg/analyzer/malloc-paths-9.c

raw
1 /* { dg-additional-options "-fdiagnostics-show-line-numbers -fdiagnostics-path-format=inline-events -fdiagnostics-show-caret" } */
2 /* { dg-enable-nn-line-numbers "" } */
3 /* C only: C++ exceptions mess up events. Therefore this test has been duplicated
4 as c-c++-common/analyzer/malloc-paths-9-noexcept.c */
5
6 #include <stdlib.h>
7
8 void test_1 (void)
9 {
10 void *ptr = malloc (1024);
11 free (ptr);
12 free (ptr); /* { dg-warning "double-'free' of 'ptr'" } */
13 }
14 /* { dg-begin-multiline-output "" }
15 NN | free (ptr);
16 | ^~~~~~~~~~
17 'test_1': events 1-3
18 NN | void *ptr = malloc (1024);
19 | ^~~~~~~~~~~~~
20 | |
21 | (1) allocated here
22 NN | free (ptr);
23 | ~~~~~~~~~~
24 | |
25 | (2) first 'free' here
26 NN | free (ptr);
27 | ~~~~~~~~~~
28 | |
29 | (3) second 'free' here; first 'free' was at (2)
30 { dg-end-multiline-output "" } */
31
32 void test_2 (int x, int y)
33 {
34 void *ptr = malloc (1024);
35 if (x)
36 free (ptr);
37 if (y)
38 free (ptr); /* { dg-warning "double-'free' of 'ptr'" } */
39 } /* { dg-warning "leak of 'ptr'" } */
40
41 /* "double-'free' of 'ptr'". */
42 /* { dg-begin-multiline-output "" }
43 NN | free (ptr);
44 | ^~~~~~~~~~
45 'test_2': events 1-7
46 NN | void *ptr = malloc (1024);
47 | ^~~~~~~~~~~~~
48 | |
49 | (1) allocated here
50 NN | if (x)
51 | ~
52 | |
53 | (2) following 'true' branch (when 'x != 0')...
54 NN | free (ptr);
55 | ~~~~~~~~~~
56 | |
57 | (3) ...to here
58 | (4) first 'free' here
59 NN | if (y)
60 | ~
61 | |
62 | (5) following 'true' branch (when 'y != 0')...
63 NN | free (ptr);
64 | ~~~~~~~~~~
65 | |
66 | (6) ...to here
67 | (7) second 'free' here; first 'free' was at (4)
68 { dg-end-multiline-output "" } */
69
70 /* "leak of 'ptr'. */
71 /* { dg-begin-multiline-output "" }
72 NN | }
73 | ^
74 'test_2': events 1-6
75 NN | void *ptr = malloc (1024);
76 | ^~~~~~~~~~~~~
77 | |
78 | (1) allocated here
79 NN | if (x)
80 | ~
81 | |
82 | (2) following 'false' branch (when 'x == 0')...
83 NN | free (ptr);
84 NN | if (y)
85 | ~
86 | |
87 | (3) ...to here
88 | (4) following 'false' branch (when 'y == 0')...
89 NN | free (ptr);
90 NN | }
91 | ~
92 | |
93 | (5) ...to here
94 | (6) 'ptr' leaks here; was allocated at (1)
95 { dg-end-multiline-output "" } */
96
97 int test_3 (int x, int y)
98 {
99 int *ptr = (int *)malloc (sizeof (int));
100 *ptr = 42; /* { dg-warning "dereference of possibly-NULL 'ptr'" } */
101 if (x)
102 free (ptr);
103
104 *ptr = 19; /* { dg-warning "use after 'free' of 'ptr'" } */
105 // TODO: two warnings here: one is from sm-malloc, the other from region model
106
107 if (y)
108 free (ptr); /* No double-'free' warning: we've already attempted
109 to dereference it above. */
110 return *ptr; /* { dg-warning "use after 'free' of 'ptr'" "use-after-free" } */
111 /* { dg-warning "leak of 'ptr'" "leak" { target *-*-* } .-1 } */
112 }
113
114 /* "dereference of possibly-NULL 'ptr'". */
115 /* { dg-begin-multiline-output "" }
116 NN | *ptr = 42;
117 | ~~~~~^~~~
118 'test_3': events 1-2
119 NN | int *ptr = (int *)malloc (sizeof (int));
120 | ^~~~~~~~~~~~~~~~~~~~~
121 | |
122 | (1) this call could return NULL
123 NN | *ptr = 42;
124 | ~~~~~~~~~
125 | |
126 | (2) 'ptr' could be NULL: unchecked value from (1)
127 { dg-end-multiline-output "" } */
128
129 /* "use after 'free' of 'ptr'". */
130 /* { dg-begin-multiline-output "" }
131 NN | *ptr = 19;
132 | ~~~~~^~~~
133 'test_3': events 1-6
134 NN | int *ptr = (int *)malloc (sizeof (int));
135 | ^~~~~~~~~~~~~~~~~~~~~
136 | |
137 | (1) allocated here
138 NN | *ptr = 42;
139 | ~~~~~~~~~
140 | |
141 | (2) assuming 'ptr' is non-NULL
142 NN | if (x)
143 | ~
144 | |
145 | (3) following 'true' branch (when 'x != 0')...
146 NN | free (ptr);
147 | ~~~~~~~~~~
148 | |
149 | (4) ...to here
150 | (5) freed here
151 NN |
152 NN | *ptr = 19;
153 | ~~~~~~~~~
154 | |
155 | (6) use after 'free' of 'ptr'; freed at (5)
156 { dg-end-multiline-output "" } */
157
158 /* "use after 'free' of 'ptr'". */
159 /* { dg-begin-multiline-output "" }
160 NN | return *ptr;
161 | ^~~~
162 'test_3': events 1-8
163 NN | int *ptr = (int *)malloc (sizeof (int));
164 | ^~~~~~~~~~~~~~~~~~~~~
165 | |
166 | (1) allocated here
167 NN | *ptr = 42;
168 | ~~~~~~~~~
169 | |
170 | (2) assuming 'ptr' is non-NULL
171 NN | if (x)
172 | ~
173 | |
174 | (3) following 'false' branch (when 'x == 0')...
175 ......
176 NN | *ptr = 19;
177 | ~~~~~~~~~
178 | |
179 | (4) ...to here
180 ......
181 NN | if (y)
182 | ~
183 | |
184 | (5) following 'true' branch (when 'y != 0')...
185 NN | free (ptr);
186 | ~~~~~~~~~~
187 | |
188 | (6) ...to here
189 | (7) freed here
190 NN |
191 NN | return *ptr;
192 | ~~~~
193 | |
194 | (8) use after 'free' of 'ptr'; freed at (7)
195 { dg-end-multiline-output "" } */
196
197 /* "leak of 'ptr'". */
198 /* { dg-begin-multiline-output "" }
199 NN | return *ptr;
200 | ^~~~
201 'test_3': events 1-7
202 NN | int *ptr = (int *)malloc (sizeof (int));
203 | ^~~~~~~~~~~~~~~~~~~~~
204 | |
205 | (1) allocated here
206 NN | *ptr = 42;
207 | ~~~~~~~~~
208 | |
209 | (2) assuming 'ptr' is non-NULL
210 NN | if (x)
211 | ~
212 | |
213 | (3) following 'false' branch (when 'x == 0')...
214 ......
215 NN | *ptr = 19;
216 | ~~~~~~~~~
217 | |
218 | (4) ...to here
219 ......
220 NN | if (y)
221 | ~
222 | |
223 | (5) following 'false' branch (when 'y == 0')...
224 ......
225 NN | return *ptr;
226 | ~~~~
227 | |
228 | (6) ...to here
229 | (7) 'ptr' leaks here; was allocated at (1)
230 { dg-end-multiline-output "" } */