-when using the new operator to allocate memory, you need to pay attention to the different way of detecting errors. so ::operator new(std::size_t) throws an exception on error, and ::operator new(std::size_t, const std::nothrow_t &) returns zero on error. the programmer can get confused and check the error that occurs when allocating memory incorrectly. That can lead to an unhandled program termination or to a violation of the program logic.
+When using the new operator to allocate memory, you need to pay attention to the different ways of detecting errors. ::operator new(std::size_t) throws an exception on error, whereas ::operator new(std::size_t, const std::nothrow_t &) returns zero on error. The programmer can get confused and check the error that occurs when allocating memory incorrectly. That can lead to an unhandled program termination or to a violation of the program logic.
Loss of detection probably refers to use cases where memory allocation using your own solutions with strong nesting. It is also possible when using a buffer in the form of fields of different structures with the same names.
-We recommend using the error detection method, depending on the selected memory allocation method..
+Use the correct error detection method corresponding with the memory allocation.
-The following file demonstrates various approaches to detecting memory allocation errors using the new operator.
+The following example demonstrates various approaches to detecting memory allocation errors using the new operator.
From 5d163b4c1575115a5f1c14eba0145b74908ee292 Mon Sep 17 00:00:00 2001
From: ihsinme <61293369+ihsinme@users.noreply.github.com>
Date: Wed, 27 Jan 2021 15:05:58 +0300
Subject: [PATCH 06/14] Update
WrongInDetectingAndHandlingMemoryAllocationErrors.qhelp
---
.../WrongInDetectingAndHandlingMemoryAllocationErrors.qhelp | 2 --
1 file changed, 2 deletions(-)
diff --git a/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.qhelp b/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.qhelp
index 7be7fcd5b357..c3e543a1b589 100644
--- a/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.qhelp
+++ b/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.qhelp
@@ -5,8 +5,6 @@
When using the new operator to allocate memory, you need to pay attention to the different ways of detecting errors. ::operator new(std::size_t) throws an exception on error, whereas ::operator new(std::size_t, const std::nothrow_t &) returns zero on error. The programmer can get confused and check the error that occurs when allocating memory incorrectly. That can lead to an unhandled program termination or to a violation of the program logic.
-Loss of detection probably refers to use cases where memory allocation using your own solutions with strong nesting. It is also possible when using a buffer in the form of fields of different structures with the same names.
-
From 16d058f49865acb62d36c1a9b1af0aa6810d9eac Mon Sep 17 00:00:00 2001
From: ihsinme <61293369+ihsinme@users.noreply.github.com>
Date: Wed, 27 Jan 2021 15:06:57 +0300
Subject: [PATCH 07/14] Update
WrongInDetectingAndHandlingMemoryAllocationErrors.ql
---
.../CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.ql | 1 -
1 file changed, 1 deletion(-)
diff --git a/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.ql b/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.ql
index 383c8a1f1287..c32c14c1dd01 100644
--- a/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.ql
+++ b/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.ql
@@ -2,7 +2,6 @@
* @name Сonfusion In Detecting And Handling Memory Allocation Errors
* @description --::operator new(std::size_t) throws an exception on error, and ::operator new(std::size_t, const std::nothrow_t &) returns zero on error.
* --the programmer can get confused when check the error that occurs when allocating memory incorrectly.
- * --Making a call of this type may result in a zero byte being written just outside the buffer.
* @kind problem
* @id cpp/detect-and-handle-memory-allocation-errors
* @problem.severity warning
From bdfdcbd6735a841c4329a26b15bc4130c10e8f67 Mon Sep 17 00:00:00 2001
From: ihsinme <61293369+ihsinme@users.noreply.github.com>
Date: Wed, 27 Jan 2021 15:48:18 +0300
Subject: [PATCH 08/14] Update
WrongInDetectingAndHandlingMemoryAllocationErrors.ql
---
...rongInDetectingAndHandlingMemoryAllocationErrors.ql | 10 ++--------
1 file changed, 2 insertions(+), 8 deletions(-)
diff --git a/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.ql b/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.ql
index c32c14c1dd01..e165d61985b4 100644
--- a/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.ql
+++ b/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.ql
@@ -44,14 +44,8 @@ class WrongCheckErrorOperatorNew extends FunctionCall {
* Holds if handler `try ... catch` exists.
*/
predicate isExistsTryCatchBlock() {
- exists(TryStmt tb, AssignExpr aex, Initializer it |
- tb.getAChild*() = exp
- or
- exp = it.getExpr() and
- tb.getAChild*().(DeclStmt).getADeclaration() = it.getDeclaration()
- or
- aex.getAChild*() = exp and
- tb.getAChild*().(AssignExpr) = aex
+ exists(TryStmt ts |
+ this.getEnclosingStmt() = ts.getStmt().getAChild*()
)
}
From c8eeb5f73e70c567dc062c7a3b143faa8063ec1a Mon Sep 17 00:00:00 2001
From: ihsinme <61293369+ihsinme@users.noreply.github.com>
Date: Fri, 29 Jan 2021 11:51:15 +0300
Subject: [PATCH 09/14] Update
WrongInDetectingAndHandlingMemoryAllocationErrors.ql
---
.../WrongInDetectingAndHandlingMemoryAllocationErrors.ql | 3 +++
1 file changed, 3 insertions(+)
diff --git a/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.ql b/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.ql
index e165d61985b4..04a284d78463 100644
--- a/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.ql
+++ b/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.ql
@@ -22,6 +22,9 @@ class IfCompareWithZero extends IfStmt {
or
this.getCondition().(NEExpr).getAChild().getValue() = "0" and
this.hasElse()
+ or
+ this.getCondition().(NEExpr).getAChild().getValue() = "0" and
+ this.getThen().getAChild*() instanceof ReturnStmt
}
}
From bdbf5a4fae29511be7d64039847a1a70a7e61545 Mon Sep 17 00:00:00 2001
From: ihsinme <61293369+ihsinme@users.noreply.github.com>
Date: Fri, 29 Jan 2021 13:41:45 +0300
Subject: [PATCH 10/14] Apply suggestions from code review
Co-authored-by: Geoffrey White <40627776+geoffw0@users.noreply.github.com>
---
.../WrongInDetectingAndHandlingMemoryAllocationErrors.cpp | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.cpp b/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.cpp
index 0232fc131ebd..df69886e97bd 100644
--- a/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.cpp
+++ b/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.cpp
@@ -8,7 +8,9 @@ void badFunction(const int *source, std::size_t length) noexcept {
void goodFunction(const int *source, std::size_t length) noexcept {
try {
int * dest = new int[length];
- } catch(std::bad_alloc)
+ } catch(std::bad_alloc) {
+ // ...
+ }
std::memset(dest, 0, length);
// ..
}
@@ -16,7 +18,9 @@ void goodFunction(const int *source, std::size_t length) noexcept {
void badFunction(const int *source, std::size_t length) noexcept {
try {
int * dest = new (std::nothrow) int[length];
- } catch(std::bad_alloc)
+ } catch(std::bad_alloc) {
+ // ...
+ }
std::memset(dest, 0, length);
// ..
}
From 2b946aee5a3ff0c7d5e284e2ff7527e4dd79cda6 Mon Sep 17 00:00:00 2001
From: ihsinme <61293369+ihsinme@users.noreply.github.com>
Date: Sun, 31 Jan 2021 15:21:54 +0300
Subject: [PATCH 11/14] Update
WrongInDetectingAndHandlingMemoryAllocationErrors.ql
---
.../WrongInDetectingAndHandlingMemoryAllocationErrors.ql | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.ql b/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.ql
index 04a284d78463..403d8388b179 100644
--- a/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.ql
+++ b/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.ql
@@ -58,7 +58,7 @@ class WrongCheckErrorOperatorNew extends FunctionCall {
predicate isExistsIfCondition() {
exists(IfCompareWithZero ifc, AssignExpr aex, Initializer it |
// call `operator new` directly from the condition of `operator if`.
- this = ifc.getCondition().getAChild()
+ this = ifc.getCondition().getAChild*()
or
// check results call `operator new` with variable appropriation
postDominates(ifc, this) and
From 2131f3580167e37e23daf77313bc4cde0a6b4615 Mon Sep 17 00:00:00 2001
From: ihsinme <61293369+ihsinme@users.noreply.github.com>
Date: Thu, 4 Feb 2021 15:41:40 +0300
Subject: [PATCH 12/14] Update
WrongInDetectingAndHandlingMemoryAllocationErrors.ql
---
.../WrongInDetectingAndHandlingMemoryAllocationErrors.ql | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.ql b/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.ql
index 403d8388b179..0181744bb865 100644
--- a/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.ql
+++ b/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.ql
@@ -1,5 +1,5 @@
/**
- * @name Сonfusion In Detecting And Handling Memory Allocation Errors
+ * @name onfusion In Detecting And Handling Memory Allocation Errors
* @description --::operator new(std::size_t) throws an exception on error, and ::operator new(std::size_t, const std::nothrow_t &) returns zero on error.
* --the programmer can get confused when check the error that occurs when allocating memory incorrectly.
* @kind problem
@@ -47,9 +47,7 @@ class WrongCheckErrorOperatorNew extends FunctionCall {
* Holds if handler `try ... catch` exists.
*/
predicate isExistsTryCatchBlock() {
- exists(TryStmt ts |
- this.getEnclosingStmt() = ts.getStmt().getAChild*()
- )
+ exists(TryStmt ts | this.getEnclosingStmt() = ts.getStmt().getAChild*())
}
/**
From a43167faf7cbb82cf0d4f3eaa5ca492818e7a9f0 Mon Sep 17 00:00:00 2001
From: ihsinme <61293369+ihsinme@users.noreply.github.com>
Date: Thu, 4 Feb 2021 15:44:28 +0300
Subject: [PATCH 13/14] Update
WrongInDetectingAndHandlingMemoryAllocationErrors.qhelp
---
.../WrongInDetectingAndHandlingMemoryAllocationErrors.qhelp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.qhelp b/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.qhelp
index c3e543a1b589..9e6cb2d89cec 100644
--- a/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.qhelp
+++ b/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.qhelp
@@ -3,7 +3,7 @@
"qhelp.dtd">
-When using the new operator to allocate memory, you need to pay attention to the different ways of detecting errors. ::operator new(std::size_t) throws an exception on error, whereas ::operator new(std::size_t, const std::nothrow_t &) returns zero on error. The programmer can get confused and check the error that occurs when allocating memory incorrectly. That can lead to an unhandled program termination or to a violation of the program logic.
+When using the new operator to allocate memory, you need to pay attention to the different ways of detecting errors. ::operator new(std::size_t) throws an exception on error, whereas ::operator new(std::size_t, const std::nothrow_t &) returns zero on error. The programmer can get confused and check the error that occurs when allocating memory incorrectly. That can lead to an unhandled program termination or to a violation of the program logic.
From 43045c1f034b9fe2eeeb7f6746f02a7392921725 Mon Sep 17 00:00:00 2001
From: ihsinme <61293369+ihsinme@users.noreply.github.com>
Date: Thu, 4 Feb 2021 15:47:16 +0300
Subject: [PATCH 14/14] Update
WrongInDetectingAndHandlingMemoryAllocationErrors.ql
---
.../WrongInDetectingAndHandlingMemoryAllocationErrors.ql | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.ql b/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.ql
index 0181744bb865..dd9c16fac114 100644
--- a/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.ql
+++ b/cpp/ql/src/experimental/Security/CWE/CWE-570/WrongInDetectingAndHandlingMemoryAllocationErrors.ql
@@ -1,5 +1,5 @@
/**
- * @name onfusion In Detecting And Handling Memory Allocation Errors
+ * @name Detect And Handle Memory Allocation Errors
* @description --::operator new(std::size_t) throws an exception on error, and ::operator new(std::size_t, const std::nothrow_t &) returns zero on error.
* --the programmer can get confused when check the error that occurs when allocating memory incorrectly.
* @kind problem