Skip to content

Commit

Permalink
Fix try with resources being matched in improper cases
Browse files Browse the repository at this point in the history
  • Loading branch information
jaskarth committed Jul 15, 2023
1 parent 0690cf2 commit ff1686e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ public static boolean makeTryWithResourceJ11(CatchStatement tryStatement) {
return false;
}

if (!tryStatement.getVars().get(0).getVarType().value.equals("java/lang/Throwable")) {
return false;
}

Statement inner = tryStatement.getStats().get(1); // Get catch block

VarExprent closeable = null;
Expand All @@ -125,6 +129,11 @@ public static boolean makeTryWithResourceJ11(CatchStatement tryStatement) {
return false;
}

CatchStatement innerTry = (CatchStatement)inner;
if (!innerTry.getVars().get(0).getVarType().value.equals("java/lang/Throwable")) {
return false;
}

Statement inTry = inner.getStats().get(0);

// Catch block contains a basic block inside which has the closeable invocation
Expand Down Expand Up @@ -164,6 +173,15 @@ public static boolean makeTryWithResourceJ11(CatchStatement tryStatement) {

// Process try catch inside of if statement
if (inner instanceof CatchStatement && !inner.getStats().isEmpty()) {
if (inner.getStats().isEmpty()) {
return false;
}

CatchStatement innerTry = (CatchStatement)inner;
if (!innerTry.getVars().get(0).getVarType().value.equals("java/lang/Throwable")) {
return false;
}

Statement inTry = inner.getStats().get(0);

if (inTry instanceof BasicBlockStatement && !inTry.getExprents().isEmpty()) {
Expand Down Expand Up @@ -192,6 +210,9 @@ public static boolean makeTryWithResourceJ11(CatchStatement tryStatement) {
}

Set<Statement> destinations = findExitpoints(tryStatement);
if (destinations.isEmpty()) {
return false;
}

Statement check = tryStatement;
List<StatEdge> preds = new ArrayList<>();
Expand Down
2 changes: 1 addition & 1 deletion testData/results/pkg/TestTryWithResourcesFakeTrigger.dec
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,4 @@ Not mapped:
16
18
30
33
33

0 comments on commit ff1686e

Please sign in to comment.