Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Check for flow breaks before patching with the repair for S2142 #610

Merged
merged 3 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/main/java/sorald/processor/InterruptedExceptionProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import spoon.reflect.code.CtCatch;
import spoon.reflect.code.CtIf;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.code.CtLabelledFlowBreak;
import spoon.reflect.code.CtReturn;
import spoon.reflect.code.CtStatement;
import spoon.reflect.code.CtThrow;
Expand Down Expand Up @@ -44,26 +45,30 @@ protected void repairInternal(CtCatch element) {
* Return the last index that is safe to insert at, safe meaning that the interrupt is certain
* to actually be invoked.
*
* <p>This is either the first index at which a return statement or throw is found, or the index
* past the last index if no returns or throws are found.
* <p>This is either the first index at which some flow breaks are found, or the index past the
* last index if none found.
*/
private static int lastSafeInterruptIndex(CtBlock<?> block) {
for (int i = 0; i < block.getStatements().size(); i++) {
if (containsReturnOrThrow(block.getStatement(i))) {
if (containsReturnOrThrowOrLabelledFlowBreak(block.getStatement(i))) {
return i;
}
}
return block.getStatements().size();
}

/**
* Check if the given statement contains a return or throw.
* Check if the given statement contains a return, throw, or a labelled flow break.
*
* <p>TODO recursively check method declarations if there are method calls.
*/
private static boolean containsReturnOrThrow(CtStatement statement) {
private static boolean containsReturnOrThrowOrLabelledFlowBreak(CtStatement statement) {
return !statement
.filterChildren(e -> e instanceof CtReturn || e instanceof CtThrow)
.filterChildren(
e ->
e instanceof CtReturn
|| e instanceof CtThrow
|| e instanceof CtLabelledFlowBreak)
.list()
.isEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class CatchBlockWithBreak {
public static void main() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException exception) {
Thread.currentThread().interrupt();
break;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class CatchBlockWithBreak {
public static void main() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException exception) {
break;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class CatchBlockWithContinue {
public static void main() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException exception) {
Thread.currentThread().interrupt();
continue;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class CatchBlockWithContinue {
public static void main() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException exception) {
continue;
}
}
}
}