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
170 lines (131 loc) · 2.79 KB
/
test.cpp
File metadata and controls
170 lines (131 loc) · 2.79 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
typedef unsigned int size_t;
void *malloc(size_t size);
void free(void *ptr);
// --- myClass1 ---
class myClass1
{
public:
myClass1();
~myClass1();
void method1();
void method2();
private:
int *array1, *array2, *array3, *array4, *array5, *array6, *array7, *array8;
};
myClass1 :: myClass1()
{
array1 = (int *)malloc(sizeof(int) * 100);
array2 = (int *)malloc(sizeof(int) * 100);
array3 = (int *)malloc(sizeof(int) * 100);
array4 = (int *)malloc(sizeof(int) * 100); // never freed
free(array1);
}
myClass1 :: ~myClass1()
{
free(array2);
free(array7);
}
void myClass1 :: method1()
{
array5 = (int *)malloc(sizeof(int) * 100);
array6 = (int *)malloc(sizeof(int) * 100);
array7 = (int *)malloc(sizeof(int) * 100);
array8 = (int *)malloc(sizeof(int) * 100); // never freed
free(array3);
free(array5);
}
void myClass1 :: method2()
{
free(array6);
}
// --- myClass2 ---
class myClass2
{
public:
myClass2();
~myClass2();
void method1();
void method2();
private:
int *array1, *array2, *array3, *array4, *array5, *array6, *array7, *array8;
};
myClass2 :: myClass2()
{
array1 = (int *)malloc(sizeof(int) * 100);
array2 = (int *)malloc(sizeof(int) * 100);
array3 = (int *)malloc(sizeof(int) * 100);
array4 = (int *)malloc(sizeof(int) * 100); // never freed
free(array1);
}
myClass2 :: ~myClass2()
{
free(array2);
free(array7);
}
void myClass2 :: method1()
{
array5 = (int *)malloc(sizeof(int) * 100);
array6 = (int *)malloc(sizeof(int) * 100);
array7 = (int *)malloc(sizeof(int) * 100);
array8 = (int *)malloc(sizeof(int) * 100); // never freed
free(array3);
free(array5);
}
void myClass2 :: method2()
{
free(array6);
}
int main()
{
{
myClass1 mc1;
mc1.method1();
mc1.method2();
}
{
myClass2 *mc2;
mc2 = new myClass2();
mc2->method1();
mc2->method2();
delete mc2;
}
{
void *v1 = malloc(100);
int *i2 = (int *)malloc(100);
void *v3 = malloc(100);
int *i3 = (int *)v3;
void *v4 = malloc(100);
free(v1);
free(i2);
free(i3);
free((int *)v4);
}
return 0;
}
// --- placement new ---
namespace std {
typedef unsigned long size_t;
struct nothrow_t {};
extern const nothrow_t nothrow;
}
void* operator new (std::size_t size, void* ptr) noexcept;
void* operator new[](std::size_t size, void* ptr) noexcept;
void* operator new(std::size_t size, const std::nothrow_t&) noexcept;
void* operator new[](std::size_t size, const std::nothrow_t&) noexcept;
int overloadedNew() {
char buf[sizeof(int)];
new(&buf[0]) int(5); // GOOD
int five = *(int*)buf;
new(buf) int[1]; // GOOD
*(int*)buf = 4;
new(std::nothrow) int(3); // BAD
new(std::nothrow) int[2]; // BAD
}
// --- strdup ---
char *strdup(const char *s1);
void output_msg(const char *msg);
void test_strdup() {
char msg[] = "OctoCat";
char *cpy = strdup(msg); // BAD
output_msg(cpy);
}