1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.unix.Stream -analyzer-output text \
3 // RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.unix.Stream,unix.StdCLibraryFunctions -analyzer-output text \
4 // RUN: -analyzer-config unix.StdCLibraryFunctions:ModelPOSIX=true -verify=expected,stdargs %s
6 #include "Inputs/system-header-simulator.h"
8 void check_note_at_correct_open(void) {
9 FILE *F1
= tmpfile(); // expected-note {{Stream opened here}}
10 // stdargs-note@-1 {{'tmpfile' is successful}}
12 // expected-note@-1 {{'F1' is non-null}}
13 // expected-note@-2 {{Taking false branch}}
17 // expected-note@-1 {{'F2' is non-null}}
18 // expected-note@-2 {{Taking false branch}}
26 // expected-warning@-1 {{Opened stream never closed. Potential resource leak}}
27 // expected-note@-2 {{Opened stream never closed. Potential resource leak}}
29 void check_note_fopen(void) {
30 FILE *F
= fopen("file", "r"); // expected-note {{Stream opened here}}
31 // stdargs-note@-1 {{'fopen' is successful}}
33 // expected-note@-1 {{'F' is non-null}}
34 // expected-note@-2 {{Taking false branch}}
37 // expected-warning@-1 {{Opened stream never closed. Potential resource leak}}
38 // expected-note@-2 {{Opened stream never closed. Potential resource leak}}
40 void check_note_freopen(void) {
41 FILE *F
= fopen("file", "r"); // expected-note {{Stream opened here}}
42 // stdargs-note@-1 {{'fopen' is successful}}
44 // expected-note@-1 {{'F' is non-null}}
45 // expected-note@-2 {{Taking false branch}}
47 F
= freopen(0, "w", F
); // expected-note {{Stream reopened here}}
48 // stdargs-note@-1 {{'freopen' is successful}}
50 // expected-note@-1 {{'F' is non-null}}
51 // expected-note@-2 {{Taking false branch}}
54 // expected-warning@-1 {{Opened stream never closed. Potential resource leak}}
55 // expected-note@-2 {{Opened stream never closed. Potential resource leak}}
57 void check_note_leak_2(int c
) {
58 FILE *F1
= fopen("foo1.c", "r"); // expected-note {{Stream opened here}}
59 // stdargs-note@-1 {{'fopen' is successful}}
61 // expected-note@-1 {{'F1' is non-null}}
62 // expected-note@-2 {{Taking false branch}}
63 // expected-note@-3 {{'F1' is non-null}}
64 // expected-note@-4 {{Taking false branch}}
66 FILE *F2
= fopen("foo2.c", "r"); // expected-note {{Stream opened here}}
67 // stdargs-note@-1 {{'fopen' is successful}}
69 // expected-note@-1 {{'F2' is non-null}}
70 // expected-note@-2 {{Taking false branch}}
71 // expected-note@-3 {{'F2' is non-null}}
72 // expected-note@-4 {{Taking false branch}}
77 // expected-note@-1 {{Assuming 'c' is not equal to 0}}
78 // expected-note@-2 {{Taking true branch}}
79 // expected-note@-3 {{Assuming 'c' is not equal to 0}}
80 // expected-note@-4 {{Taking true branch}}
82 // expected-warning@-1 {{Opened stream never closed. Potential resource leak}}
83 // expected-note@-2 {{Opened stream never closed. Potential resource leak}}
84 // expected-warning@-3 {{Opened stream never closed. Potential resource leak}}
85 // expected-note@-4 {{Opened stream never closed. Potential resource leak}}
90 void check_track_null(void) {
92 F
= fopen("foo1.c", "r"); // expected-note {{Value assigned to 'F'}} expected-note {{Assuming pointer value is null}}
93 // stdargs-note@-1 {{'fopen' fails}}
94 if (F
!= NULL
) { // expected-note {{Taking false branch}} expected-note {{'F' is equal to NULL}}
98 fclose(F
); // expected-warning {{Stream pointer might be NULL}}
99 // expected-note@-1 {{Stream pointer might be NULL}}
102 void check_eof_notes_feof_after_feof(void) {
105 F
= fopen("foo1.c", "r");
106 if (F
== NULL
) { // expected-note {{Taking false branch}} expected-note {{'F' is not equal to NULL}}
110 if (feof(F
)) { // expected-note {{Taking true branch}}
112 fread(Buf
, 1, 1, F
); // expected-note {{Assuming stream reaches end-of-file here}}
113 if (feof(F
)) { // expected-note {{Taking true branch}}
114 fread(Buf
, 1, 1, F
); // expected-warning {{Read function called when stream is in EOF state. Function has no effect}}
115 // expected-note@-1 {{Read function called when stream is in EOF state. Function has no effect}}
121 void check_eof_notes_feof_after_no_feof(void) {
124 F
= fopen("foo1.c", "r");
125 if (F
== NULL
) { // expected-note {{Taking false branch}} expected-note {{'F' is not equal to NULL}}
129 if (feof(F
)) { // expected-note {{Taking false branch}}
132 } else if (ferror(F
)) { // expected-note {{Taking false branch}}
136 fread(Buf
, 1, 1, F
); // expected-note {{Assuming stream reaches end-of-file here}}
137 if (feof(F
)) { // expected-note {{Taking true branch}}
138 fread(Buf
, 1, 1, F
); // expected-warning {{Read function called when stream is in EOF state. Function has no effect}}
139 // expected-note@-1 {{Read function called when stream is in EOF state. Function has no effect}}
144 void check_eof_notes_feof_or_no_error(void) {
147 F
= fopen("foo1.c", "r");
148 if (F
== NULL
) // expected-note {{Taking false branch}} expected-note {{'F' is not equal to NULL}}
150 int RRet
= fread(Buf
, 1, 1, F
); // expected-note {{Assuming stream reaches end-of-file here}}
151 if (ferror(F
)) { // expected-note {{Taking false branch}}
153 fread(Buf
, 1, 1, F
); // expected-warning {{Read function called when stream is in EOF state. Function has no effect}}
154 // expected-note@-1 {{Read function called when stream is in EOF state. Function has no effect}}