-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathtest.cpp
More file actions
202 lines (177 loc) · 4.39 KB
/
test.cpp
File metadata and controls
202 lines (177 loc) · 4.39 KB
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
#include <cstddef>
#include <cstdint>
#include <limits.h>
void test_add_simple(signed int i1, signed int i2) {
i1 + i2; // NON_COMPLIANT - not bounds checked
i1 += i2; // NON_COMPLIANT - not bounds checked
}
void test_add_precheck(signed int i1, signed int i2) {
// Style recommended by CERT
if (((i2 > 0) && (i1 > (INT_MAX - i2))) ||
((i2 < 0) && (i1 < (INT_MIN - i2)))) {
// handle error
} else {
i1 + i2; // COMPLIANT - bounds appropriately checked
i1 += i2; // COMPLIANT - bounds appropriately checked
}
}
void test_add_precheck_2(signed int i1, signed int i2) {
if (i1 + i2 < i1) { // NON_COMPLIANT - bad overflow check - undefined behavior
// handle error
} else {
i1 + i2; // NON_COMPLIANT
i1 += i2; // NON_COMPLIANT
}
}
void test_add_postcheck(signed int i1, signed int i2) {
signed int i3 = i1 + i2; // NON_COMPLIANT - signed overflow is undefined
// behavior, so checking afterwards is not sufficient
if (i3 < i1) {
// handle error
}
i1 += i2; // NON_COMPLIANT
if (i1 < i2) {
// handle error
}
}
void test_sub_simple(signed int i1, signed int i2) {
i1 - i2; // NON_COMPLIANT - not bounds checked
i1 -= i2; // NON_COMPLIANT - not bounds checked
}
void test_sub_precheck(signed int i1, signed int i2) {
// Style recomended by CERT
if ((i2 > 0 && i1 < INT_MIN + i2) || (i2 < 0 && i1 > INT_MAX + i2)) {
// handle error
} else {
i1 - i2; // COMPLIANT - bounds checked
i1 -= i2; // COMPLIANT - bounds checked
}
}
void test_sub_postcheck(signed int i1, signed int i2) {
signed int i3 = i1 - i2; // NON_COMPLIANT - underflow is undefined behavior.
if (i3 > i1) {
// handle error
}
i1 -= i2; // NON_COMPLIANT - underflow is undefined behavior.
if (i1 > i2) {
// handle error
}
}
void test_mul_simple(signed int i1, signed int i2) {
i1 *i2; // NON_COMPLIANT
i1 *= i2; // NON_COMPLIANT
}
void test_mul_precheck(signed int i1, signed int i2) {
signed long long tmp =
(signed long long)i1 * (signed long long)i2; // COMPLIANT
signed int result;
if (tmp > INT_MAX || tmp < INT_MIN) {
// handle error
} else {
result = (signed int)tmp;
}
}
void test_mul_precheck_2(signed int i1, signed int i2) {
if (i1 > 0) {
if (i2 > 0) {
if (i1 > (INT_MAX / i2)) {
return; // handle error
}
} else {
if (i2 < (INT_MIN / i1)) {
// handle error
return; // handle error
}
}
} else {
if (i2 > 0) {
if (i1 < (INT_MIN / i2)) {
// handle error
return; // handle error
}
} else {
if ((i1 != 0) && (i2 < (INT_MAX / i1))) {
// handle error
return; // handle error
}
}
}
i1 *i2; // COMPLIANT
i1 *= i2; // COMPLIANT
}
void test_simple_div(signed int i1, signed int i2) {
i1 / i2; // NON_COMPLIANT
i1 /= i2; // NON_COMPLIANT
}
void test_simple_div_no_zero(signed int i1, signed int i2) {
if (i2 == 0) {
// handle error
} else {
i1 / i2; // NON_COMPLIANT
i1 /= i2; // NON_COMPLIANT
}
}
void test_div_precheck(signed int i1, signed int i2) {
if ((i2 == 0) || ((i1 == INT_MIN) && (i2 == -1))) {
/* Handle error */
} else {
i1 / i2; // COMPLIANT
i1 /= i2; // COMPLIANT
}
}
void test_simple_rem(signed int i1, signed int i2) {
i1 % i2; // NON_COMPLIANT
i1 %= i2; // NON_COMPLIANT
}
void test_simple_rem_no_zero(signed int i1, signed int i2) {
if (i2 == 0) {
// handle error
} else {
i1 % i2; // NON_COMPLIANT
i1 %= i2; // NON_COMPLIANT
}
}
void test_rem_precheck(signed int i1, signed int i2) {
if ((i2 == 0) || ((i1 == INT_MIN) && (i2 == -1))) {
/* Handle error */
} else {
i1 % i2; // COMPLIANT
i1 %= i2; // COMPLIANT
}
}
void test_simple_negate(signed int i1) {
-i1; // NON_COMPLIANT
}
void test_negate_precheck(signed int i1) {
if (i1 == INT_MIN) {
// handle error
} else {
-i1; // COMPLIANT
}
}
void test_inc(signed int i1) {
i1++; // NON_COMPLIANT
}
void test_inc_guard(signed int i1) {
if (i1 < INT_MAX) {
i1++; // COMPLIANT
}
}
void test_inc_loop_guard() {
for (signed int i1 = 0; i1 < 10; i1++) { // COMPLIANT
// ...
}
}
void test_dec(signed int i1) {
i1--; // NON_COMPLIANT
}
void test_dec_guard(signed int i1) {
if (i1 > INT_MIN) {
i1--; // COMPLIANT
}
}
void test_dec_loop_guard() {
for (signed int i1 = 10; i1 > 0; i1--) { // COMPLIANT
// ...
}
}