1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.unix.Stream -analyzer-output text -verify %s
3 #include "Inputs/system-header-simulator.h"
5 void check_note_at_correct_open(void) {
6 FILE *F1
= tmpfile(); // expected-note {{Stream opened here}}
8 // expected-note@-1 {{'F1' is non-null}}
9 // expected-note@-2 {{Taking false branch}}
13 // expected-note@-1 {{'F2' is non-null}}
14 // expected-note@-2 {{Taking false branch}}
22 // expected-warning@-1 {{Opened stream never closed. Potential resource leak}}
23 // expected-note@-2 {{Opened stream never closed. Potential resource leak}}
25 void check_note_fopen(void) {
26 FILE *F
= fopen("file", "r"); // expected-note {{Stream opened here}}
28 // expected-note@-1 {{'F' is non-null}}
29 // expected-note@-2 {{Taking false branch}}
32 // expected-warning@-1 {{Opened stream never closed. Potential resource leak}}
33 // expected-note@-2 {{Opened stream never closed. Potential resource leak}}
35 void check_note_freopen(void) {
36 FILE *F
= fopen("file", "r"); // expected-note {{Stream opened here}}
38 // expected-note@-1 {{'F' is non-null}}
39 // expected-note@-2 {{Taking false branch}}
41 F
= freopen(0, "w", F
); // expected-note {{Stream reopened here}}
43 // expected-note@-1 {{'F' is non-null}}
44 // expected-note@-2 {{Taking false branch}}
47 // expected-warning@-1 {{Opened stream never closed. Potential resource leak}}
48 // expected-note@-2 {{Opened stream never closed. Potential resource leak}}
50 void check_note_leak_2(int c
) {
51 FILE *F1
= fopen("foo1.c", "r"); // expected-note {{Stream opened here}}
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}}
58 FILE *F2
= fopen("foo2.c", "r"); // expected-note {{Stream opened here}}
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}}
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}}
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}}
81 void check_track_null(void) {
83 F
= fopen("foo1.c", "r"); // expected-note {{Value assigned to 'F'}} expected-note {{Assuming pointer value is null}}
84 if (F
!= NULL
) { // expected-note {{Taking false branch}} expected-note {{'F' is equal to NULL}}
88 fclose(F
); // expected-warning {{Stream pointer might be NULL}}
89 // expected-note@-1 {{Stream pointer might be NULL}}
92 void check_eof_notes_feof_after_feof(void) {
95 F
= fopen("foo1.c", "r");
96 if (F
== NULL
) { // expected-note {{Taking false branch}} expected-note {{'F' is not equal to NULL}}
100 if (feof(F
)) { // expected-note {{Taking true branch}}
102 fread(Buf
, 1, 1, F
); // expected-note {{Assuming stream reaches end-of-file here}}
103 if (feof(F
)) { // expected-note {{Taking true branch}}
104 fread(Buf
, 1, 1, F
); // expected-warning {{Read function called when stream is in EOF state. Function has no effect}}
105 // expected-note@-1 {{Read function called when stream is in EOF state. Function has no effect}}
111 void check_eof_notes_feof_after_no_feof(void) {
114 F
= fopen("foo1.c", "r");
115 if (F
== NULL
) { // expected-note {{Taking false branch}} expected-note {{'F' is not equal to NULL}}
119 if (feof(F
)) { // expected-note {{Taking false branch}}
122 } else if (ferror(F
)) { // expected-note {{Taking false branch}}
126 fread(Buf
, 1, 1, F
); // expected-note {{Assuming stream reaches end-of-file here}}
127 if (feof(F
)) { // expected-note {{Taking true branch}}
128 fread(Buf
, 1, 1, F
); // expected-warning {{Read function called when stream is in EOF state. Function has no effect}}
129 // expected-note@-1 {{Read function called when stream is in EOF state. Function has no effect}}
134 void check_eof_notes_feof_or_no_error(void) {
137 F
= fopen("foo1.c", "r");
138 if (F
== NULL
) // expected-note {{Taking false branch}} expected-note {{'F' is not equal to NULL}}
140 int RRet
= fread(Buf
, 1, 1, F
); // expected-note {{Assuming stream reaches end-of-file here}}
141 if (ferror(F
)) { // expected-note {{Taking false branch}}
143 fread(Buf
, 1, 1, F
); // expected-warning {{Read function called when stream is in EOF state. Function has no effect}}
144 // expected-note@-1 {{Read function called when stream is in EOF state. Function has no effect}}