Skip to content

Commit 20e19ec

Browse files
authored
Add files via upload
1 parent 9071ba2 commit 20e19ec

3 files changed

Lines changed: 103 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
| test.cpp:30:15:30:26 | call to operator new[] | memory allocation error check is incorrect or missing |
2+
| test.cpp:38:9:38:20 | call to operator new[] | memory allocation error check is incorrect or missing |
3+
| test.cpp:50:13:50:38 | call to operator new[] | memory allocation error check is incorrect or missing |
4+
| test.cpp:51:22:51:47 | call to operator new[] | memory allocation error check is incorrect or missing |
5+
| test.cpp:53:18:53:43 | call to operator new[] | memory allocation error check is incorrect or missing |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.ql
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#define NULL ((void*)0)
2+
class exception {};
3+
4+
namespace std{
5+
struct nothrow_t {};
6+
typedef unsigned long size_t;
7+
class bad_alloc{
8+
const char* what() const throw();
9+
};
10+
extern const std::nothrow_t nothrow;
11+
}
12+
13+
using namespace std;
14+
15+
void* operator new(std::size_t _Size);
16+
void* operator new[](std::size_t _Size);
17+
void* operator new( std::size_t count, const std::nothrow_t& tag );
18+
void* operator new[]( std::size_t count, const std::nothrow_t& tag );
19+
20+
void badNew_0_0()
21+
{
22+
while (true) {
23+
new int[100];
24+
if(!(new int[100]))
25+
return;
26+
}
27+
}
28+
void badNew_0_1()
29+
{
30+
int * i = new int[100];
31+
if(i == 0)
32+
return;
33+
if(!i)
34+
return;
35+
if(i == NULL)
36+
return;
37+
int * j;
38+
j = new int[100];
39+
if(j == 0)
40+
return;
41+
if(!j)
42+
return;
43+
if(j == NULL)
44+
return;
45+
}
46+
void badNew_1_0()
47+
{
48+
try {
49+
while (true) {
50+
new(std::nothrow) int[100];
51+
int* p = new(std::nothrow) int[100];
52+
int* p1;
53+
p1 = new(std::nothrow) int[100];
54+
}
55+
} catch (const exception &){//const std::bad_alloc& e) {
56+
// std::cout << e.what() << '\n';
57+
}
58+
}
59+
void badNew_1_1()
60+
{
61+
while (true) {
62+
int* p = new(std::nothrow) int[100];
63+
new(std::nothrow) int[100];
64+
}
65+
}
66+
67+
void goodNew_0_0()
68+
{
69+
try {
70+
while (true) {
71+
new int[100];
72+
}
73+
} catch (const exception &){//const std::bad_alloc& e) {
74+
// std::cout << e.what() << '\n';
75+
}
76+
}
77+
78+
void goodNew_1_0()
79+
{
80+
while (true) {
81+
int* p = new(std::nothrow) int[100];
82+
if (p == nullptr) {
83+
// std::cout << "Allocation returned nullptr\n";
84+
break;
85+
}
86+
int* p1;
87+
p1 = new(std::nothrow) int[100];
88+
if (p1 == nullptr) {
89+
// std::cout << "Allocation returned nullptr\n";
90+
break;
91+
}
92+
if (new(std::nothrow) int[100] == nullptr) {
93+
// std::cout << "Allocation returned nullptr\n";
94+
break;
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)