From 9645f33c7bfb2f6a998ffda1129edca732165c1c Mon Sep 17 00:00:00 2001 From: Ahmed Ashour Date: Wed, 24 Apr 2019 18:31:49 +0200 Subject: [PATCH] fix: bitwise or/and with non-boolean (#628) (PR #629) --- .../dex/regions/conditions/IfCondition.java | 21 ++++++----- .../conditions/TestConditions17.java | 35 +++++++++++++++++++ 2 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 jadx-core/src/test/java/jadx/tests/integration/conditions/TestConditions17.java diff --git a/jadx-core/src/main/java/jadx/core/dex/regions/conditions/IfCondition.java b/jadx-core/src/main/java/jadx/core/dex/regions/conditions/IfCondition.java index faef0953f..8d58aad33 100644 --- a/jadx-core/src/main/java/jadx/core/dex/regions/conditions/IfCondition.java +++ b/jadx-core/src/main/java/jadx/core/dex/regions/conditions/IfCondition.java @@ -12,6 +12,7 @@ import jadx.core.dex.instructions.ArithNode; import jadx.core.dex.instructions.ArithOp; import jadx.core.dex.instructions.IfNode; import jadx.core.dex.instructions.IfOp; +import jadx.core.dex.instructions.args.ArgType; import jadx.core.dex.instructions.args.InsnWrapArg; import jadx.core.dex.instructions.args.LiteralArg; import jadx.core.dex.instructions.args.RegisterArg; @@ -221,21 +222,23 @@ public final class IfCondition { break; case ARITH: - ArithOp arithOp = ((ArithNode) wrapInsn).getOp(); - if (arithOp == ArithOp.OR || arithOp == ArithOp.AND) { - IfOp ifOp = c.getInsn().getOp(); - boolean isTrue = ifOp == IfOp.NE && lit == 0 + if (c.getB().getType() == ArgType.BOOLEAN) { + ArithOp arithOp = ((ArithNode) wrapInsn).getOp(); + if (arithOp == ArithOp.OR || arithOp == ArithOp.AND) { + IfOp ifOp = c.getInsn().getOp(); + boolean isTrue = ifOp == IfOp.NE && lit == 0 || ifOp == IfOp.EQ && lit == 1; - IfOp op = isTrue ? IfOp.NE : IfOp.EQ; - Mode mode = isTrue && arithOp == ArithOp.OR || + IfOp op = isTrue ? IfOp.NE : IfOp.EQ; + Mode mode = isTrue && arithOp == ArithOp.OR || !isTrue && arithOp == ArithOp.AND ? Mode.OR : Mode.AND; - IfNode if1 = new IfNode(op, -1, wrapInsn.getArg(0), LiteralArg.FALSE); - IfNode if2 = new IfNode(op, -1, wrapInsn.getArg(1), LiteralArg.FALSE); - return new IfCondition(mode, + IfNode if1 = new IfNode(op, -1, wrapInsn.getArg(0), LiteralArg.FALSE); + IfNode if2 = new IfNode(op, -1, wrapInsn.getArg(1), LiteralArg.FALSE); + return new IfCondition(mode, Arrays.asList(new IfCondition(new Compare(if1)), new IfCondition(new Compare(if2)))); + } } break; diff --git a/jadx-core/src/test/java/jadx/tests/integration/conditions/TestConditions17.java b/jadx-core/src/test/java/jadx/tests/integration/conditions/TestConditions17.java new file mode 100644 index 000000000..beb9e30b3 --- /dev/null +++ b/jadx-core/src/test/java/jadx/tests/integration/conditions/TestConditions17.java @@ -0,0 +1,35 @@ +package jadx.tests.integration.conditions; + +import static jadx.tests.api.utils.JadxMatchers.containsOne; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +import jadx.core.dex.nodes.ClassNode; +import jadx.tests.api.IntegrationTest; + +public class TestConditions17 extends IntegrationTest { + + public static class TestCls { + + public static final int SOMETHING = 2; + + public static void test(int a) { + if ((a & SOMETHING) != 0) { + print(1); + } + print(2); + } + + public static void print(Object o) { + } + } + + @Test + public void test() { + ClassNode cls = getClassNode(TestCls.class); + String code = cls.getCode().toString(); + + assertThat(code, containsOne(" & ")); + } +}