1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.unix.Stream -analyzer-output text -verify %s
2 
3 #include "Inputs/system-header-simulator.h"
4 
check_note_at_correct_open()5 void check_note_at_correct_open() {
6   FILE *F1 = tmpfile(); // expected-note {{Stream opened here}}
7   if (!F1)
8     // expected-note@-1 {{'F1' is non-null}}
9     // expected-note@-2 {{Taking false branch}}
10     return;
11   FILE *F2 = tmpfile();
12   if (!F2) {
13     // expected-note@-1 {{'F2' is non-null}}
14     // expected-note@-2 {{Taking false branch}}
15     fclose(F1);
16     return;
17   }
18   rewind(F2);
19   fclose(F2);
20   rewind(F1);
21 }
22 // expected-warning@-1 {{Opened stream never closed. Potential resource leak}}
23 // expected-note@-2 {{Opened stream never closed. Potential resource leak}}
24 
check_note_fopen()25 void check_note_fopen() {
26   FILE *F = fopen("file", "r"); // expected-note {{Stream opened here}}
27   if (!F)
28     // expected-note@-1 {{'F' is non-null}}
29     // expected-note@-2 {{Taking false branch}}
30     return;
31 }
32 // expected-warning@-1 {{Opened stream never closed. Potential resource leak}}
33 // expected-note@-2 {{Opened stream never closed. Potential resource leak}}
34 
check_note_freopen()35 void check_note_freopen() {
36   FILE *F = fopen("file", "r"); // expected-note {{Stream opened here}}
37   if (!F)
38     // expected-note@-1 {{'F' is non-null}}
39     // expected-note@-2 {{Taking false branch}}
40     return;
41   F = freopen(0, "w", F); // expected-note {{Stream reopened here}}
42   if (!F)
43     // expected-note@-1 {{'F' is non-null}}
44     // expected-note@-2 {{Taking false branch}}
45     return;
46 }
47 // expected-warning@-1 {{Opened stream never closed. Potential resource leak}}
48 // expected-note@-2 {{Opened stream never closed. Potential resource leak}}
49 
check_note_leak_2(int c)50 void check_note_leak_2(int c) {
51   FILE *F1 = fopen("foo1.c", "r"); // expected-note {{Stream opened here}}
52   if (!F1)
53     // expected-note@-1 {{'F1' is non-null}}
54     // expected-note@-2 {{Taking false branch}}
55     // expected-note@-3 {{'F1' is non-null}}
56     // expected-note@-4 {{Taking false branch}}
57     return;
58   FILE *F2 = fopen("foo2.c", "r"); // expected-note {{Stream opened here}}
59   if (!F2) {
60     // expected-note@-1 {{'F2' is non-null}}
61     // expected-note@-2 {{Taking false branch}}
62     // expected-note@-3 {{'F2' is non-null}}
63     // expected-note@-4 {{Taking false branch}}
64     fclose(F1);
65     return;
66   }
67   if (c)
68     // expected-note@-1 {{Assuming 'c' is not equal to 0}}
69     // expected-note@-2 {{Taking true branch}}
70     // expected-note@-3 {{Assuming 'c' is not equal to 0}}
71     // expected-note@-4 {{Taking true branch}}
72     return;
73   // expected-warning@-1 {{Opened stream never closed. Potential resource leak}}
74   // expected-note@-2 {{Opened stream never closed. Potential resource leak}}
75   // expected-warning@-3 {{Opened stream never closed. Potential resource leak}}
76   // expected-note@-4 {{Opened stream never closed. Potential resource leak}}
77   fclose(F1);
78   fclose(F2);
79 }
80