From ff1686efd818ffc8567e9bc0e71003b5a2e35d42 Mon Sep 17 00:00:00 2001 From: SuperCoder79 <25208576+SuperCoder7979@users.noreply.github.com> Date: Wed, 23 Nov 2022 11:56:48 -0500 Subject: [PATCH] Fix try with resources being matched in improper cases --- .../decompiler/TryWithResourcesProcessor.java | 21 +++++++++++++++++++ .../pkg/TestTryWithResourcesFakeTrigger.dec | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/TryWithResourcesProcessor.java b/src/org/jetbrains/java/decompiler/modules/decompiler/TryWithResourcesProcessor.java index 19c3bcb2e9..84cbd2fa9c 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/TryWithResourcesProcessor.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/TryWithResourcesProcessor.java @@ -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; @@ -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 @@ -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()) { @@ -192,6 +210,9 @@ public static boolean makeTryWithResourceJ11(CatchStatement tryStatement) { } Set destinations = findExitpoints(tryStatement); + if (destinations.isEmpty()) { + return false; + } Statement check = tryStatement; List preds = new ArrayList<>(); diff --git a/testData/results/pkg/TestTryWithResourcesFakeTrigger.dec b/testData/results/pkg/TestTryWithResourcesFakeTrigger.dec index b2f9dcf516..8f73334e01 100644 --- a/testData/results/pkg/TestTryWithResourcesFakeTrigger.dec +++ b/testData/results/pkg/TestTryWithResourcesFakeTrigger.dec @@ -96,4 +96,4 @@ Not mapped: 16 18 30 -33 +33 \ No newline at end of file