forked from github/codeql-coding-standards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
197 lines (162 loc) · 3.67 KB
/
test.cpp
File metadata and controls
197 lines (162 loc) · 3.67 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
class A {
public:
int m1;
};
void init_by_ref(int &ref_param);
void init_by_pointer(int *pointer_param);
template <typename T> void use(T t){};
void test_basic_init() {
int l1 = 0;
use(l1); // COMPLIANT
int l2{};
use(l2); // COMPLIANT
A l3{};
use(l3); // COMPLIANT
int l4;
init_by_ref(l4);
use(l4); // COMPLIANT
int l5;
init_by_pointer(&l5);
use(l5); // COMPLIANT
A l6;
l6.m1 = 1; // COMPLIANT
use(l6); // COMPLIANT
int l7[10] = {1, 0};
use(l7); // COMPLIANT
A l8;
use(l8); // COMPLIANT - default initialized
}
void test_basic_uninit() {
int l1;
use(l1); // NON_COMPLIANT
int *l2;
use(l2); // NON_COMPLIANT
A *l3;
use(l3); // NON_COMPLIANT
A l4;
use(l4.m1); // NON_COMPLIANT[FALSE_NEGATIVE] - field is not initialized
int l5[10];
use(l5[0]); // NON_COMPLIANT[FALSE_NEGATIVE] - array entry is not initialized
}
bool cond_init_by_ref(int &ref_param);
bool run1();
void test_conditional(bool x) {
int l1; // l1 is defined and used only when x is true
if (x) {
l1 = 0;
}
if (x) {
use(l1); // COMPLIANT
}
int l2; // l2 is defined and used only when x is false
if (!x) {
l2 = 0;
}
if (!x) {
use(l2); // COMPLIANT
}
bool l3 = false;
int l4;
if (x) {
l3 = true;
l4 = 1;
}
if (l3) { // l3 true indicates l4 is initialized
use(l4); // COMPLIANT
}
int numElements = 0;
int *arrayPtr;
if (x) {
numElements = 5;
arrayPtr = new int[5]{};
}
if (numElements > 0) { // numElements > 0 indicates arrayPtr is initialized
use(arrayPtr); // COMPLIANT[FALSE_POSITIVE]
}
int l5;
bool result = run1() && cond_init_by_ref(l5);
if (result) { // result indicates l5 is initialized
use(l5); // COMPLIANT[FALSE_POSITIVE]
}
int l6;
if (run1() && cond_init_by_ref(l6)) { // result indicates l6 is initialized
use(l6); // COMPLIANT
}
int l7;
if (!run1() || !cond_init_by_ref(l7)) { // result indicates l7 is initialized
return;
}
use(l7); // COMPLIANT
int l8;
if (__builtin_expect(!run1() || !cond_init_by_ref(l8),
0)) { // result indicates l8 is initialized
return;
}
use(l8); // COMPLIANT[FALSE_NEGATIVE]
}
void test_non_default_init() {
static int sl;
use(sl); // COMPLIANT - static variables are zero initialized
thread_local int tl;
use(tl); // COMPLIANT - thread local variables are zero initialized
static int *slp;
use(slp); // COMPLIANT - static variables are zero initialized
thread_local int *tlp;
use(tlp); // COMPLIANT - thread local variables are zero initialized
_Atomic int ai;
use(ai); // COMPLIANT - atomics are special and not covered by this rule
}
namespace {
int i; // COMPLIANT
}
void extra_test() {
int i;
int j = i + 1; // NON_COMPLIANT
int *i1 = new int;
int i2 = *i1; // NON_COMPLIANT
int *i3;
if (i3 = i1) { // NON_COMPLIANT
}
}
void extra_conditionals(bool b) {
if (b) {
goto L;
}
int i;
i = 1;
L:
i = i + 1; // NON_COMPLIANT[FALSE_NEGATIVE]
}
struct S {
int m1;
int m2;
};
void struct_test() {
S s1;
S s2 = {1};
auto i1 = s1.m1; // NON_COMPLIANT[FALSE_NEGATIVE] - rule currently is not
// field sensitive
auto i2 = s2.m2; // COMPLIANT
int a1[10] = {1, 1, 1};
int a2[10];
auto a3 = a1[5]; // COMPLIANT
auto a4 = a2[5]; // NON_COMPLIANT[FALSE_NEGATIVE]
}
class C {
private:
int m1;
int m2;
public:
C() : m1(1), m2(1) {}
C(int a) : m1(a) {}
int getm2() { return m2; }
};
void test_class() {
C c1;
if (c1.getm2() > 0) { // COMPLIANT
}
C c2(5);
if (c2.getm2() > 0) { // NON_COMPLIANT[FALSE_NEGATIVE] - rule currently is not
// field sensitive
}
}