Skip to content

Commit f80754b

Browse files
Check for loops whose body is an empty statement
`for (...; ...; ...) ;` has no body, so `CtFor.getBody()` is null and looking for a `continue` in it crashed with a null dereference. A missing body is now treated as an empty one: the condition and the update are still checked. `while`, `do` and for-each loops with an empty statement body already verified; the regression covers them too. Fixes #395. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent dd7ca3d commit f80754b

3 files changed

Lines changed: 50 additions & 2 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package testSuite;
2+
3+
import java.util.Iterator;
4+
5+
import liquidjava.specification.Refinement;
6+
7+
// Loops whose body is an empty statement (#395)
8+
@SuppressWarnings("unused")
9+
public class CorrectEmptyLoopBody {
10+
static int total(int[] counts) {
11+
int total = 0;
12+
for (int i = 0; i < counts.length; total += counts[i], i++)
13+
;
14+
return total;
15+
}
16+
17+
static void countDown() {
18+
@Refinement("_ >= 0")
19+
int n = 10;
20+
for (; n > 0; n--)
21+
;
22+
}
23+
24+
static void skip(Iterator<String> it, int[] xs) {
25+
while (it.hasNext() && it.next() != null)
26+
;
27+
do
28+
;
29+
while (it.hasNext() && it.next() != null);
30+
for (int x : xs)
31+
;
32+
}
33+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package testSuite;
2+
3+
import liquidjava.specification.Refinement;
4+
5+
// The update of a for loop whose body is an empty statement is still checked (#395)
6+
@SuppressWarnings("unused")
7+
public class ErrorEmptyLoopBody {
8+
static void countDown() {
9+
@Refinement("_ >= 0")
10+
int n = 10;
11+
for (; n > 0; n -= 2) // Expect: Refinement Error
12+
;
13+
}
14+
}

‎liquidjava-verifier/src/main/java/liquidjava/processor/refinement_checker/RefinementTypeChecker.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -678,8 +678,9 @@ public void visitCtFor(CtFor forLoop) {
678678
scan(forLoop.getExpression());
679679
assumeLoopCondition(forLoop.getExpression());
680680
List<RefinedVariable> pathVariables = vcChecker.getPathVariables();
681-
scan(forLoop.getBody());
682-
if (!forLoop.getBody().getElements(new TypeFilter<>(CtContinue.class)).isEmpty()) {
681+
CtStatement body = forLoop.getBody(); // null for an empty statement body, as in for (...);
682+
scan(body);
683+
if (body != null && !body.getElements(new TypeFilter<>(CtContinue.class)).isEmpty()) {
683684
// a continue reaches the update from any point of the body, skipping its facts and assignments
684685
vcChecker.restorePathVariables(pathVariables);
685686
havocChangedIn(forLoop);

0 commit comments

Comments
 (0)