Skip to content

Commit

Permalink
fix: bitwise or/and with non-boolean (#628) (PR #629)
Browse files Browse the repository at this point in the history
  • Loading branch information
asashour authored and skylot committed Apr 24, 2019
1 parent 336d6ce commit 9645f33
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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;
Expand Down Expand Up @@ -221,21 +222,23 @@ private static IfCondition simplifyCmpOp(Compare c) {
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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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(" & "));
}
}

0 comments on commit 9645f33

Please sign in to comment.