Skip to content

Commit a9a2ca3

Browse files
authored
Add files via upload
1 parent 0257011 commit a9a2ca3

File tree

3 files changed

+256
-0
lines changed

3 files changed

+256
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| test.cpp:63:3:71:3 | { ... } | it is possible to dereference a pointer when accessing a buffer, since it is possible to throw an exception before the memory for the bufMyData is allocated |
2+
| test.cpp:178:3:180:3 | { ... } | perhaps a situation of uncertainty due to the repeated call of the delete function for the variable valData |
3+
| test.cpp:216:3:218:3 | { ... } | perhaps a situation of uncertainty due to the repeated call of the delete function for the variable valData |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
experimental/Security/CWE/CWE-476/DangerousUseOfExceptionBlocks.ql
Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
#define NULL ((void*)0)
2+
typedef unsigned long size_t;
3+
namespace std {
4+
enum class align_val_t : size_t {};
5+
}
6+
7+
class exception {};
8+
9+
void cleanFunction();
10+
11+
void* operator new(size_t, float);
12+
void* operator new[](size_t, float);
13+
void* operator new(size_t, std::align_val_t, float);
14+
void* operator new[](size_t, std::align_val_t, float);
15+
void operator delete(void*, float);
16+
void operator delete[](void*, float);
17+
void operator delete(void*, std::align_val_t, float);
18+
void operator delete[](void*, std::align_val_t, float);
19+
20+
struct myData
21+
{
22+
int sizeInt;
23+
char* buffer;
24+
};
25+
26+
struct myGlobalData
27+
{
28+
int sizeInt;
29+
myData** bufMyData;
30+
};
31+
32+
void allocData(myGlobalData * dataP) {
33+
for (size_t i = 0; i < dataP->sizeInt; i++)
34+
{
35+
dataP->bufMyData[i] = new myData;
36+
dataP->bufMyData[i]->sizeInt = 10;
37+
dataP->bufMyData[i]->buffer = new char[dataP->bufMyData[i]->sizeInt];
38+
}
39+
}
40+
41+
42+
void throwFunction(int a) {
43+
if (a == 5) throw "my exception!";
44+
}
45+
void throwFunction2(int a) {
46+
if (a == 5) throw exception();
47+
}
48+
void funcWork1b() {
49+
int a;
50+
myGlobalData *valData = new myGlobalData;
51+
52+
try {
53+
cleanFunction();
54+
throwFunction(a);
55+
valData->sizeInt = 10;
56+
valData->bufMyData = new myData*[valData->sizeInt];
57+
cleanFunction();
58+
allocData(valData);
59+
cleanFunction();
60+
61+
}
62+
catch (...)
63+
{
64+
for (size_t i = 0; i < valData->sizeInt; i++)
65+
{
66+
delete[] valData->bufMyData[i]->buffer; // BAD
67+
delete valData->bufMyData[i];
68+
}
69+
delete [] valData->bufMyData;
70+
delete valData;
71+
}
72+
}
73+
74+
void funcWork1() {
75+
int a;
76+
myGlobalData *valData = new myGlobalData;
77+
valData->sizeInt = 10;
78+
valData->bufMyData = new myData*[valData->sizeInt];
79+
try {
80+
cleanFunction();
81+
throwFunction(a);
82+
cleanFunction();
83+
allocData(valData);
84+
cleanFunction();
85+
86+
}
87+
catch (...)
88+
{
89+
for (size_t i = 0; i < valData->sizeInt; i++)
90+
{
91+
if (valData->bufMyData[i])
92+
delete[] valData->bufMyData[i]->buffer; // GOOD
93+
delete valData->bufMyData[i];
94+
}
95+
delete [] valData->bufMyData;
96+
delete valData;
97+
}
98+
}
99+
100+
void funcWork2() {
101+
int a;
102+
myGlobalData *valData = new myGlobalData;
103+
valData->sizeInt = 10;
104+
valData->bufMyData = new myData*[valData->sizeInt];
105+
try {
106+
do {
107+
cleanFunction();
108+
allocData(valData);
109+
cleanFunction();
110+
throwFunction(a);
111+
112+
}
113+
while(0);
114+
115+
}
116+
catch (...)
117+
{
118+
for (size_t i = 0; i < valData->sizeInt; i++)
119+
{
120+
delete[] valData->bufMyData[i]->buffer; // GOOD
121+
delete valData->bufMyData[i];
122+
}
123+
delete [] valData->bufMyData;
124+
delete valData;
125+
}
126+
}
127+
void funcWork3() {
128+
int a;
129+
myGlobalData *valData = new myGlobalData;
130+
valData->sizeInt = 10;
131+
valData->bufMyData = new myData*[valData->sizeInt];
132+
try {
133+
cleanFunction();
134+
allocData(valData);
135+
cleanFunction();
136+
throwFunction(a);
137+
138+
}
139+
catch (...)
140+
{
141+
for (size_t i = 0; i < valData->sizeInt; i++)
142+
{
143+
delete[] valData->bufMyData[i]->buffer; // GOOD
144+
delete valData->bufMyData[i];
145+
}
146+
delete [] valData->bufMyData;
147+
delete valData;
148+
}
149+
}
150+
151+
152+
void funcWork4() {
153+
int a;
154+
myGlobalData *valData;
155+
try {
156+
valData = new myGlobalData;
157+
cleanFunction();
158+
delete valData;
159+
valData = 0;
160+
throwFunction(a);
161+
}
162+
catch (...)
163+
{
164+
delete valData; // GOOD
165+
}
166+
}
167+
168+
void funcWork4b() {
169+
int a;
170+
myGlobalData *valData;
171+
try {
172+
valData = new myGlobalData;
173+
cleanFunction();
174+
delete valData;
175+
throwFunction(a);
176+
}
177+
catch (...)
178+
{
179+
delete valData; // BAD
180+
}
181+
}
182+
void funcWork5() {
183+
int a;
184+
myGlobalData *valData;
185+
try {
186+
valData = new myGlobalData;
187+
cleanFunction();
188+
delete valData;
189+
valData = 0;
190+
throwFunction2(a);
191+
}
192+
catch (const exception &) {
193+
delete valData;
194+
valData = 0;
195+
throw;
196+
}
197+
catch (...)
198+
{
199+
delete valData; // GOOD
200+
}
201+
}
202+
203+
void funcWork5b() {
204+
int a;
205+
myGlobalData *valData;
206+
try {
207+
valData = new myGlobalData;
208+
cleanFunction();
209+
throwFunction2(a);
210+
}
211+
catch (const exception &) {
212+
delete valData;
213+
throw;
214+
}
215+
catch (...)
216+
{
217+
delete valData; // BAD
218+
}
219+
}
220+
void funcWork6() {
221+
int a;
222+
int flagB = 0;
223+
myGlobalData *valData;
224+
try {
225+
valData = new myGlobalData;
226+
cleanFunction();
227+
throwFunction2(a);
228+
}
229+
catch (const exception &) {
230+
delete valData;
231+
flagB = 1;
232+
throw;
233+
}
234+
catch (...)
235+
{
236+
if(flagB == 0)
237+
delete valData; // GOOD
238+
}
239+
}
240+
241+
void runnerFunc()
242+
{
243+
funcWork1();
244+
funcWork1b();
245+
funcWork2();
246+
funcWork3();
247+
funcWork4();
248+
funcWork4b();
249+
funcWork5();
250+
funcWork5b();
251+
funcWork6();
252+
}

0 commit comments

Comments
 (0)