1 // RUN: %clang_analyze_cc1 -verify %s \
2 // RUN: -analyzer-checker=core \
3 // RUN: -analyzer-checker=unix.Errno \
4 // RUN: -analyzer-checker=unix.Stream \
5 // RUN: -analyzer-checker=unix.StdCLibraryFunctions \
6 // RUN: -analyzer-config unix.StdCLibraryFunctions:ModelPOSIX=true \
7 // RUN: -analyzer-checker=debug.ExprInspection
9 // enable only StdCLibraryFunctions checker
10 // RUN: %clang_analyze_cc1 -verify %s \
11 // RUN: -analyzer-checker=core \
12 // RUN: -analyzer-checker=unix.Errno \
13 // RUN: -analyzer-checker=unix.StdCLibraryFunctions \
14 // RUN: -analyzer-config unix.StdCLibraryFunctions:ModelPOSIX=true \
15 // RUN: -analyzer-checker=debug.ExprInspection
17 #include "Inputs/system-header-simulator.h"
18 #include "Inputs/errno_var.h"
20 void clang_analyzer_eval(int);
22 const char *WBuf
= "123456789";
25 void test_freopen(FILE *F
) {
26 F
= freopen("xxx", "w", F
);
28 if (errno
) {} // expected-warning{{undefined}}
30 clang_analyzer_eval(errno
!= 0); // expected-warning {{TRUE}}
34 void test_fread(FILE *F
) {
35 size_t Ret
= fread(RBuf
, 1, 10, F
);
36 clang_analyzer_eval(F
!= NULL
); // expected-warning {{TRUE}}
38 if (errno
) {} // expected-warning{{undefined}}
40 clang_analyzer_eval(Ret
< 10); // expected-warning {{TRUE}}
41 clang_analyzer_eval(errno
!= 0); // expected-warning {{TRUE}}
43 clang_analyzer_eval(feof(F
)); // expected-warning {{UNKNOWN}}
44 clang_analyzer_eval(ferror(F
)); // expected-warning {{UNKNOWN}}
47 void test_fwrite(FILE *F
) {
48 size_t Ret
= fwrite(WBuf
, 1, 10, F
);
49 clang_analyzer_eval(F
!= NULL
); // expected-warning {{TRUE}}
51 if (errno
) {} // expected-warning{{undefined}}
53 clang_analyzer_eval(Ret
< 10); // expected-warning {{TRUE}}
54 clang_analyzer_eval(errno
!= 0); // expected-warning {{TRUE}}
56 clang_analyzer_eval(feof(F
)); // expected-warning {{UNKNOWN}}
57 clang_analyzer_eval(ferror(F
)); // expected-warning {{UNKNOWN}}
60 void test_fgetc(FILE *F
) {
62 clang_analyzer_eval(F
!= NULL
); // expected-warning {{TRUE}}
64 if (errno
) {} // expected-warning {{undefined}}
66 clang_analyzer_eval(errno
!= 0); // expected-warning {{TRUE}}
67 // expected-warning@-1 {{FALSE}}
69 clang_analyzer_eval(feof(F
)); // expected-warning {{UNKNOWN}}
70 clang_analyzer_eval(ferror(F
)); // expected-warning {{UNKNOWN}}
73 void test_fputc(FILE *F
) {
74 int Ret
= fputc('a', F
);
75 clang_analyzer_eval(F
!= NULL
); // expected-warning {{TRUE}}
77 clang_analyzer_eval(Ret
== 'a'); // expected-warning {{TRUE}}
78 if (errno
) {} // expected-warning {{undefined}}
80 clang_analyzer_eval(errno
!= 0); // expected-warning {{TRUE}}
82 clang_analyzer_eval(feof(F
)); // expected-warning {{UNKNOWN}}
83 clang_analyzer_eval(ferror(F
)); // expected-warning {{UNKNOWN}}
86 void test_fgets(char *Buf
, int N
, FILE *F
) {
87 char *Ret
= fgets(Buf
, N
, F
);
88 clang_analyzer_eval(F
!= NULL
); // expected-warning {{TRUE}}
89 clang_analyzer_eval(Buf
!= NULL
); // expected-warning {{TRUE}}
90 clang_analyzer_eval(N
>= 0); // expected-warning {{TRUE}}
92 if (errno
) {} // expected-warning {{undefined}}
94 clang_analyzer_eval(Ret
== 0); // expected-warning {{TRUE}}
95 clang_analyzer_eval(errno
!= 0); // expected-warning {{TRUE}}
96 // expected-warning@-1 {{FALSE}}
98 clang_analyzer_eval(feof(F
)); // expected-warning {{UNKNOWN}}
99 clang_analyzer_eval(ferror(F
)); // expected-warning {{UNKNOWN}}
102 Ret
= fgets(Buf1
, 11, F
); // expected-warning {{The 1st argument to 'fgets' is a buffer with size 10}}
105 void test_fgets_bufsize(FILE *F
) {
107 fgets(Buf
, 11, F
); // expected-warning {{The 1st argument to 'fgets' is a buffer with size 10}}
110 void test_fputs(char *Buf
, FILE *F
) {
111 int Ret
= fputs(Buf
, F
);
112 clang_analyzer_eval(F
!= NULL
); // expected-warning {{TRUE}}
113 clang_analyzer_eval(Buf
!= NULL
); // expected-warning {{TRUE}}
115 if (errno
) {} // expected-warning {{undefined}}
117 clang_analyzer_eval(Ret
== EOF
); // expected-warning {{TRUE}}
118 clang_analyzer_eval(errno
!= 0); // expected-warning {{TRUE}}
120 clang_analyzer_eval(feof(F
)); // expected-warning {{UNKNOWN}}
121 clang_analyzer_eval(ferror(F
)); // expected-warning {{UNKNOWN}}
124 void test_ungetc(FILE *F
) {
125 int Ret
= ungetc('X', F
);
126 clang_analyzer_eval(F
!= NULL
); // expected-warning {{TRUE}}
128 if (errno
) {} // expected-warning {{undefined}}
130 clang_analyzer_eval(Ret
== EOF
); // expected-warning {{TRUE}}
131 clang_analyzer_eval(errno
!= 0); // expected-warning {{TRUE}}
133 clang_analyzer_eval(feof(F
)); // expected-warning {{UNKNOWN}}
134 clang_analyzer_eval(ferror(F
)); // expected-warning {{UNKNOWN}}
137 void test_ungetc_EOF(FILE *F
, int C
) {
138 int Ret
= ungetc(EOF
, F
);
139 clang_analyzer_eval(F
!= NULL
); // expected-warning {{TRUE}}
140 clang_analyzer_eval(Ret
== EOF
); // expected-warning {{TRUE}}
141 clang_analyzer_eval(errno
!= 0); // expected-warning {{TRUE}}
144 clang_analyzer_eval(C
== EOF
); // expected-warning {{TRUE}}
145 // expected-warning@-1{{FALSE}}
149 void test_fclose(FILE *F
) {
151 clang_analyzer_eval(F
!= NULL
); // expected-warning {{TRUE}}
153 if (errno
) {} // expected-warning{{undefined}}
155 clang_analyzer_eval(Ret
== EOF
); // expected-warning {{TRUE}}
156 clang_analyzer_eval(errno
!= 0); // expected-warning {{TRUE}}
158 clang_analyzer_eval(feof(F
)); // expected-warning {{UNKNOWN}}
159 clang_analyzer_eval(ferror(F
)); // expected-warning {{UNKNOWN}}
162 void test_fseek(FILE *F
) {
163 int Ret
= fseek(F
, SEEK_SET
, 1);
164 clang_analyzer_eval(F
!= NULL
); // expected-warning {{TRUE}}
166 if (errno
) {} // expected-warning{{undefined}}
168 clang_analyzer_eval(Ret
== -1); // expected-warning {{TRUE}}
169 clang_analyzer_eval(errno
!= 0); // expected-warning {{TRUE}}
171 clang_analyzer_eval(feof(F
)); // expected-warning {{UNKNOWN}}
172 clang_analyzer_eval(ferror(F
)); // expected-warning {{UNKNOWN}}
175 void check_fgetpos(FILE *F
) {
178 int Ret
= fgetpos(F
, &Pos
);
179 clang_analyzer_eval(F
!= NULL
); // expected-warning {{TRUE}}
181 clang_analyzer_eval(errno
!= 0); // expected-warning{{TRUE}}
183 clang_analyzer_eval(errno
== 0); // expected-warning{{TRUE}}
184 // expected-warning@-1{{FALSE}}
185 if (errno
) {} // no-warning
186 clang_analyzer_eval(feof(F
)); // expected-warning {{UNKNOWN}}
187 clang_analyzer_eval(ferror(F
)); // expected-warning {{UNKNOWN}}
190 void check_fsetpos(FILE *F
) {
193 int Ret
= fsetpos(F
, &Pos
);
194 clang_analyzer_eval(F
!= NULL
); // expected-warning {{TRUE}}
196 clang_analyzer_eval(errno
!= 0); // expected-warning{{TRUE}}
198 clang_analyzer_eval(errno
== 0); // expected-warning{{TRUE}}
199 // expected-warning@-1{{FALSE}}
200 if (errno
) {} // no-warning
201 clang_analyzer_eval(feof(F
)); // expected-warning {{UNKNOWN}}
202 clang_analyzer_eval(ferror(F
)); // expected-warning {{UNKNOWN}}
205 void check_ftell(FILE *F
) {
208 clang_analyzer_eval(F
!= NULL
); // expected-warning {{TRUE}}
210 clang_analyzer_eval(errno
!= 0); // expected-warning{{TRUE}}
212 clang_analyzer_eval(errno
== 0); // expected-warning{{TRUE}}
213 // expected-warning@-1{{FALSE}}
214 clang_analyzer_eval(Ret
>= 0); // expected-warning{{TRUE}}
216 if (errno
) {} // no-warning
217 clang_analyzer_eval(feof(F
)); // expected-warning {{UNKNOWN}}
218 clang_analyzer_eval(ferror(F
)); // expected-warning {{UNKNOWN}}
221 void test_rewind(FILE *F
) {
224 clang_analyzer_eval(F
!= NULL
); // expected-warning{{TRUE}}
225 clang_analyzer_eval(errno
== 0); // expected-warning{{TRUE}}
226 // expected-warning@-1{{FALSE}}
230 void test_fflush(FILE *F
) {
233 clang_analyzer_eval(F
!= NULL
); // expected-warning{{TRUE}}
234 // expected-warning@-1{{FALSE}}
236 clang_analyzer_eval(errno
!= 0); // expected-warning{{TRUE}}
238 clang_analyzer_eval(Ret
== 0); // expected-warning{{TRUE}}
239 clang_analyzer_eval(errno
== 0); // expected-warning{{TRUE}}
240 // expected-warning@-1{{FALSE}}
244 void test_feof(FILE *F
) {
247 clang_analyzer_eval(F
!= NULL
); // expected-warning{{TRUE}}
248 if (errno
) {} // no-warning
249 clang_analyzer_eval(errno
== 0); // expected-warning{{TRUE}}
250 // expected-warning@-1{{FALSE}}
253 void test_ferror(FILE *F
) {
256 clang_analyzer_eval(F
!= NULL
); // expected-warning{{TRUE}}
257 if (errno
) {} // no-warning
258 clang_analyzer_eval(errno
== 0); // expected-warning{{TRUE}}
259 // expected-warning@-1{{FALSE}}
262 void test_clearerr(FILE *F
) {
265 clang_analyzer_eval(F
!= NULL
); // expected-warning{{TRUE}}
266 if (errno
) {} // no-warning
267 clang_analyzer_eval(errno
== 0); // expected-warning{{TRUE}}
268 // expected-warning@-1{{FALSE}}
271 void test_fileno(FILE *F
) {
274 clang_analyzer_eval(F
!= NULL
); // expected-warning{{TRUE}}
275 clang_analyzer_eval(A
>= 0); // expected-warning{{TRUE}}
276 if (errno
) {} // no-warning
277 clang_analyzer_eval(errno
== 0); // expected-warning{{TRUE}}
278 // expected-warning@-1{{FALSE}}
281 void freadwrite_zerosize(FILE *F
) {
282 fwrite(WBuf
, 1, 0, F
);
283 clang_analyzer_eval(feof(F
)); // expected-warning {{UNKNOWN}}
284 clang_analyzer_eval(ferror(F
)); // expected-warning {{UNKNOWN}}
285 fwrite(WBuf
, 0, 1, F
);
286 clang_analyzer_eval(feof(F
)); // expected-warning {{UNKNOWN}}
287 clang_analyzer_eval(ferror(F
)); // expected-warning {{UNKNOWN}}
288 fread(RBuf
, 1, 0, F
);
289 clang_analyzer_eval(feof(F
)); // expected-warning {{UNKNOWN}}
290 clang_analyzer_eval(ferror(F
)); // expected-warning {{UNKNOWN}}
291 fread(RBuf
, 0, 1, F
);
292 clang_analyzer_eval(feof(F
)); // expected-warning {{UNKNOWN}}
293 clang_analyzer_eval(ferror(F
)); // expected-warning {{UNKNOWN}}
296 void freadwrite_zerosize_errno(FILE *F
, int A
) {
299 fwrite(WBuf
, 1, 0, F
);
300 if (errno
) {} // expected-warning{{undefined}}
303 fwrite(WBuf
, 0, 1, F
);
304 if (errno
) {} // expected-warning{{undefined}}
307 fread(RBuf
, 1, 0, F
);
308 if (errno
) {} // expected-warning{{undefined}}
311 fread(RBuf
, 0, 1, F
);
312 if (errno
) {} // expected-warning{{undefined}}