forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
166 lines (131 loc) · 2.76 KB
/
test.cpp
File metadata and controls
166 lines (131 loc) · 2.76 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
struct mySmallStruct {
char data[4];
};
struct myLargeStruct {
char data[4096];
};
template <class T>
class myTemplateClass
{
public:
myTemplateClass() {}
void set(T _t) { // BAD: T can be myLargeStruct, which is large
t = _t;
}
T t;
};
template<class T>
void myTemplateFunction(myTemplateClass<T> mtc_t) // BAD: T can be myLargeStruct, which is large
{
}
void myFunction1(mySmallStruct a, myLargeStruct b) // BAD: b is large
{
myTemplateClass<mySmallStruct> mtc_a;
myTemplateClass<myLargeStruct> mtc_b;
mtc_a.set(a);
mtc_b.set(b);
myTemplateFunction(mtc_a);
myTemplateFunction(mtc_b);
}
void myFunction2(mySmallStruct *a, myLargeStruct *b) // GOOD
{
}
void myFunction3(mySmallStruct &a, myLargeStruct &b) // GOOD
{
}
struct CustomAssignmentOp {
// GOOD: it's an accepted pattern to implement copy assignment via copy and
// swap. This delegates the resource management involved in copying to the
// copy constructor so that logic only has to be written once.
CustomAssignmentOp &operator=(CustomAssignmentOp rhs);
char data[4096];
};
// ---
struct MyLargeClass {
public:
MyLargeClass();
void myMethod();
void myConstMethod() const;
int value;
char data[4096];
};
void mlc_modify(MyLargeClass &c) {
c.value++;
}
int mlc_get(const MyLargeClass &c) {
return c.value;
}
void myFunction4(
MyLargeClass a, // GOOD: large, but the copy is written to so can't be trivially replaced with a reference
MyLargeClass b, // GOOD
MyLargeClass c, // GOOD
MyLargeClass d, // GOOD
MyLargeClass e, // GOOD
MyLargeClass f, // GOOD
MyLargeClass g // GOOD
)
{
MyLargeClass *mlc_ptr;
int *i_ptr;
a.value++;
b.value = 1;
c.data[0] += 1;
d.myMethod();
mlc_modify(e);
mlc_ptr = &f;
mlc_modify(*mlc_ptr);
i_ptr = &g.value;
*(i_ptr)++;
}
void myFunction5(
MyLargeClass a, // BAD
MyLargeClass b, // BAD
MyLargeClass c, // BAD
MyLargeClass d, // BAD
MyLargeClass e, // BAD
MyLargeClass f // BAD
)
{
const MyLargeClass *mlc_ptr;
const int *i_ptr;
int i;
i = a.value;
i += b.data[0];
c.myConstMethod();
i += mlc_get(d);
mlc_ptr = &e;
mlc_get(*mlc_ptr);
i_ptr = &f.value;
i += *i_ptr;
}
// ---
class MyArithmeticClass {
public:
MyArithmeticClass(int _value) : value(_value) {};
MyArithmeticClass &operator+=(const MyArithmeticClass &other) {
this->value += other.value;
return *this;
}
private:
int value;
char data[1024];
};
MyArithmeticClass operator+(MyArithmeticClass lhs, const MyArithmeticClass &rhs) { // GOOD
lhs += rhs; // lhs is copied by design
return lhs;
}
void myFunction6(MyLargeClass a); // GOOD (no definition, so we can't tell what's done with `a`)
// ---
struct big
{
int xs[800];
int *ptr;
};
void myFunction7(
big a, // GOOD
big b // BAD
)
{
a.xs[0]++; // modifies a
b.ptr[0]++; // does not modify b
}