Debian Codegraph @

gcc/testsuite/c-c++-common/analyzer/malloc-paths-5.c

raw
1 /* { dg-skip-if "requires hosted libstdc++ for stdlib malloc" { ! hostedlib } } */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 extern void do_stuff (void);
7
8 int test (const char *filename, int flag)
9 {
10 FILE *f;
11 int *p, *q;
12 int i;
13
14 p = (int *)malloc (sizeof (int)); /* { dg-line malloc_of_p } */
15 if (!p) /* { dg-line test_of_p } */
16 {
17 free (p);
18 return -1;
19 }
20
21 q = (int *)malloc (sizeof (int)); /* { dg-line malloc_of_q } */
22 if (!q) /* { dg-line test_of_q } */
23 {
24 free (p); /* { dg-line first_free_of_p } */
25 /* oops: forgot the "return" here, so it falls through. */
26 }
27
28 do_stuff ();
29
30 free (p); /* { dg-line second_free_of_p } */
31 free (q);
32 return 0;
33
34 /* { dg-warning "double-'free' of 'p'" "warning" { target *-*-* } second_free_of_p } */
35 /* { dg-message "\\(1\\) allocated here" "event 1" { target *-*-* } malloc_of_p } */
36 /* { dg-message "\\(2\\) assuming 'p' is non-NULL" "event 2" { target *-*-* } test_of_p } */
37 /* { dg-message "\\(3\\) following 'false' branch \\(when 'p' is non-NULL\\)\\.\\.\\." "event 3" { target *-*-* } test_of_p } */
38 /* { dg-message "\\(4\\) \\.\\.\\.to here" "event 4" { target *-*-* } malloc_of_q } */
39 /* { dg-message "\\(5\\) following 'true' branch \\(when 'q' is NULL\\)\\.\\.\\." "event 5" { target *-*-* } test_of_q } */
40 /* { dg-message "\\(6\\) \\.\\.\\.to here" "event 6" { target *-*-* } first_free_of_p } */
41 /* { dg-message "\\(7\\) first 'free' here" "event 7" { target *-*-* } first_free_of_p } */
42 /* { dg-message "\\(8\\) second 'free' here; first 'free' was at \\(7\\)" "event 8" { target *-*-* } second_free_of_p } */
43
44 /* We don't care about the state changes to q. */
45 }