1 // RUN: %clang_analyze_cc1 -verify %s \
2 // RUN: -analyzer-checker=core \
3 // RUN: -analyzer-checker=unix.Stream \
4 // RUN: -analyzer-checker=debug.ExprInspection
6 #include "Inputs/system-header-simulator.h"
7 #include "Inputs/system-header-simulator-for-valist.h"
9 void clang_analyzer_eval(int);
10 void clang_analyzer_dump(int);
12 void test_fread(void) {
13 FILE *F
= fopen("file", "r+");
17 char Buf
[3] = {10, 10, 10};
19 // The check applies to success and failure.
20 clang_analyzer_dump(Buf
[0]); // expected-warning {{conj_$}} Should not preserve the previous value, thus should not be 10.
21 clang_analyzer_dump(Buf
[2]); // expected-warning {{conj_$}}
23 char Buf1
[3] = {10, 10, 10};
24 fread(Buf1
, 1, 3, F
); // expected-warning {{is in EOF state}}
25 clang_analyzer_dump(Buf1
[0]); // expected-warning {{10 S32b}}
26 clang_analyzer_dump(Buf1
[2]); // expected-warning {{10 S32b}}
32 void test_fwrite(void) {
33 FILE *F
= fopen("file", "r+");
37 char Buf
[3] = {10, 10, 10};
39 // The check applies to success and failure.
40 clang_analyzer_dump(Buf
[0]); // expected-warning {{10 S32b}}
41 clang_analyzer_dump(Buf
[2]); // expected-warning {{10 S32b}}
51 char Buf
[3] = {10, 10, 10};
53 // The check applies to success and failure.
54 clang_analyzer_dump(Buf
[0]); // expected-warning {{conj_$}} Should not preserve the previous value, thus should not be 10.
55 clang_analyzer_dump(Buf
[2]); // expected-warning {{conj_$}}
57 char Buf1
[3] = {10, 10, 10};
58 fgets(Buf1
, 3, F
); // expected-warning {{is in EOF state}}
59 clang_analyzer_dump(Buf1
[0]); // expected-warning {{10 S32b}}
60 clang_analyzer_dump(Buf1
[2]); // expected-warning {{10 S32b}}
73 // The check applies to success and failure.
74 clang_analyzer_dump(Buf
[0]); // expected-warning {{97 S32b}}
75 clang_analyzer_dump(Buf
[2]); // expected-warning {{97 S32b}}
76 clang_analyzer_dump(Buf
[3]); // expected-warning {{0 S32b}}
88 int Ret
= fscanf(F
, "%d %u", &a
, &b
);
90 clang_analyzer_dump(a
); // expected-warning {{conj_$}}
91 // FIXME: should be {{1 S32b}}.
92 clang_analyzer_dump(b
); // expected-warning {{conj_$}}
93 // FIXME: should be {{uninitialized value}}.
94 } else if (Ret
== 1) {
95 clang_analyzer_dump(a
); // expected-warning {{conj_$}}
96 clang_analyzer_dump(b
); // expected-warning {{conj_$}}
97 // FIXME: should be {{uninitialized value}}.
98 } else if (Ret
>= 2) {
99 clang_analyzer_dump(a
); // expected-warning {{conj_$}}
100 clang_analyzer_dump(b
); // expected-warning {{conj_$}}
101 clang_analyzer_eval(Ret
== 2); // expected-warning {{FALSE}} expected-warning {{TRUE}}
102 // FIXME: should be only TRUE.
104 clang_analyzer_dump(a
); // expected-warning {{1 S32b}}
105 clang_analyzer_dump(b
); // expected-warning {{uninitialized value}}
111 void test_getdelim(char *P
, size_t Sz
) {
118 ssize_t Ret
= getdelim(&P
, &Sz
, '\t', F
);
120 clang_analyzer_eval(P
== P1
); // expected-warning {{FALSE}} \
121 // expected-warning {{TRUE}}
122 clang_analyzer_eval(Sz
== Sz1
); // expected-warning {{FALSE}} \
123 // expected-warning {{TRUE}}
125 clang_analyzer_eval(P
== P1
); // expected-warning {{FALSE}} \
126 // expected-warning {{TRUE}}
127 clang_analyzer_eval(Sz
== Sz1
); // expected-warning {{FALSE}} \
128 // expected-warning {{TRUE}}
134 void test_fgetpos() {
140 int Ret
= fgetpos(F
, &Pos
);
142 clang_analyzer_dump(Pos
); // expected-warning {{conj_$}}
144 clang_analyzer_dump(Pos
); // expected-warning {{1 S32b}}
150 void test_fprintf() {
151 FILE *F1
= tmpfile();
156 char *output
= "HELLO";
157 int r
= fprintf(F1
, "%s\t%u\n", output
, a
);
158 // fprintf does not invalidate any of its input
159 // 69 is ascii for 'E'
160 clang_analyzer_dump(a
); // expected-warning {{42 S32b}}
161 clang_analyzer_dump(output
[1]); // expected-warning {{69 S32b}}
165 int test_vfscanf_inner(const char *fmt
, ...) {
166 FILE *F1
= tmpfile();
173 int r
= vfscanf(F1
, fmt
, ap
);
180 void test_vfscanf() {
183 int r
= test_vfscanf_inner("%d", &i
);
185 // i gets invalidated by the call to test_vfscanf_inner, not by vfscanf.
186 clang_analyzer_dump(i
); // expected-warning {{conj_$}}
187 clang_analyzer_dump(j
); // expected-warning {{43 S32b}}