1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.unix.Stream -verify %s
3 #include "Inputs/system-header-simulator.h"
5 void check_fread(void) {
7 fread(0, 0, 0, fp
); // expected-warning {{Stream pointer might be NULL}}
11 void check_fwrite(void) {
13 fwrite(0, 0, 0, fp
); // expected-warning {{Stream pointer might be NULL}}
17 void check_fseek(void) {
19 fseek(fp
, 0, 0); // expected-warning {{Stream pointer might be NULL}}
23 void check_ftell(void) {
25 ftell(fp
); // expected-warning {{Stream pointer might be NULL}}
29 void check_rewind(void) {
31 rewind(fp
); // expected-warning {{Stream pointer might be NULL}}
35 void check_fgetpos(void) {
38 fgetpos(fp
, &pos
); // expected-warning {{Stream pointer might be NULL}}
42 void check_fsetpos(void) {
45 fsetpos(fp
, &pos
); // expected-warning {{Stream pointer might be NULL}}
49 void check_clearerr(void) {
51 clearerr(fp
); // expected-warning {{Stream pointer might be NULL}}
55 void check_feof(void) {
57 feof(fp
); // expected-warning {{Stream pointer might be NULL}}
61 void check_ferror(void) {
63 ferror(fp
); // expected-warning {{Stream pointer might be NULL}}
67 void check_fileno(void) {
69 fileno(fp
); // expected-warning {{Stream pointer might be NULL}}
74 FILE *p
= fopen("foo", "r");
76 fread(buf
, 1, 1, p
); // expected-warning {{Stream pointer might be NULL}}
81 FILE *p
= fopen("foo", "r");
84 fseek(p
, 1, SEEK_SET
); // no-warning
85 fseek(p
, 1, 3); // expected-warning {{The whence argument to fseek() should be SEEK_SET, SEEK_END, or SEEK_CUR}}
89 void f_double_close(void) {
90 FILE *p
= fopen("foo", "r");
94 fclose(p
); // expected-warning {{Stream might be already closed}}
97 void f_double_close_alias(void) {
98 FILE *p1
= fopen("foo", "r");
103 fclose(p2
); // expected-warning {{Stream might be already closed}}
106 void f_use_after_close(void) {
107 FILE *p
= fopen("foo", "r");
111 clearerr(p
); // expected-warning {{Stream might be already closed}}
114 void f_open_after_close(void) {
115 FILE *p
= fopen("foo", "r");
119 p
= fopen("foo", "r");
125 void f_reopen_after_close(void) {
126 FILE *p
= fopen("foo", "r");
130 // Allow reopen after close.
131 p
= freopen("foo", "w", p
);
138 FILE *p
= fopen("foo.c", "r");
142 return; // expected-warning {{Opened stream never closed. Potential resource leak}}
146 FILE *f_null_checked(void) {
147 FILE *p
= fopen("foo.c", "r");
149 return p
; // no-warning
154 void pr7831(FILE *fp
) {
155 fclose(fp
); // no-warning
158 // PR 8081 - null pointer crash when 'whence' is not an integer constant
159 void pr8081(FILE *stream
, long offset
, int whence
) {
160 fseek(stream
, offset
, whence
);
163 void check_freopen_1(void) {
164 FILE *f1
= freopen("foo.c", "r", (FILE *)0); // expected-warning {{Stream pointer might be NULL}}
165 f1
= freopen(0, "w", (FILE *)0x123456); // Do not report this as error.
168 void check_freopen_2(void) {
169 FILE *f1
= fopen("foo.c", "r");
171 FILE *f2
= freopen(0, "w", f1
);
173 // Check if f1 and f2 point to the same stream.
175 fclose(f2
); // expected-warning {{Stream might be already closed.}}
178 // f1 is non-NULL but points to a possibly invalid stream.
179 rewind(f1
); // expected-warning {{Stream might be invalid}}
180 // f2 is NULL but the previous error stops the checker.
186 void check_freopen_3(void) {
187 FILE *f1
= fopen("foo.c", "r");
189 // Unchecked result of freopen.
190 // The f1 may be invalid after this call.
192 rewind(f1
); // expected-warning {{Stream might be invalid}}
197 extern FILE *GlobalF
;
198 extern void takeFile(FILE *);
200 void check_escape1(void) {
204 fwrite("1", 1, 1, F
); // may fail
206 fwrite("1", 1, 1, F
); // no warning
209 void check_escape2(void) {
213 fwrite("1", 1, 1, F
); // may fail
215 fwrite("1", 1, 1, F
); // no warning
218 void check_escape3(void) {
223 F
= freopen(0, "w", F
);
226 fwrite("1", 1, 1, F
); // may fail
227 fwrite("1", 1, 1, F
); // no warning
230 void check_escape4(void) {
234 fwrite("1", 1, 1, F
); // may fail
236 // no escape at (non-StreamChecker-handled) system call
237 // FIXME: all such calls should be handled by the checker
240 fwrite("1", 1, 1, F
); // expected-warning {{might be 'indeterminate'}}
245 _Noreturn
void handle_error(void);
247 void check_leak_noreturn_1(void) {
248 FILE *F1
= tmpfile();
252 handle_error(); // no warning
255 } // expected-warning {{Opened stream never closed. Potential resource leak}}
257 // Check that "location uniqueing" works.
258 // This results in reporting only one occurence of resource leak for a stream.
259 void check_leak_noreturn_2(void) {
260 FILE *F1
= tmpfile();
264 return; // no warning
267 } // expected-warning {{Opened stream never closed. Potential resource leak}}
268 // FIXME: This warning should be placed at the `return` above.
269 // See https://reviews.llvm.org/D83120 about details.