[clang] Handle __declspec() attributes in using
[llvm-project.git] / clang / test / Analysis / stream.c
blob44373e4ad9f6ccb9bd09ba40614c0a6791be8497
1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.unix.Stream -verify %s
3 #include "Inputs/system-header-simulator.h"
5 void f_seek(void) {
6 FILE *p = fopen("foo", "r");
7 if (!p)
8 return;
9 fseek(p, 1, SEEK_SET); // no-warning
10 fseek(p, 1, 3); // expected-warning {{The whence argument to fseek() should be SEEK_SET, SEEK_END, or SEEK_CUR}}
11 fclose(p);
14 void f_double_close(void) {
15 FILE *p = fopen("foo", "r");
16 if (!p)
17 return;
18 fclose(p);
19 fclose(p); // expected-warning {{Stream might be already closed}}
22 void f_double_close_alias(void) {
23 FILE *p1 = fopen("foo", "r");
24 if (!p1)
25 return;
26 FILE *p2 = p1;
27 fclose(p1);
28 fclose(p2); // expected-warning {{Stream might be already closed}}
31 void f_use_after_close(void) {
32 FILE *p = fopen("foo", "r");
33 if (!p)
34 return;
35 fclose(p);
36 clearerr(p); // expected-warning {{Stream might be already closed}}
39 void f_open_after_close(void) {
40 FILE *p = fopen("foo", "r");
41 if (!p)
42 return;
43 fclose(p);
44 p = fopen("foo", "r");
45 if (!p)
46 return;
47 fclose(p);
50 void f_reopen_after_close(void) {
51 FILE *p = fopen("foo", "r");
52 if (!p)
53 return;
54 fclose(p);
55 // Allow reopen after close.
56 p = freopen("foo", "w", p);
57 if (!p)
58 return;
59 fclose(p);
62 void f_leak(int c) {
63 FILE *p = fopen("foo.c", "r");
64 if (!p)
65 return;
66 if(c)
67 return; // expected-warning {{Opened stream never closed. Potential resource leak}}
68 fclose(p);
71 FILE *f_null_checked(void) {
72 FILE *p = fopen("foo.c", "r");
73 if (p)
74 return p; // no-warning
75 else
76 return 0;
79 void pr7831(FILE *fp) {
80 fclose(fp); // no-warning
83 // PR 8081 - null pointer crash when 'whence' is not an integer constant
84 void pr8081(FILE *stream, long offset, int whence) {
85 fseek(stream, offset, whence);
88 void check_freopen_1(void) {
89 FILE *f1 = freopen("foo.c", "r", (FILE *)0); // Not reported by the stream checker.
90 f1 = freopen(0, "w", (FILE *)0x123456); // Do not report this as error.
93 void check_freopen_2(void) {
94 FILE *f1 = fopen("foo.c", "r");
95 if (f1) {
96 FILE *f2 = freopen(0, "w", f1);
97 if (f2) {
98 // Check if f1 and f2 point to the same stream.
99 fclose(f1);
100 fclose(f2); // expected-warning {{Stream might be already closed.}}
101 } else {
102 // Reopen failed.
103 // f1 is non-NULL but points to a possibly invalid stream.
104 rewind(f1); // expected-warning {{Stream might be invalid}}
105 // f2 is NULL but the previous error stops the checker.
106 rewind(f2);
111 void check_freopen_3(void) {
112 FILE *f1 = fopen("foo.c", "r");
113 if (f1) {
114 // Unchecked result of freopen.
115 // The f1 may be invalid after this call.
116 freopen(0, "w", f1);
117 rewind(f1); // expected-warning {{Stream might be invalid}}
118 fclose(f1);
122 extern FILE *GlobalF;
123 extern void takeFile(FILE *);
125 void check_escape1(void) {
126 FILE *F = tmpfile();
127 if (!F)
128 return;
129 fwrite("1", 1, 1, F); // may fail
130 GlobalF = F;
131 fwrite("1", 1, 1, F); // no warning
134 void check_escape2(void) {
135 FILE *F = tmpfile();
136 if (!F)
137 return;
138 fwrite("1", 1, 1, F); // may fail
139 takeFile(F);
140 fwrite("1", 1, 1, F); // no warning
143 void check_escape3(void) {
144 FILE *F = tmpfile();
145 if (!F)
146 return;
147 takeFile(F);
148 F = freopen(0, "w", F);
149 if (!F)
150 return;
151 fwrite("1", 1, 1, F); // may fail
152 fwrite("1", 1, 1, F); // no warning
155 void check_escape4(void) {
156 FILE *F = tmpfile();
157 if (!F)
158 return;
159 fwrite("1", 1, 1, F); // may fail
161 // no escape at (non-StreamChecker-handled) system call
162 // FIXME: all such calls should be handled by the checker
163 fprintf(F, "0");
165 fwrite("1", 1, 1, F); // expected-warning {{might be 'indeterminate'}}
166 fclose(F);
169 int Test;
170 _Noreturn void handle_error(void);
172 void check_leak_noreturn_1(void) {
173 FILE *F1 = tmpfile();
174 if (!F1)
175 return;
176 if (Test == 1) {
177 handle_error(); // no warning
179 rewind(F1);
180 } // expected-warning {{Opened stream never closed. Potential resource leak}}
182 // Check that "location uniqueing" works.
183 // This results in reporting only one occurence of resource leak for a stream.
184 void check_leak_noreturn_2(void) {
185 FILE *F1 = tmpfile();
186 if (!F1)
187 return;
188 if (Test == 1) {
189 return; // no warning
191 rewind(F1);
192 } // expected-warning {{Opened stream never closed. Potential resource leak}}
193 // FIXME: This warning should be placed at the `return` above.
194 // See https://reviews.llvm.org/D83120 about details.