-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathtest.c
212 lines (179 loc) · 6.96 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "math.h"
float getFloat() { return 1.0; }
// Parameter could be infinity
void f1(float p1) {
float l1 = 1;
float l2 = 1.0 / 0;
float l3 = -l2;
10 / l1; // COMPLIANT
10 / l2; // NON_COMPLIANT: Underflows to zero
10 / l3; // NON_COMPLIANT: Underflow to negative zero
10 / p1; // COMPLIANT: Reduce false positives by assuming not infinity
10 / getFloat(); // COMPLIANT: Reduce false positives by assuming not infinity
(int)l1; // COMPLIANT
(int)l2; // NON_COMPLIANT
(int)l3; // NON_COMPLIANT
(int)p1; // COMPLIANT: Reduce false positives by assuming not infinity
(int)getFloat(); // COMPLIANT: Reduce false positives by assuming not infinity
// Not NaN:
float l4 = l1 / l1; // COMPLIANT
// NaN because of infinity divided by itself:
float l5 = l2 / l2; // NON_COMPLIANT: Division by infinity not allowed.
float l6 = l3 / l3; // NON_COMPLIANT: Division by infinity not allowed.
// NaN because of zero divided by itself:
float l7 = getFloat() /
p1; // COMPLIANT: Reduce false positives by assuming not infinity
float l8 = 0.0 / 0.0;
(int)l4; // COMPLIANT
(int)l5; // COMPLIANT: Casting NaN to int
(int)l6; // COMPLIANT: Casting NaN to int
(int)l7; // NON_COMPLIANT: Casting Infinity to int
(int)l8; // COMPLIANT: Casting NaN to int
l4 == 0; // COMPLIANT
l4 != 0; // COMPLIANT
l4 <= 0; // COMPLIANT
l4 < 0; // COMPLIANT
l4 >= 0; // COMPLIANT
l5 == 0; // NON_COMPLIANT: Comparison with NaN always false
l5 != 0; // NON_COMPLIANT: Comparison with NaN always false
l5 < 0; // NON_COMPLIANT: Comparison with NaN always false
l5 <= 0; // NON_COMPLIANT: Comparison with NaN always false
l5 > 0; // NON_COMPLIANT: Comparison with NaN always false
l5 >= 0; // NON_COMPLIANT: Comparison with NaN always false
l6 == 0; // NON_COMPLIANT: Comparison with NaN always false
l7 == 0; // NON_COMPLIANT: Comparison with NaN always false
l8 == 0; // NON_COMPLIANT: Comparison with NaN always false
// Guards
float l9 = 0;
if (l9 != 0) {
(int)(l9 / l9); // COMPLIANT: l9 is not zero
} else {
(int)(l9 / l9); // NON_COMPLIANT[False positive]: Guarded to not be NaN
}
float l10 = 0;
if (l10 == 0) {
(int)(l10 / l10); // NON_COMPLIANT[False positive]: Casting NaN to integer
} else {
(int)(l10 / l10); // COMPLIANT: Guarded to not be NaN
}
float l11 = 0;
l11 == 0 ? (int)(l11 / l11) : 0; // NON_COMPLIANT[False positive]
l11 == 0 ? 0 : (int)(l11 / l11); // COMPLIANT
l11 != 0 ? (int)(l11 / l11) : 0; // COMPLIANT
l11 != 0 ? 0 : (int)(l11 / l11); // NON_COMPLIANT[False positive]
float l12 = 1.0 / 0;
if (isinf(l12)) {
(int)l12; // NON_COMPLIANT: Casting Infinity to integer
} else {
(int)l12; // COMPLIANT: Guarded not to be Infinity
}
if (!isinf(l12)) {
(int)l12; // COMPLIANT: Guarded not to be Infinity
} else {
(int)l12; // NON_COMPLIANT: Casting Infinity to integer
}
if (isinf(l12) == 1) {
(int)l12; // NON_COMPLIANT: Must be +Infinity
} else {
(int)l12; // NON_COMPLIANT: May be -Infinity
}
if (isfinite(l12)) {
(int)l12; // COMPLIANT: Guarded not to be Infinity
} else {
(int)l12; // NON_COMPLIANT: Casting Infinity to integer
}
if (isnormal(l12)) {
(int)l12; // COMPLIANT: Guarded not to be Infinity
} else {
(int)l12; // NON_COMPLIANT: Casting Infinity to integer
}
if (isnan(l12)) {
(int)l12; // COMPLIANT: Guarded not to be Infinity
} else {
(int)l12; // NON_COMPLIANT: Casting Infinity to integer
}
isinf(l12) ? (int)l12 : 0; // NON_COMPLIANT: Check on wrong branch
isinf(l12) ? 0 : (int)l12; // COMPLIANT: Checked not infinite before use
isfinite(l12) ? (int)l12 : 0; // COMPLIANT: Checked finite before use
isfinite(l12) ? 0 : (int)l12; // NON_COMPLIANT: Checked on wrong branch
isnan(l12) ? (int)l12
: 0; // COMPLIANT: Checked NaN, therefore not infinite, before use
isnan(l12) ? 0 : (int)l12; // NON_COMPLIANT: Check on wrong branch
float l13 = 0.0 / 0;
if (isinf(l13)) {
(int)l13; // COMPLIANT: Guarded not to be NaN
} else {
(int)l13; // COMPLIANT: Casting NaN to integer
}
if (isinf(l13) == 1) {
(int)l13; // COMPLIANT: Guarded not to be NaN (must be +Infinity)
} else {
(int)l13; // COMPLIANT: Casting NaN to integer
}
if (isfinite(l13)) {
(int)l13; // COMPLIANT: Guarded not to be NaN
} else {
(int)l13; // COMPLIANT: Casting NaN to integer
}
if (isnormal(l13)) {
(int)l13; // COMPLIANT: Guarded not to be NaN
} else {
(int)l13; // COMPLIANT: Casting NaN to integer
}
if (isnan(l13)) {
(int)l13; // COMPLIANT: Casting NaN to integer
} else {
(int)l13; // COMPLIANT: Guarded not to be NaN
}
isinf(l13) ? (int)l13
: 0; // COMPLIANT: Checked infinite, therefore not NaN, before use
isinf(l13) ? 0 : (int)l13; // COMPLIANT: Check on wrong branch
isfinite(l13) ? (int)l13 : 0; // COMPLIANT: Checked finite before use
isfinite(l13) ? 0 : (int)l13; // COMPLIANT: Checked on wrong branch
isnan(l13) ? (int)l13 : 0; // COMPLIANT: Check on wrong branch
isnan(l13) ? 0 : (int)l13; // COMPLIANT: Checked not NaN before use
(int)pow(2, p1); // NON_COMPLIANT[False negative]: likely to be Infinity
(int)pow(2, sin(p1)); // COMPLIANT: not likely to be Infinity
(int)(1 /
sin(p1)); // NON_COMPLIANT: possible infinity from zero in denominator
(int)(1 / log(p1)); // COMPLIANT: not possibly zero in denominator
(int)pow(p1, p1); // COMPLIANT: NaN if p1 is zero
if (p1 != 0) {
(int)pow(p1, p1); // COMPLIANT: p1 is not zero
}
(int)acos(p1); // COMPLIANT: NaN if p1 is not within -1..1
(int)acos(cos(p1)); // COMPLIANT: cos(p1) is within -1..1
}
void castToInt(float p) { (int)p; }
void checkBeforeCastToInt(float p) {
if (isfinite(p)) {
castToInt(p);
}
}
void castToIntToFloatToInt(float p) {
// This should be reported as a violation, but not downstream from here.
castToInt((int)p);
}
void addOneThenCastToInt(float p) { castToInt(p + 1); }
void addInfThenCastToInt(float p) { castToInt(p + 1.0 / 0.0); }
void addNaNThenCastToInt(float p) { castToInt(p + 0.0 / 0.0); }
void f2() {
castToInt(1.0 /
0.0); // NON_COMPLIANT: Infinity flows to denominator in division
castToInt(0.0 / 0.0); // COMPLIANT
checkBeforeCastToInt(1.0 / 0.0); // COMPLIANT
checkBeforeCastToInt(0.0 / 0.0); // COMPLIANT
addOneThenCastToInt(1.0 / 0.0); // NON_COMPLIANT[False negative]
addOneThenCastToInt(0.0 / 0.0); // NON_COMPLIANT
castToIntToFloatToInt(1.0 / 0.0); // NON_COMPLIANT
castToIntToFloatToInt(0.0 / 0.0); // COMPLIANT
// Check that during flow analysis, we only report the true root cause:
float rootInf = 1.0 / 0.0;
float rootNaN = 0.0 / 0.0;
float middleInf = rootInf + 1;
float middleNaN = rootNaN + 1;
castToInt(middleInf); // NON_COMPLIANT
castToInt(middleNaN); // COMPLIANT
addInfThenCastToInt(middleInf); // NON_COMPLIANT
addNaNThenCastToInt(middleNaN); // COMPLIANT
}