-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathtest.cpp
More file actions
90 lines (79 loc) · 2.35 KB
/
Copy pathtest.cpp
File metadata and controls
90 lines (79 loc) · 2.35 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
// NOTICE: THE TEST CASES BELOW ARE ALSO INCLUDED IN THE C TEST CASE AND
// CHANGES SHOULD BE REFLECTED THERE AS WELL.
#include <limits.h>
void test_add_simple(unsigned int i1, unsigned int i2) {
i1 + i2; // NON_COMPLIANT - not bounds checked
i1 += i2; // NON_COMPLIANT - not bounds checked
}
void test_add_precheck(unsigned int i1, unsigned int i2) {
if (UINT_MAX - i1 < i2) {
// handle error
} else {
i1 + i2; // COMPLIANT - bounds checked
i1 += i2; // COMPLIANT - bounds checked
}
}
void test_add_precheck_2(unsigned int i1, unsigned int i2) {
if (i1 + i2 < i1) {
// handle error
} else {
i1 + i2; // COMPLIANT - bounds checked
i1 += i2; // COMPLIANT - bounds checked
}
}
void test_add_postcheck(unsigned int i1, unsigned int i2) {
unsigned int i3 = i1 + i2; // COMPLIANT - checked for overflow afterwards
if (i3 < i1) {
// handle error
}
i1 += i2; // COMPLIANT - checked for overflow afterwards
if (i1 < i2) {
// handle error
}
}
void test_ex2(unsigned int i1, unsigned int i2) {
unsigned int ci1 = 2;
unsigned int ci2 = 3;
ci1 + ci2; // COMPLIANT, compile time constants
i1 + 0; // COMPLIANT
i1 += 0; // COMPLIANT
i1 - 0; // COMPLIANT
i1 -= 0; // COMPLIANT
UINT_MAX - i1; // COMPLIANT - cannot be smaller than 0
i1 * 1; // COMPLIANT
i1 *= 1; // COMPLIANT
if (0 <= i1 && i1 < 32) {
UINT_MAX >> i1; // COMPLIANT
}
}
void test_ex3(unsigned int i1, unsigned int i2) {
i1 << i2; // COMPLIANT - by EX3
}
void test_sub_simple(unsigned int i1, unsigned int i2) {
i1 - i2; // NON_COMPLIANT - not bounds checked
i1 -= i2; // NON_COMPLIANT - not bounds checked
}
void test_sub_precheck(unsigned int i1, unsigned int i2) {
if (i1 < i2) {
// handle error
} else {
i1 - i2; // COMPLIANT - bounds checked
i1 -= i2; // COMPLIANT - bounds checked
}
}
void test_sub_postcheck(unsigned int i1, unsigned int i2) {
unsigned int i3 = i1 - i2; // COMPLIANT - checked for wrap afterwards
if (i3 > i1) {
// handle error
}
i1 -= i2; // COMPLIANT - checked for wrap afterwards
if (i1 > i2) {
// handle error
}
void test_mod_rem(unsigned int i1, unsigned int i2) {
i1 / i2; // COMPLIANT - exception 2
i1 /= i2; // COMPLIANT - exception 2
i1 % i2; // COMPLIANT - exception 2
i1 %= i2; // COMPLIANT - exception 2
}
}