Skip to content

Commit 80fd5b2

Browse files
authored
Merge pull request #2175 from aschackmull/java/continue-in-false-loop
Java: Port C++ query cpp/continue-in-false-loop to Java.
2 parents 01035f1 + 3462624 commit 80fd5b2

File tree

6 files changed

+140
-0
lines changed

6 files changed

+140
-0
lines changed

change-notes/1.23/analysis-java.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
The following changes in version 1.23 affect Java analysis in all applications.
44

5+
## New queries
6+
7+
| **Query** | **Tags** | **Purpose** |
8+
|-----------------------------|-----------|--------------------------------------------------------------------|
9+
| Continue statement that does not continue (`java/continue-in-false-loop`) | correctness | Finds `continue` statements in `do { ... } while (false)` loops. |
10+
511
## Changes to existing queries
612

713
| **Query** | **Expected impact** | **Change** |
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
6+
7+
<overview>
8+
<p>A <code>continue</code> statement only re-runs the loop if the loop condition is true. Therefore using <code>continue</code> in a loop with a constant false condition will never cause the loop body to be re-run, which is misleading.
9+
</p>
10+
11+
</overview>
12+
<recommendation>
13+
14+
<p>Replace the <code>continue</code> statement with a <code>break</code> statement if the intent is to break from the loop.
15+
</p>
16+
17+
</recommendation>
18+
19+
<references>
20+
<li>
21+
Java Language Specification:
22+
<a href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.13">14.13 The do Statement</a>.
23+
</li>
24+
</references>
25+
</qhelp>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @name Continue statement that does not continue
3+
* @description A 'continue' statement only re-runs the loop if the
4+
* loop-condition is true. Therefore using 'continue' in a loop
5+
* with a constant false condition is misleading and usually a
6+
* bug.
7+
* @kind problem
8+
* @id java/continue-in-false-loop
9+
* @problem.severity warning
10+
* @precision high
11+
* @tags correctness
12+
*/
13+
14+
import java
15+
16+
from DoStmt do, ContinueStmt continue
17+
where
18+
do.getCondition().(BooleanLiteral).getBooleanValue() = false and
19+
continue.(JumpStmt).getTarget() = do
20+
select continue, "This 'continue' never re-runs the loop - the $@ is always false.",
21+
do.getCondition(), "loop condition"
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
public class A {
2+
3+
public interface Cond {
4+
boolean cond();
5+
}
6+
7+
void test1(int x, Cond c) {
8+
int i;
9+
10+
// --- do loops ---
11+
12+
do {
13+
if (c.cond())
14+
continue; // BAD
15+
if (c.cond())
16+
break;
17+
} while (false);
18+
19+
do {
20+
if (c.cond())
21+
continue;
22+
if (c.cond())
23+
break;
24+
} while (true);
25+
26+
do {
27+
if (c.cond())
28+
continue;
29+
if (c.cond())
30+
break;
31+
} while (c.cond());
32+
33+
// --- while, for loops ---
34+
35+
while (false) {
36+
if (c.cond())
37+
continue; // GOOD [never reached, if the condition changed so it was then the result would no longer apply]
38+
if (c.cond())
39+
break;
40+
}
41+
42+
for (i = 0; false; i++) {
43+
if (c.cond())
44+
continue; // GOOD [never reached, if the condition changed so it was then the result would no longer apply]
45+
if (c.cond())
46+
break;
47+
}
48+
49+
// --- nested loops ---
50+
51+
do {
52+
do {
53+
if (c.cond())
54+
continue; // BAD
55+
if (c.cond())
56+
break;
57+
} while (false);
58+
if (c.cond())
59+
break;
60+
} while (true);
61+
62+
do {
63+
do {
64+
if (c.cond())
65+
continue; // GOOD
66+
if (c.cond())
67+
break;
68+
} while (true);
69+
} while (false);
70+
71+
do {
72+
switch (x) {
73+
case 1:
74+
// do [1]
75+
break; // break out of the switch
76+
default:
77+
// do [2]
78+
// break out of the loop entirely, skipping [3]
79+
continue; // BAD; labelled break is better
80+
};
81+
// do [3]
82+
} while (false);
83+
}
84+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| A.java:14:9:14:17 | stmt | This 'continue' never re-runs the loop - the $@ is always false. | A.java:17:14:17:18 | false | loop condition |
2+
| A.java:54:11:54:19 | stmt | This 'continue' never re-runs the loop - the $@ is always false. | A.java:57:16:57:20 | false | loop condition |
3+
| A.java:79:9:79:17 | stmt | This 'continue' never re-runs the loop - the $@ is always false. | A.java:82:14:82:18 | false | loop condition |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Likely Bugs/Statements/ContinueInFalseLoop.ql

0 commit comments

Comments
 (0)