Skip to content

Commit fc7d9c2

Browse files
committed
C++: Fix missing result by properly specifying that the function with unknown code actually didn't throw an exception.
1 parent 90e8368 commit fc7d9c2

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
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 |
1718
| test.cpp:212:14:212:34 | new | This allocation cannot throw. $@ is unnecessary. | test.cpp:213:34:213:36 | { ... } | This catch block |

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,10 @@ void good_placement_new_with_exception_handling() {
158158
catch (...) { }
159159
}
160160

161-
int rand();
161+
int unknown_value_without_exceptions() noexcept;
162162

163163
void may_throw() {
164-
if(rand()) {
164+
if(unknown_value_without_exceptions()) {
165165
throw "bad luck exception!";
166166
}
167167
}
@@ -170,11 +170,11 @@ void unknown_code_that_may_throw(int*);
170170
void unknown_code_that_will_not_throw(int*) noexcept;
171171

172172
void calls_throwing_code(int* p) {
173-
if(rand()) unknown_code_that_may_throw(p);
173+
if(unknown_value_without_exceptions()) unknown_code_that_may_throw(p);
174174
}
175175

176176
void calls_non_throwing(int* p) {
177-
if (rand()) unknown_code_that_will_not_throw(p);
177+
if (unknown_value_without_exceptions()) unknown_code_that_will_not_throw(p);
178178
}
179179

180180
void good_new_with_throwing_call() {
@@ -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 [NOT DETECTED]
199+
int* p1 = new(std::nothrow) int; // BAD
200200
calls_non_throwing(p1);
201201
} catch(...) { }
202202

0 commit comments

Comments
 (0)