Add files via upload

This commit is contained in:
ihsinme
2021-01-25 00:09:55 +03:00
committed by GitHub
parent 9071ba2f99
commit 20e19ec467
3 changed files with 103 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
| test.cpp:30:15:30:26 | call to operator new[] | memory allocation error check is incorrect or missing |
| test.cpp:38:9:38:20 | call to operator new[] | memory allocation error check is incorrect or missing |
| test.cpp:50:13:50:38 | call to operator new[] | memory allocation error check is incorrect or missing |
| test.cpp:51:22:51:47 | call to operator new[] | memory allocation error check is incorrect or missing |
| test.cpp:53:18:53:43 | call to operator new[] | memory allocation error check is incorrect or missing |

View File

@@ -0,0 +1 @@
experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.ql

View File

@@ -0,0 +1,97 @@
#define NULL ((void*)0)
class exception {};
namespace std{
struct nothrow_t {};
typedef unsigned long size_t;
class bad_alloc{
const char* what() const throw();
};
extern const std::nothrow_t nothrow;
}
using namespace std;
void* operator new(std::size_t _Size);
void* operator new[](std::size_t _Size);
void* operator new( std::size_t count, const std::nothrow_t& tag );
void* operator new[]( std::size_t count, const std::nothrow_t& tag );
void badNew_0_0()
{
while (true) {
new int[100];
if(!(new int[100]))
return;
}
}
void badNew_0_1()
{
int * i = new int[100];
if(i == 0)
return;
if(!i)
return;
if(i == NULL)
return;
int * j;
j = new int[100];
if(j == 0)
return;
if(!j)
return;
if(j == NULL)
return;
}
void badNew_1_0()
{
try {
while (true) {
new(std::nothrow) int[100];
int* p = new(std::nothrow) int[100];
int* p1;
p1 = new(std::nothrow) int[100];
}
} catch (const exception &){//const std::bad_alloc& e) {
// std::cout << e.what() << '\n';
}
}
void badNew_1_1()
{
while (true) {
int* p = new(std::nothrow) int[100];
new(std::nothrow) int[100];
}
}
void goodNew_0_0()
{
try {
while (true) {
new int[100];
}
} catch (const exception &){//const std::bad_alloc& e) {
// std::cout << e.what() << '\n';
}
}
void goodNew_1_0()
{
while (true) {
int* p = new(std::nothrow) int[100];
if (p == nullptr) {
// std::cout << "Allocation returned nullptr\n";
break;
}
int* p1;
p1 = new(std::nothrow) int[100];
if (p1 == nullptr) {
// std::cout << "Allocation returned nullptr\n";
break;
}
if (new(std::nothrow) int[100] == nullptr) {
// std::cout << "Allocation returned nullptr\n";
break;
}
}
}