Skip to content

Commit e82e887

Browse files
geoffw0Copilot
andcommitted
C++: Test rethrows without an active exception
Cover bare rethrows outside catches and exempt lexical or dynamic catch contexts for cpp/rethrow-no-exception. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 0a5b643 commit e82e887

3 files changed

Lines changed: 38 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.cpp:2:3:2:7 | re-throw exception | As there is no current exception, this rethrow expression will terminate the program. |
2+
| test.cpp:6:3:6:7 | re-throw exception | As there is no current exception, this rethrow expression will terminate the program. |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: Best Practices/Exceptions/AccidentalRethrow.ql
2+
postprocess: utils/test/InlineExpectationsTestQuery.ql
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
void rethrowOutsideCatch() {
2+
throw; // $ Alert
3+
}
4+
5+
void helperRethrow() {
6+
throw; // $ Alert
7+
}
8+
9+
void safeRethrowInCatch() {
10+
try {
11+
} catch (...) {
12+
throw;
13+
}
14+
}
15+
16+
// The function name matches "%exception%", so a rethrow here is assumed to
17+
// be intentional even though it is lexically and dynamically outside any
18+
// catch block.
19+
void rethrowException() {
20+
throw;
21+
}
22+
23+
// Not lexically inside a catch block, but every call to this function is
24+
// made from within a catch block, so the rethrow is assumed to be safe.
25+
void calledFromCatch() {
26+
throw;
27+
}
28+
29+
void triggersFromCatch() {
30+
try {
31+
} catch (...) {
32+
calledFromCatch();
33+
}
34+
}

0 commit comments

Comments
 (0)