Skip to content

Commit 62fb216

Browse files
committed
CPP: Fix false positive.
1 parent 629d127 commit 62fb216

3 files changed

Lines changed: 16 additions & 7 deletions

File tree

cpp/ql/src/Likely Bugs/ContinueInFalseLoop.ql

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,25 @@ DoStmt getAFalseLoop() {
2121
}
2222

2323
/**
24-
* Gets a `do` ... `while` loop surrounding a statement.
24+
* Gets a `do` ... `while` loop surrounding a statement. This is blocked by a
25+
* `switch` statement, since a `continue` inside a `switch` inside a loop may be
26+
* jusitifed (`continue` breaks out of the loop whereas `break` only escapes the
27+
* `switch`).
2528
*/
2629
DoStmt enclosingLoop(Stmt s) {
2730
exists(Stmt parent |
2831
parent = s.getParent() and
29-
if parent instanceof Loop then
30-
result = parent
31-
else
32-
result = enclosingLoop(parent))
32+
(
33+
(
34+
parent instanceof Loop and
35+
result = parent
36+
) or (
37+
not parent instanceof Loop and
38+
not parent instanceof SwitchStmt and
39+
result = enclosingLoop(parent)
40+
)
41+
)
42+
)
3343
}
3444

3545
from DoStmt loop, ContinueStmt continue
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
| test.cpp:13:4:13:12 | continue; | This 'continue' never re-runs the loop - the $@ is always false. | test.cpp:16:11:16:15 | 0 | loop condition |
22
| test.cpp:59:5:59:13 | continue; | This 'continue' never re-runs the loop - the $@ is always false. | test.cpp:62:12:62:16 | 0 | loop condition |
3-
| test.cpp:88:4:88:12 | continue; | This 'continue' never re-runs the loop - the $@ is always false. | test.cpp:93:11:93:11 | 0 | loop condition |

cpp/ql/test/query-tests/Likely Bugs/ContinueInFalseLoop/test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ void test1(int x)
8585
default:
8686
// do [2]
8787

88-
continue; // break out of the loop entirely, skipping [3] [FALSE POSITIVE]
88+
continue; // GOOD; break out of the loop entirely, skipping [3]
8989
};
9090

9191
// do [3]

0 commit comments

Comments
 (0)