Skip to content

Commit

Permalink
fix: don't change AST before checks in ternary transform (#710)
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Jul 20, 2019
1 parent 7c53b98 commit b32dc17
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,6 @@ private static boolean processOneBranchTernary(MethodNode mth, IfRegion ifRegion
}

private static void replaceWithTernary(MethodNode mth, IfRegion ifRegion, BlockNode block, InsnNode insn) {
BlockNode header = ifRegion.getConditionBlocks().get(0);
if (!ifRegion.getParent().replaceSubBlock(ifRegion, header)) {
return;
}
RegisterArg resArg = insn.getResult();
if (resArg.getSVar().getUseList().size() != 1) {
return;
Expand All @@ -277,6 +273,10 @@ private static void replaceWithTernary(MethodNode mth, IfRegion ifRegion, BlockN
}

// all checks passed
BlockNode header = ifRegion.getConditionBlocks().get(0);
if (!ifRegion.getParent().replaceSubBlock(ifRegion, header)) {
return;
}
InsnList.remove(block, insn);
TernaryInsn ternInsn = new TernaryInsn(ifRegion.getCondition(),
phiInsn.getResult(), InsnArg.wrapInsnIntoArg(insn), otherArg);
Expand Down
3 changes: 2 additions & 1 deletion jadx-core/src/main/java/jadx/core/utils/DebugUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ private static void printInsns(MethodNode mth, String indent, IBlock block) {
CodeWriter code = new CodeWriter();
ig.makeInsn(insn, code);
String insnStr = code.toString().substring(CodeWriter.NL.length());
LOG.debug("{}|> {}\t{}", indent, insnStr, insn.getAttributesString());
String attrStr = insn.isAttrStorageEmpty() ? "" : '\t' + insn.getAttributesString();
LOG.debug("{}|> {}{}", indent, insnStr, attrStr);
} catch (CodegenException e) {
LOG.debug("{}|>!! {}", indent, insn);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package jadx.tests.integration.trycatch;

import java.util.Properties;

import org.junit.jupiter.api.Test;

import jadx.core.dex.nodes.ClassNode;
import jadx.tests.api.IntegrationTest;

import static jadx.tests.api.utils.JadxMatchers.containsOne;
import static org.hamcrest.MatcherAssert.assertThat;

public class TestTryWithEmptyCatch extends IntegrationTest {

public static class TestCls extends Exception {
private static final long serialVersionUID = -5723049816464070603L;
private Properties field;

public TestCls(String str) {
super(str);
Properties properties = null;
try {
if (str.contains("properties")) {
properties = new Properties();
}
} catch (Exception unused) {
// empty
}
this.field = properties;
}
}

@Test
public void test() {
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();

assertThat(code, containsOne("try {"));
assertThat(code, containsOne("if ("));
}
}

0 comments on commit b32dc17

Please sign in to comment.