[AMDGPU][AsmParser][NFC] Get rid of custom default operand handlers.
[llvm-project.git] / clang / test / Analysis / errno.c
blob1f094cc741eb308980954b40fa5312d861825d54
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=alpha.unix.Errno \
7 // RUN: -DERRNO_VAR
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=alpha.unix.Errno \
15 // RUN: -DERRNO_FUNC
17 #include "Inputs/system-header-simulator.h"
18 #ifdef ERRNO_VAR
19 #include "Inputs/errno_var.h"
20 #endif
21 #ifdef ERRNO_FUNC
22 #include "Inputs/errno_func.h"
23 #endif
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();
32 void something();
34 void test() {
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}}
43 something();
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) {
51 if (X > 0) {
52 ErrnoTesterChecker_setErrno(X);
53 clang_analyzer_eval(errno > 0); // expected-warning{{TRUE}}
57 void testIfError() {
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();
74 if (X == 0) {
75 if (errno) { // expected-warning{{An undefined value may be read from 'errno' [alpha.unix.Errno]}}
77 if (errno) { // no warning for second time (analysis stops at the first warning)
80 X = ErrnoTesterChecker_setErrnoCheckState();
81 if (X == 0) {
82 if (errno) { // expected-warning{{An undefined value may be read from 'errno' [alpha.unix.Errno]}}
84 errno = 0;
86 X = ErrnoTesterChecker_setErrnoCheckState();
87 if (X == 0) {
88 errno = 0;
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
97 // required to do so.
98 int X = ErrnoTesterChecker_setErrnoCheckState();
99 if (X == 1) {
100 if (errno) { // no warning
103 X = ErrnoTesterChecker_setErrnoCheckState();
104 if (X == 1) {
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();
115 if (X == 2) {
116 errno = 0; // expected-warning{{Value of 'errno' was not checked and is overwritten here [alpha.unix.Errno]}}
117 errno = 0;
119 X = ErrnoTesterChecker_setErrnoCheckState();
120 if (X == 2) {
121 errno = 0; // expected-warning{{Value of 'errno' was not checked and is overwritten here [alpha.unix.Errno]}}
122 if (errno) {
127 void testErrnoCheck3() {
128 int X = ErrnoTesterChecker_setErrnoCheckState();
129 if (X == 2) {
130 if (errno) {
132 errno = 0; // no warning after 'errno' was read
134 X = ErrnoTesterChecker_setErrnoCheckState();
135 if (X == 2) {
136 int A = errno;
137 errno = 0; // no warning after 'errno' was read
141 void testErrnoCheckUndefinedLoad() {
142 int X = ErrnoTesterChecker_setErrnoCheckState();
143 if (X == 0) {
144 if (errno) { // expected-warning{{An undefined value may be read from 'errno' [alpha.unix.Errno]}}
149 void testErrnoNotCheckedAtSystemCall() {
150 int X = ErrnoTesterChecker_setErrnoCheckState();
151 if (X == 2) {
152 printf("%i", 1); // expected-warning{{Value of 'errno' was not checked and may be overwritten by function 'printf' [alpha.unix.Errno]}}
153 printf("%i", 1); // no warning ('printf' does not change errno state)
157 void testErrnoCheckStateInvalidate() {
158 int X = ErrnoTesterChecker_setErrnoCheckState();
159 if (X == 0) {
160 something();
161 if (errno) { // no warning after an invalidating function call
164 X = ErrnoTesterChecker_setErrnoCheckState();
165 if (X == 0) {
166 printf("%i", 1);
167 if (errno) { // no warning after an invalidating standard function call
172 void testErrnoCheckStateInvalidate1() {
173 int X = ErrnoTesterChecker_setErrnoCheckState();
174 if (X == 2) {
175 clang_analyzer_eval(errno); // expected-warning{{TRUE}}
176 something();
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();
198 do {
199 } while (errno != 0);
200 // expected-warning@-1{{An undefined value may be read from 'errno'}}
203 void test_while_cond() {
204 ErrnoTesterChecker_setErrnoIfError();
205 while (errno != 0) {
206 // expected-warning@-1{{An undefined value may be read from 'errno'}}
210 void test_switch_cond() {
211 ErrnoTesterChecker_setErrnoIfError();
212 switch (errno) {}
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();
224 int A = errno ?: 2;
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();
240 return errno;
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'}}
257 int f(int);
259 void test_errno_in_condition_in_function_call() {
260 ErrnoTesterChecker_setErrnoIfError();
261 if (f(errno) != 0) {