From 711419a7970f1119cd4df53abb85e41be1fb476f Mon Sep 17 00:00:00 2001 From: Skylot Date: Sun, 2 Oct 2022 21:31:39 +0100 Subject: [PATCH] fix: correct fix for all use places of incompatible primitives (#1688) --- .../typeinference/TypeInferenceVisitor.java | 13 ++---- .../conditions/TestBooleanToInt.java | 1 + .../conditions/TestBooleanToInt2.java | 40 +++++++++++++++++++ .../smali/conditions/TestBooleanToInt2.smali | 29 ++++++++++++++ 4 files changed, 74 insertions(+), 9 deletions(-) create mode 100644 jadx-core/src/test/java/jadx/tests/integration/conditions/TestBooleanToInt2.java create mode 100644 jadx-core/src/test/smali/conditions/TestBooleanToInt2.smali diff --git a/jadx-core/src/main/java/jadx/core/dex/visitors/typeinference/TypeInferenceVisitor.java b/jadx-core/src/main/java/jadx/core/dex/visitors/typeinference/TypeInferenceVisitor.java index 89031707..37fc0560 100644 --- a/jadx-core/src/main/java/jadx/core/dex/visitors/typeinference/TypeInferenceVisitor.java +++ b/jadx-core/src/main/java/jadx/core/dex/visitors/typeinference/TypeInferenceVisitor.java @@ -913,25 +913,20 @@ public final class TypeInferenceVisitor extends AbstractVisitor { } boolean fixed = false; - for (ITypeBound bound : typeInfo.getBounds()) { - if (bound.getBound() == BoundEnum.USE - && fixBooleanUsage(mth, bound)) { + for (RegisterArg arg : new ArrayList<>(var.getUseList())) { + if (fixBooleanUsage(mth, arg)) { fixed = true; } } return fixed; } - private boolean fixBooleanUsage(MethodNode mth, ITypeBound bound) { - ArgType boundType = bound.getType(); + private boolean fixBooleanUsage(MethodNode mth, RegisterArg boundArg) { + ArgType boundType = boundArg.getInitType(); if (boundType == ArgType.BOOLEAN || (boundType.isTypeKnown() && !boundType.isPrimitive())) { return false; } - RegisterArg boundArg = bound.getArg(); - if (boundArg == null) { - return false; - } InsnNode insn = boundArg.getParentInsn(); if (insn == null || insn.getType() == InsnType.IF) { return false; diff --git a/jadx-core/src/test/java/jadx/tests/integration/conditions/TestBooleanToInt.java b/jadx-core/src/test/java/jadx/tests/integration/conditions/TestBooleanToInt.java index 440f6e24..ae0b6326 100644 --- a/jadx-core/src/test/java/jadx/tests/integration/conditions/TestBooleanToInt.java +++ b/jadx-core/src/test/java/jadx/tests/integration/conditions/TestBooleanToInt.java @@ -6,6 +6,7 @@ import jadx.tests.api.SmaliTest; import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat; +@SuppressWarnings("CommentedOutCode") public class TestBooleanToInt extends SmaliTest { // @formatter:off diff --git a/jadx-core/src/test/java/jadx/tests/integration/conditions/TestBooleanToInt2.java b/jadx-core/src/test/java/jadx/tests/integration/conditions/TestBooleanToInt2.java new file mode 100644 index 00000000..96d6c94f --- /dev/null +++ b/jadx-core/src/test/java/jadx/tests/integration/conditions/TestBooleanToInt2.java @@ -0,0 +1,40 @@ +package jadx.tests.integration.conditions; + +import org.junit.jupiter.api.Test; + +import jadx.tests.api.SmaliTest; + +import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat; + +@SuppressWarnings("CommentedOutCode") +public class TestBooleanToInt2 extends SmaliTest { + + // @formatter:off + /* + public static class TestCls { + public void test() { + boolean v = getValue(); + use1(Integer.valueOf(v)); + use2(v); + } + + private boolean getValue() { + return false; + } + + private void use1(Integer v) { + } + + private void use2(int v) { + } + } + */ + // @formatter:on + @Test + public void test() { + assertThat(getClassNodeFromSmali()) + .code() + .containsOne("use1(Integer.valueOf(value ? 1 : 0));") + .containsOne("use2(value ? 1 : 0);"); + } +} diff --git a/jadx-core/src/test/smali/conditions/TestBooleanToInt2.smali b/jadx-core/src/test/smali/conditions/TestBooleanToInt2.smali new file mode 100644 index 00000000..88db44dc --- /dev/null +++ b/jadx-core/src/test/smali/conditions/TestBooleanToInt2.smali @@ -0,0 +1,29 @@ +.class public Lconditions/TestBooleanToInt2; +.super Ljava/lang/Object; + +.method public test()V + .registers 3 + invoke-direct {p0}, Lconditions/TestBooleanToInt2;->getValue()Z + move-result v0 + invoke-static {v0}, Ljava/lang/Integer;->valueOf(I)Ljava/lang/Integer; + move-result-object v1 + invoke-direct {p0, v1}, Lconditions/TestBooleanToInt2;->use1(Ljava/lang/Integer;)V + invoke-direct {p0, v0}, Lconditions/TestBooleanToInt2;->use2(I)V + return-void +.end method + +.method private getValue()Z + .registers 2 + const/4 v0, 0x0 + return v0 +.end method + +.method private use1(Ljava/lang/Integer;)V + .registers 2 + return-void +.end method + +.method private use2(I)V + .registers 2 + return-void +.end method -- GitLab