1 #include <stdio.h>
2
3 void
test_1(const char * path)4 test_1 (const char *path)
5 {
6 FILE *f = fopen (path, "r"); /* { dg-message "opened here" } */
7 if (!f)
8 return;
9
10 fclose (f); /* { dg-message "\\(4\\) \\.\\.\\.to here" "to here" } */
11 /* { dg-message "\\(5\\) first 'fclose' here" "first fclose" { target *-*-* } .-1 } */
12 fclose (f); /* { dg-warning "double 'fclose' of FILE 'f'" "warning" } */
13 /* { dg-message "second 'fclose' here; first 'fclose' was at \\(5\\)" "second fclose" { target *-*-* } .-1 } */
14 }
15
16 void
test_2(const char * src,const char * dst)17 test_2 (const char *src, const char *dst)
18 {
19 FILE *f_in = fopen (src, "r"); /* { dg-message "\\(1\\) opened here" } */
20 if (!f_in)
21 return;
22
23 FILE *f_out = fopen (src, "w");
24 if (!f_out)
25 return; /* { dg-warning "leak of FILE 'f_in'" "warning" } */
26 /* { dg-message "\\(7\\) 'f_in' leaks here; was opened at \\(1\\)" "event" { target *-*-* } .-1 } */
27
28 fclose (f_out);
29 fclose (f_in);
30 }
31
32 void
test_3(const char * path)33 test_3 (const char *path)
34 {
35 FILE *f = fopen (path, "r"); /* { dg-message "opened here" } */
36 return; /* { dg-warning "leak of FILE 'f'" } */
37 }
38
39 void
test_4(const char * path)40 test_4 (const char *path)
41 {
42 FILE *f = fopen (path, "r"); /* { dg-message "opened here" } */
43
44 /* Ensure we know about common fns that are known to not close the
45 file (e.g. "fseek"). */
46 fseek (f, 1024, SEEK_SET);
47
48 return; /* { dg-warning "leak of FILE 'f'" } */
49 }
50