1 // RUN: %clang_analyze_cc1 -verify %s \
2 // RUN: -analyzer-checker=core \
3 // RUN: -analyzer-checker=apiModeling.Errno \
4 // RUN: -analyzer-checker=debug.ExprInspection \
5 // RUN: -analyzer-checker=debug.ErrnoTest \
6 // RUN: -analyzer-checker=unix.Errno \
9 // RUN: %clang_analyze_cc1 -verify %s \
10 // RUN: -analyzer-checker=core \
11 // RUN: -analyzer-checker=apiModeling.Errno \
12 // RUN: -analyzer-checker=debug.ExprInspection \
13 // RUN: -analyzer-checker=debug.ErrnoTest \
14 // RUN: -analyzer-checker=unix.Errno \
17 #include "Inputs/system-header-simulator.h"
19 #include "Inputs/errno_var.h"
22 #include "Inputs/errno_func.h"
25 void clang_analyzer_eval(int);
26 void ErrnoTesterChecker_setErrno(int);
27 int ErrnoTesterChecker_getErrno();
28 int ErrnoTesterChecker_setErrnoIfError();
29 int ErrnoTesterChecker_setErrnoIfErrorRange();
30 int ErrnoTesterChecker_setErrnoCheckState();
35 // Test if errno is initialized.
36 clang_analyzer_eval(errno
== 0); // expected-warning{{TRUE}}
38 ErrnoTesterChecker_setErrno(1);
39 // Test if errno was recognized and changed.
40 clang_analyzer_eval(errno
== 1); // expected-warning{{TRUE}}
41 clang_analyzer_eval(ErrnoTesterChecker_getErrno() == 1); // expected-warning{{TRUE}}
45 // Test if errno was invalidated.
46 clang_analyzer_eval(errno
); // expected-warning{{UNKNOWN}}
47 clang_analyzer_eval(ErrnoTesterChecker_getErrno()); // expected-warning{{UNKNOWN}}
50 void testRange(int X
) {
52 ErrnoTesterChecker_setErrno(X
);
53 clang_analyzer_eval(errno
> 0); // expected-warning{{TRUE}}
58 if (ErrnoTesterChecker_setErrnoIfError())
59 clang_analyzer_eval(errno
== 11); // expected-warning{{TRUE}}
62 void testIfErrorRange() {
63 if (ErrnoTesterChecker_setErrnoIfErrorRange()) {
64 clang_analyzer_eval(errno
!= 0); // expected-warning{{TRUE}}
65 clang_analyzer_eval(errno
== 1); // expected-warning{{FALSE}} expected-warning{{TRUE}}
69 void testErrnoCheck0() {
70 // If the function returns a success result code, value of 'errno'
71 // is unspecified and it is unsafe to make any decision with it.
72 // The function did not promise to not change 'errno' if no failure happens.
73 int X
= ErrnoTesterChecker_setErrnoCheckState();
75 if (errno
) { // expected-warning{{An undefined value may be read from 'errno' [unix.Errno]}}
77 if (errno
) { // no warning for second time (analysis stops at the first warning)
80 X
= ErrnoTesterChecker_setErrnoCheckState();
82 if (errno
) { // expected-warning{{An undefined value may be read from 'errno' [unix.Errno]}}
86 X
= ErrnoTesterChecker_setErrnoCheckState();
89 if (errno
) { // no warning after overwritten 'errno'
94 void testErrnoCheck1() {
95 // If the function returns error result code that is out-of-band (not a valid
96 // non-error return value) the value of 'errno' can be checked but it is not
98 int X
= ErrnoTesterChecker_setErrnoCheckState();
100 if (errno
) { // no warning
103 X
= ErrnoTesterChecker_setErrnoCheckState();
105 errno
= 0; // no warning
109 void testErrnoCheck2() {
110 // If the function returns an in-band error result the value of 'errno' is
111 // required to be checked to verify if error happened.
112 // The same applies to other functions that can indicate failure only by
113 // change of 'errno'.
114 int X
= ErrnoTesterChecker_setErrnoCheckState();
116 errno
= 0; // expected-warning{{Value of 'errno' was not checked and is overwritten here [unix.Errno]}}
119 X
= ErrnoTesterChecker_setErrnoCheckState();
121 errno
= 0; // expected-warning{{Value of 'errno' was not checked and is overwritten here [unix.Errno]}}
127 void testErrnoCheck3() {
128 int X
= ErrnoTesterChecker_setErrnoCheckState();
132 errno
= 0; // no warning after 'errno' was read
134 X
= ErrnoTesterChecker_setErrnoCheckState();
137 errno
= 0; // no warning after 'errno' was read
141 void testErrnoCheckUndefinedLoad() {
142 int X
= ErrnoTesterChecker_setErrnoCheckState();
144 if (errno
) { // expected-warning{{An undefined value may be read from 'errno' [unix.Errno]}}
149 void testErrnoNotCheckedAtSystemCall() {
150 int X
= ErrnoTesterChecker_setErrnoCheckState();
152 printf("%i", 1); // expected-warning{{Value of 'errno' was not checked and may be overwritten by function 'printf' [unix.Errno]}}
153 printf("%i", 1); // no warning ('printf' does not change errno state)
157 void testErrnoCheckStateInvalidate() {
158 int X
= ErrnoTesterChecker_setErrnoCheckState();
161 if (errno
) { // no warning after an invalidating function call
164 X
= ErrnoTesterChecker_setErrnoCheckState();
167 if (errno
) { // no warning after an invalidating standard function call
172 void testErrnoCheckStateInvalidate1() {
173 int X
= ErrnoTesterChecker_setErrnoCheckState();
175 clang_analyzer_eval(errno
); // expected-warning{{TRUE}}
177 clang_analyzer_eval(errno
); // expected-warning{{UNKNOWN}}
178 errno
= 0; // no warning after invalidation
182 void test_if_cond_in_expr() {
183 ErrnoTesterChecker_setErrnoIfError();
184 if (errno
+ 10 > 2) {
185 // expected-warning@-1{{An undefined value may be read from 'errno'}}
189 void test_for_cond() {
190 ErrnoTesterChecker_setErrnoIfError();
191 for (; errno
!= 0;) {
192 // expected-warning@-1{{An undefined value may be read from 'errno'}}
196 void test_do_cond() {
197 ErrnoTesterChecker_setErrnoIfError();
199 } while (errno
!= 0);
200 // expected-warning@-1{{An undefined value may be read from 'errno'}}
203 void test_while_cond() {
204 ErrnoTesterChecker_setErrnoIfError();
206 // expected-warning@-1{{An undefined value may be read from 'errno'}}
210 void test_switch_cond() {
211 ErrnoTesterChecker_setErrnoIfError();
213 // expected-warning@-1{{An undefined value may be read from 'errno'}}
216 void test_conditional_cond() {
217 ErrnoTesterChecker_setErrnoIfError();
218 int A
= errno
? 1 : 2;
219 // expected-warning@-1{{An undefined value may be read from 'errno'}}
222 void test_binary_conditional_cond() {
223 ErrnoTesterChecker_setErrnoIfError();
225 // expected-warning@-1{{An undefined value may be read from 'errno'}}
228 void test_errno_store_into_variable() {
229 ErrnoTesterChecker_setErrnoIfError();
230 int a
= errno
; // AllowNonConditionErrnoRead is on by default, no warning
233 void test_errno_store_into_variable_in_expr() {
234 ErrnoTesterChecker_setErrnoIfError();
235 int a
= errno
> 1; // AllowNonConditionErrnoRead is on by default, no warning
238 int test_errno_return() {
239 ErrnoTesterChecker_setErrnoIfError();
243 void test_errno_pointer1() {
244 ErrnoTesterChecker_setErrnoIfError();
245 int *ErrnoP
= &errno
;
246 int A
= errno
? 1 : 2;
247 // expected-warning@-1{{An undefined value may be read from 'errno'}}
250 void test_errno_pointer2() {
251 ErrnoTesterChecker_setErrnoIfError();
252 int *ErrnoP
= &errno
;
253 int A
= (*ErrnoP
) ? 1 : 2;
254 // expected-warning@-1{{An undefined value may be read from 'errno'}}
259 void test_errno_in_condition_in_function_call() {
260 ErrnoTesterChecker_setErrnoIfError();