From 58998089a6536cfdd19b884b48aa4187fbf8b72c Mon Sep 17 00:00:00 2001 From: Skylot Date: Thu, 7 Aug 2014 22:20:47 +0400 Subject: [PATCH] core: redone 'if' structure checking --- .../dex/visitors/regions/IfMakerHelper.java | 26 +++------ .../tests/internal/others/TestIfInTry.java | 57 +++++++++++++++++++ 2 files changed, 65 insertions(+), 18 deletions(-) create mode 100644 jadx-core/src/test/java/jadx/tests/internal/others/TestIfInTry.java diff --git a/jadx-core/src/main/java/jadx/core/dex/visitors/regions/IfMakerHelper.java b/jadx-core/src/main/java/jadx/core/dex/visitors/regions/IfMakerHelper.java index d8e8ffb01..91e7d162b 100644 --- a/jadx-core/src/main/java/jadx/core/dex/visitors/regions/IfMakerHelper.java +++ b/jadx-core/src/main/java/jadx/core/dex/visitors/regions/IfMakerHelper.java @@ -50,26 +50,16 @@ public class IfMakerHelper { boolean badThen = !allPathsFromIf(thenBlock, info); boolean badElse = !allPathsFromIf(elseBlock, info); if (badThen && badElse) { + LOG.debug("Stop processing blocks after 'if': {}, method: {}", info, mth); return null; } - if (badThen || badElse) { - if (badElse && isPathExists(thenBlock, elseBlock)) { - info = new IfInfo(info.getCondition(), thenBlock, null); - info.setOutBlock(elseBlock); - } else if (badThen && isPathExists(elseBlock, thenBlock)) { - info = IfInfo.invert(info); - info = new IfInfo(info.getCondition(), info.getThenBlock(), null); - info.setOutBlock(thenBlock); - } else if (badElse) { - info = new IfInfo(info.getCondition(), thenBlock, null); - info.setOutBlock(null); - LOG.debug("Stop processing blocks after bad 'else' in 'if': {}, method: {}", info, mth); - } else { - info = IfInfo.invert(info); - info = new IfInfo(info.getCondition(), info.getThenBlock(), null); - info.setOutBlock(null); - LOG.debug("Stop processing blocks after bad 'then' in 'if': {}, method: {}", info, mth); - } + if (badElse) { + info = new IfInfo(info.getCondition(), thenBlock, null); + info.setOutBlock(elseBlock); + } else if (badThen) { + info = IfInfo.invert(info); + info = new IfInfo(info.getCondition(), elseBlock, null); + info.setOutBlock(thenBlock); } else { List thenSC = thenBlock.getCleanSuccessors(); List elseSC = elseBlock.getCleanSuccessors(); diff --git a/jadx-core/src/test/java/jadx/tests/internal/others/TestIfInTry.java b/jadx-core/src/test/java/jadx/tests/internal/others/TestIfInTry.java new file mode 100644 index 000000000..8d55e548c --- /dev/null +++ b/jadx-core/src/test/java/jadx/tests/internal/others/TestIfInTry.java @@ -0,0 +1,57 @@ +package jadx.tests.internal.others; + +import jadx.api.InternalJadxTest; +import jadx.core.dex.nodes.ClassNode; + +import java.io.File; +import java.io.IOException; + +import org.junit.Test; + +import static jadx.tests.utils.JadxMatchers.containsOne; +import static jadx.tests.utils.JadxMatchers.countString; +import static org.junit.Assert.assertThat; + +public class TestIfInTry extends InternalJadxTest { + + public static class TestCls { + private File dir; + + public int test() { + try { + int a = f(); + if (a != 0) { + return a; + } + } catch (Exception e) { + // skip + } + try { + f(); + return 1; + } catch (IOException e) { + return -1; + } + } + + private int f() throws IOException { + return 0; + } + } + + @Test + public void test() { + setOutputCFG(); + ClassNode cls = getClassNode(TestCls.class); + String code = cls.getCode().toString(); + System.out.println(code); + + assertThat(code, containsOne("if (a != 0) {")); + assertThat(code, containsOne("} catch (Exception e) {")); + assertThat(code, countString(2, "try {")); + assertThat(code, countString(3, "f()")); + assertThat(code, containsOne("return 1;")); + assertThat(code, containsOne("} catch (IOException e")); + assertThat(code, containsOne("return -1;")); + } +}