-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdummy.cpp
228 lines (172 loc) · 3.89 KB
/
dummy.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
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
// The Worst possible coder! please contribute!
//
#include "stdio.h"
#include <memory>
#include <string>
class Base
{
private:
int _id;
char* _leak;
public:
Base() // noexcept
{
_id = ClassID(); // call vfunction from constructor
_leak = new char[255]; // leak with derived class!
}
~Base() // should be virtual!!
{
delete _leak; // leak with derived class!
}
virtual int ClassID() { return 1; }
int GetID() { return _id; }
};
class Derived : public Base
{
public:
Derived()
{
}
~Derived() // base does not have a virtual destructor!
{
// will not call base! // leak in base class
}
virtual int ClassID() { return 2; } // noexcept?
};
int useretval(int& uninit)
{
uninit++;
return uninit;
}
void LeakSomeMemory()
{
std::string* leak = new std::string(); // obvious leak
leak->append("hello world"); // obvious leak
}
std::string& retBadRef(const std::string& a, const std::string& b)
{
std::string byrefret;
byrefret += a;
byrefret += b;
return byrefret; // ret local byref
}
class A // no virtual destructor?
{
public:
virtual std::string GetName() const { return "A"; }
};
class B : public A
{
public:
virtual std::string GetName() const { return "B"; } // where is override?
};
void badfunc(A a) // oh this happens way too often!
{
printf("badfunc: %s\n",a.GetName().c_str());
}
void goodfunc(A& a) // ok by ref, should be const?
{
printf("goodfunc: %s\n", a.GetName().c_str());
}
void path_sensitive(int *p, bool cond)
{
int state=0;
// branch 1
if(p!=nullptr){
state=1;
}
for(int state = 0; state < 1; state ++)
{
int p =0;
++p;
}
// branch 2
if(cond){
state=2;
p = nullptr;
}
// branch 3
if(state==1){
*p=42; // Null dereference?
}
}
void local_analysis(int *p, int *q, bool cond){
if (p == nullptr)
return;
q = nullptr;
std::swap(p, q);
*p = 42; // Null dereference
}
static int double_up = 0; // lets define this in another scope
int main(int argc, const char* argv[])
{
int uninitialized_value;
int shadowed = 5;
int unusedvariable = 0;
int mispell = 23; // bad speling behiend the keiboard
mispell++;
int __bad_name_of_variable = 0; // __ should be exlusive to compiler stuff
__bad_name_of_variable++;
// shadowed value, best practice
for (int shadowed = 0; shadowed < 10; shadowed++ )
{
printf("wrong type value = %llu\n", shadowed);
}
useretval(uninitialized_value);
int double_up = 23; // defined globally
if(shadowed & 4 == 0) // check operator precedence
{
printf("test");
}
if(shadowed = 1) // accidental assigning
{
shadowed = 12;
}
if(uninitialized_value == 6)
{
printf("test2");
}
int fx = 7; // could be const
int fy = 2; // could be const
float fval = fx / fy; // unpromoted float
fval = fval;
char* scalar = new char[100];
delete scalar; // not scalar delete
switch(shadowed)
{
case 1:
shadowed = 4;
break;
case 2:
{
int shadowed = 1;
}
break;
case 3:
default:
case 4:
printf("yessir");
}
Derived derived;
printf("Derived Class id: %d", derived.GetID());
#ifndef _WIN32
std::auto_ptr<Derived> a(new Derived); // deprecated
#endif
LeakSomeMemory();
try
{
auto str = retBadRef("test", "test2");
printf("strings", str.c_str()); // oops, forgot expected argument in varg/
}
catch (...)
{
}
// lets do something bad but very common
B b;
badfunc(b); // nasty
goodfunc(b);
int p = 3;
path_sensitive(&p, true);
local_analysis(&p,&p,true);
return 0;
}