forked from github/codeql-coding-standards
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
134 lines (114 loc) · 3.17 KB
/
test.cpp
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
int f1_volatile();
void test_infeasible(unsigned int a) {
if (a >= 0U) // NON_COMPLIANT - `a` is unsigned, therefore the comparison is
// always true
;
if (a <= 0xffffffffU) // NON_COMPLIANT - `a` is unsigned, and cannot be above
// 0xffffffff as 32 bit int
;
if (a > 0U) // COMPLIANT - `a` is unsigned, but may be equal to zero
;
int l1 = 2;
int l2 = 10;
if (l1 < l2) // NON_COMPLIANT - both variables are constants
;
if (a < l1) { // COMPLIANT - `a` is not constant
if (a < l2) // NON_COMPLIANT - `a` is always less than `l2`
// because `a < l1` and `l1 < l2`
;
}
volatile int l3 = f1_volatile();
if (l3 < l1) { // COMPLIANT - `l3` is not constant
if (l3 < l2) // COMPLIANT - `l3` is volatile, so while `l1 < l2`, the value
// of `l3` could change
;
}
}
template <class T> int f() {
if (0) { // NON_COMPLIANT - true path is infeasible in all circumstances
return 3;
}
if (T::isVal()) { // COMPLIANT - `isVal` is `true` for all
// visible instantiations, but in the uninstantiated
// template both paths are feasible. This represents that
// this is template dependent, so we consider it compliant
return 2;
}
if (T::isVal2()) { // COMPLIANT - `isVal2` is either true or
// false
return 2;
}
return 1; // COMPLIANT - block is reachable in the uninstantiated template
}
class A {
public:
static bool isVal() { return true; }
static bool isVal2() { return true; }
};
class B {
public:
static bool isVal() { return true; }
static bool isVal2() { return false; }
};
void test_f() {
f<A>();
f<B>();
}
void test_break(int a) {
while (true) { // COMPLIANT
if (a++ >= 0) // COMPLIANT
break;
}
return;
}
void test_infeasible_break(unsigned int a) {
while (true) { // NON_COMPLIANT[FALSE_NEGATIVE]
if (a < 0U) // NON_COMPLIANT - the comparison is always false
break;
if (a >= 0U) { // NON_COMPLIANT - the comparison is always true
} else
break;
}
while (true) { // COMPLIANT
if (a < 0U) // NON_COMPLIANT - the comparison is always false
break;
else // COMPLIANT - the comparison is always false
break;
}
}
void test_loop(int a) {
while (a < 0) { // COMPLIANT
a++;
}
for (int i = a; i < 10; i++) { // COMPLIANT
a++;
}
}
template <bool x> int foo() {
if (x) { // COMPLIANT - block is reachable in the one of the instantiated
// template
return 1;
}
return 0; // COMPLIANT - block is reachable in the uninstantiated template
}
void test() {
foo<true>();
foo<false>();
}
template <class T> int template_infeasible_true_path() {
if (0) { // NON_COMPLIANT - true path is infeasible in all circumstances
return 3;
}
}
template <class T> int template_infeasible_false_path() {
if (!0) {
return 3;
}
return 1; // NON_COMPLIANT - false path is infeasible in all circumstances
}
void test_infeasible_instantiates() {
template_infeasible_true_path<A>();
template_infeasible_true_path<B>();
template_infeasible_false_path<A>();
template_infeasible_false_path<B>();
}