Skip to content

Commit 90e8368

Browse files
committed
C++: Properly handle conversions in convertedExprMayThrow. This recursive implementation idea is stolen from convertedExprMightOverflow in SimpleRangeAnalysis.
1 parent 7adb7b6 commit 90e8368

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

cpp/ql/src/experimental/Security/CWE/CWE-570/IncorrectAllocationErrorHandling.ql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ predicate stmtMayThrow(Stmt stmt) {
107107
}
108108

109109
/** Holds if the evaluation of `e` (including conversions) may throw an exception. */
110-
predicate convertedExprMayThrow(Expr e) { exprMayThrow(e.getFullyConverted()) }
110+
predicate convertedExprMayThrow(Expr e) {
111+
exprMayThrow(e)
112+
or
113+
convertedExprMayThrow(e.getConversion())
114+
}
111115

112116
/** Holds if the evaluation of `e` may throw an exception. */
113117
predicate exprMayThrow(Expr e) {

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-570/semmle/tests/IncorrectAllocationErrorHandling.expected

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,4 @@
1414
| test.cpp:93:15:93:41 | new[] | This allocation cannot throw. $@ is unnecessary. | test.cpp:97:36:98:3 | { ... } | This catch block |
1515
| test.cpp:96:10:96:36 | new[] | This allocation cannot throw. $@ is unnecessary. | test.cpp:97:36:98:3 | { ... } | This catch block |
1616
| test.cpp:151:9:151:24 | new | This allocation cannot throw. $@ is unnecessary. | test.cpp:152:15:152:18 | { ... } | This catch block |
17-
| test.cpp:199:15:199:35 | new | This allocation cannot throw. $@ is unnecessary. | test.cpp:201:16:201:19 | { ... } | This catch block |
1817
| test.cpp:212:14:212:34 | new | This allocation cannot throw. $@ is unnecessary. | test.cpp:213:34:213:36 | { ... } | This catch block |
19-
| test.cpp:225:23:225:29 | new | This allocation cannot throw. $@ is unnecessary. | test.cpp:226:34:226:36 | { ... } | This catch block |

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-570/semmle/tests/test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ void good_new_with_throwing_call() {
196196

197197
void bad_new_with_nonthrowing_call() {
198198
try {
199-
int* p1 = new(std::nothrow) int; // BAD
199+
int* p1 = new(std::nothrow) int; // BAD [NOT DETECTED]
200200
calls_non_throwing(p1);
201201
} catch(...) { }
202202

@@ -222,6 +222,6 @@ void good_new_catch_exception_in_assignment() {
222222

223223
void good_new_catch_exception_in_conversion() {
224224
try {
225-
long* p = (long*) new int; // GOOD [FALSE POSITIVE]
225+
long* p = (long*) new int; // GOOD
226226
} catch(const std::bad_alloc&) { }
227227
}

0 commit comments

Comments
 (0)