Repository navigation
Expand file tree
/
Copy pathtest.cpp
More file actions
244 lines (219 loc) · 10.4 KB
/
Copy pathtest.cpp
File metadata and controls
244 lines (219 loc) · 10.4 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <cstdint>
#include <vector>
void f1(std::int8_t *p1, // NON_COMPLIANT - *p1 not modified
const std::int8_t *p2, // COMPLIANT - already const
std::int8_t *p3, // COMPLIANT - *p3 modified
std::int8_t *const p4, // NON_COMPLIANT - *p4 not modified
std::int8_t p5[3]) { // NON_COMPLIANT - array decays to pointer
*p3 = *p1 + *p2 + *p4 + p5[2];
}
void f2(std::int32_t &l1, // COMPLIANT - modified
std::int32_t &&l2, // COMPLIANT - rvalue reference, rule N/A
std::int32_t &) { // COMPLIANT - unnamed, rule N/A
l1 = 0;
}
auto f3(std::vector<std::int32_t> &l1, // COMPLIANT - x.begin() is non-const
std::vector<std::int32_t> &l2 // NON_COMPLIANT - cbegin is const
) {
l1.begin();
l2.cbegin();
}
int32_t i32;
int32_t *ptr;
const int32_t *cptr;
int32_t &ref = i32;
void take_i32(std::int32_t l1);
void take_ptr(std::int32_t *l1);
void take_cptr(const std::int32_t *l1);
void take_cptrc(const std::int32_t *const l1);
void take_ptrc(std::int32_t *const l1);
void take_ref(std::int32_t &l1);
void take_cref(const std::int32_t &l1);
void f4() {
// Pointers used as values
[](std::int32_t *p1) { *p1 = 0; }; // COMPLIANT
[](std::int32_t *p1) { p1 = nullptr; }; // NON_COMPLIANT
[](std::int32_t *p1) { i32 = *p1; }; // NON_COMPLIANT
[](std::int32_t *p1) { take_i32(*p1); }; // NON_COMPLIANT
[](std::int32_t *p1) { ref = *p1; }; // COMPLIANT
[](std::int32_t *p1) { take_ref(*p1); }; // COMPLIANT
[](std::int32_t *p1) { const int &l1 = *p1; }; // NON_COMPLIANT
[](std::int32_t *p1) { take_cref(*p1); }; // NON_COMPLIANT
// References used as values
[](std::int32_t &p1) { p1 = 0; }; // COMPLIANT
[](std::int32_t &p1) { i32 = p1; }; // NON_COMPLIANT
[](std::int32_t &p1) { take_i32(p1); }; // NON_COMPLIANT
[](std::int32_t &p1) { ref = p1; }; // COMPLIANT
[](std::int32_t &p1) { take_ref(p1); }; // COMPLIANT
[](std::int32_t &p1) { const int &l1 = p1; }; // NON_COMPLIANT
[](std::int32_t &p1) { take_cref(p1); }; // NON_COMPLIANT
// Pointers used as pointers
[](std::int32_t *p1) { ptr = p1; }; // COMPLIANT
[](std::int32_t *p1) { take_ptr(p1); }; // COMPLIANT
[](std::int32_t *p1) { cptr = p1; }; // NON_COMPLIANT
[](std::int32_t *p1) { take_cptr(p1); }; // NON_COMPLIANT
[](std::int32_t *p1) { const std::int32_t *const l1 = p1; }; // NON_COMPLIANT
[](std::int32_t *p1) { take_cptrc(p1); }; // NON_COMPLIANT
[](std::int32_t *p1) { std::int32_t *const l1 = p1; }; // COMPLIANT
[](std::int32_t *p1) { take_ptrc(p1); }; // COMPLIANT
// Taking addresses of references parameter
[](std::int32_t &p1) { ptr = &p1; }; // COMPLIANT
[](std::int32_t &p1) { take_ptr(&p1); }; // COMPLIANT
[](std::int32_t &p1) { cptr = &p1; }; // NON_COMPLIANT
[](std::int32_t &p1) { take_cptr(&p1); }; // NON_COMPLIANT
[](std::int32_t &p1) { const std::int32_t *const l1 = &p1; }; // NON_COMPLIANT
[](std::int32_t &p1) { take_cptrc(&p1); }; // NON_COMPLIANT
[](std::int32_t &p1) { std::int32_t *const l1 = &p1; }; // COMPLIANT
[](std::int32_t &p1) { take_ptrc(&p1); }; // COMPLIANT
// Returning from pointer parameters
[](std::int32_t *p1) -> std::int32_t { return *p1; }; // NON_COMPLIANT
[](std::int32_t *p1) -> std::int32_t & { return *p1; }; // COMPLIANT
[](std::int32_t *p1) -> const std::int32_t & { return *p1; }; // NON_COMPLIANT
[](std::int32_t *p1) -> std::int32_t * { return p1; }; // COMPLIANT
[](std::int32_t *p1) -> const std::int32_t * { return p1; }; // NON_COMPLIANT
[](std::int32_t *p1) -> std::int32_t *const { return p1; }; // COMPLIANT
[](std::int32_t *p1) -> const std::int32_t *const {
return p1;
}; // NON_COMPLIANT
// Returning from reference parameters
[](std::int32_t &p1) -> std::int32_t { return p1; }; // NON_COMPLIANT
[](std::int32_t &p1) -> std::int32_t & { return p1; }; // COMPLIANT
[](std::int32_t &p1) -> const std::int32_t & { return p1; }; // NON_COMPLIANT
[](std::int32_t &p1) -> std::int32_t * { return &p1; }; // COMPLIANT
[](std::int32_t &p1) -> const std::int32_t * { return &p1; }; // NON_COMPLIANT
[](std::int32_t &p1) -> std::int32_t *const { return &p1; }; // COMPLIANT
[](std::int32_t &p1) -> const std::int32_t *const {
return &p1;
}; // NON_COMPLIANT
// Non compliant cases are compliant when const
using cpi32 = const std::int32_t *;
using cri32 = const std::int32_t &;
[](const std::int32_t *p1) { p1 = nullptr; }; // COMPLIANT
[](const std::int32_t *p1) { i32 = *p1; }; // COMPLIANT
[](const std::int32_t *p1) { take_i32(*p1); }; // COMPLIANT
[](const std::int32_t *p1) { const int &l1 = *p1; }; // COMPLIANT
[](const std::int32_t *p1) { take_cref(*p1); }; // COMPLIANT
[](const std::int32_t &p1) { i32 = p1; }; // COMPLIANT
[](const std::int32_t &p1) { take_i32(p1); }; // COMPLIANT
[](const std::int32_t &p1) { const int &l1 = p1; }; // COMPLIANT
[](const std::int32_t &p1) { take_cref(p1); }; // COMPLIANT
[](const std::int32_t *p1) { cptr = p1; }; // COMPLIANT
[](const std::int32_t *p1) { take_cptr(p1); }; // COMPLIANT
[](cpi32 p1) { const std::int32_t *const l1 = p1; }; // COMPLIANT
[](cpi32 p1) { take_cptrc(p1); }; // COMPLIANT
[](cri32 p1) { cptr = &p1; }; // COMPLIANT
[](cri32 p1) { take_cptr(&p1); }; // COMPLIANT
[](cri32 p1) { const std::int32_t *const l1 = &p1; }; // COMPLIANT
[](cri32 p1) { take_cptrc(&p1); }; // COMPLIANT
[](cpi32 p1) -> std::int32_t { return *p1; }; // COMPLIANT
[](cpi32 p1) -> const std::int32_t & { return *p1; }; // COMPLIANT
[](cpi32 p1) -> const std::int32_t * { return p1; }; // COMPLIANT
[](cpi32 p1) -> const std::int32_t *const { return p1; }; // COMPLIANT
[](cri32 p1) -> std::int32_t { return p1; }; // COMPLIANT
[](cri32 p1) -> const std::int32_t & { return p1; }; // COMPLIANT
[](cri32 p1) -> const std::int32_t * { return &p1; }; // COMPLIANT
[](cri32 p1) -> const std::int32_t *const { return &p1; }; // COMPLIANT
// Non pointer compliant cases are not compliant for `int32_t *const`.
using pi32c = std::int32_t *const;
[](std::int32_t *const p1) { i32 = *p1; }; // NON_COMPLIANT
[](std::int32_t *const p1) { take_i32(*p1); }; // NON_COMPLIANT
[](std::int32_t *const p1) { const int &l1 = *p1; }; // NON_COMPLIANT
[](std::int32_t *const p1) { take_cref(*p1); }; // NON_COMPLIANT
[](std::int32_t *const p1) { cptr = p1; }; // NON_COMPLIANT
[](std::int32_t *const p1) { take_cptr(p1); }; // NON_COMPLIANT
[](pi32c p1) { const std::int32_t *const l1 = p1; }; // NON_COMPLIANT
[](pi32c p1) { take_cptrc(p1); }; // NON_COMPLIANT
[](pi32c p1) -> std::int32_t { return *p1; }; // NON_COMPLIANT
[](pi32c p1) -> const std::int32_t & { return *p1; }; // NON_COMPLIANT
[](pi32c p1) -> const std::int32_t * { return p1; }; // NON_COMPLIANT
[](pi32c p1) -> const std::int32_t *const { return p1; }; // NON_COMPLIANT
}
void f5(std::int32_t *p1, // NON_COMPLIANT -- pointer modified
std::int32_t *p2, // COMPLIANT -- pointee modified
std::int32_t &p3, // COMPLIANT -- reference modified
std::int32_t *p4, // NON_COMPLIANT -- pointer assigned
std::int32_t *p5, // COMPLIANT -- pointee assigned
std::int32_t &p6, // COMPLIANT -- assigned
const std::int32_t *p7, // COMPLIANT
const std::int32_t &p8, // COMPLIANT
std::int32_t *p9, // NON_COMPLIANT
std::int32_t &p10, // NON_COMPLIANT
std::int32_t *p11, // COMPLIANT
std::int32_t *p12, // NON_COMPLIANT -- unused
std::int32_t **p13, // COMPLIANT
std::int32_t **p14, // COMPLIANT
std::int32_t **p15, // COMPLIANT
std::int32_t **p16 // COMPLIANT
) {
p1++;
(*p2)++;
p3++;
p4 += 0;
*p5 *= 0;
p6 /= 0;
p7++;
if (p8) {
}
p9 ? true : false;
while (p10) {
}
p11[0] = 0;
// p12
p13[0] = p1;
p14[0][0] = 0;
(*p15)[0] = 0;
*(p16[0]) = 0;
}
struct S {
std::int32_t m1;
};
void f6(S *p1, // COMPLIANT
S *p2, // NON_COMPLIANT
S *p3 // COMPLIANT
) {
p1->m1 = 1;
std::int32_t l1 = p2->m1;
std::int32_t &l2 = p3->m1;
}
void f7(std::int32_t *p1, // COMPLIANT
std::int32_t *p2, // COMPLIANT
std::int32_t *p3, // NON_COMPLIANT[False negative]
std::int32_t *p4 // NON_COMPLIANT
) {
using pi32 = std::int32_t *;
using cpi32 = const std::int32_t *;
using ci32 = const std::int32_t;
pi32 *l1 = &p1; // pointer to pointer to non-const
pi32 &l2 = p2; // reference to pointer to non-const
cpi32 *l3 = &p3; // pointer to pointer to const
const cpi32 &l4 = p4; // (const) reference to pointer to const
// cpi32 &l4 = p4; -- does not compile, non-const lvalues cannot involve a
// const cast from *i32 to const i32*
}
int main(int argc, char *argv[]); // COMPLIANT - main is excluded
void f8(std::int32_t *p1, std::int32_t &p2); // COMPLIANT - no body to analyze
void f9(std::int32_t *p1, std::int32_t &p2) = delete; // COMPLIANT - deleted
void f10(void *l1) {} // COMPLIANT - void pointer excluded
class Base {
public:
virtual void f(std::int32_t *p1, std::int32_t &p2); // COMPLIANT
};
class Derived : public Base {
public:
void f(std::int32_t *p1, std::int32_t &p2) override; // COMPLIANT
};
template <typename T> struct A {
void m1(T &p1, T *p2) {} // COMPLIANT - in template scope
void m2() {
auto lambda = [](std::int32_t &l2) {
}; // COMPLIANT - lambda in template scope
}
};
template <typename T> void f11(T &l1) {} // COMPLIANT - function template
using func = int(int);
void f12(void **p1, // COMPLIANT
void *const *p2, // COMPLIANT
func *p3, // COMPLIANT
func **p4, // COMPLIANT
func *const *p5 // COMPLIANT
) {}