Skip to content

Commit 7bcc437

Browse files
author
Robert Marsh
committed
C++: accept loops with arbitrary labels or cases
1 parent b40219b commit 7bcc437

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

cpp/ql/src/Critical/DeadCodeGoto.ql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ where b.getStmt(i) = js
2727
and not s instanceof SwitchCase
2828
// the next statement isn't breaking out of a switch
2929
and not s.(BreakStmt).getBreakable() instanceof SwitchStmt
30-
// the jump isn't a goto into the body of the next statement
31-
and not exists (LabelStmt ls | s.(Loop).getStmt().getAChild*() = ls | ls.getName() = js.(GotoStmt).getName())
30+
// the next statement isn't a loop that can be jumped into
31+
and not exists (LabelStmt ls | s.(Loop).getStmt().getAChild*() = ls)
32+
and not exists (SwitchCase sc | s.(Loop).getStmt().getAChild*() = sc)
3233
select js, "This statement makes $@ unreachable.", s, s.toString()

cpp/ql/test/query-tests/Critical/DeadCodeGoto/test.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,34 @@ int test5(int x, int y) {
5050
return x;
5151
}
5252

53+
void test6(int x, int cond) {
54+
if (cond) {
55+
x++;
56+
} else goto end; // GOOD
57+
x++;
58+
end:
59+
}
60+
61+
void test7(int x, int cond) {
62+
if (cond)
63+
{
64+
goto target;
65+
}
66+
goto somewhere_else; // GOOD
67+
while (x < 10) // not dead code
68+
{
69+
target:
70+
x++;
71+
}
72+
somewhere_else:
73+
switch (1)
74+
{
75+
goto end;
76+
while (x < 10) // not dead code
77+
{
78+
case 1:
79+
x++;
80+
} break;
81+
}
82+
end:
83+
}

0 commit comments

Comments
 (0)