From 1891f6fd7eb8c7d45f858460e13053c41ebfcb17 Mon Sep 17 00:00:00 2001 From: Skylot Date: Mon, 13 Mar 2023 21:25:41 +0000 Subject: [PATCH] fix: ignore source name if current alias is better (#1795) --- .../dex/visitors/rename/SourceFileRename.java | 10 +++++ .../main/java/jadx/core/utils/BetterName.java | 4 +- .../integration/deobf/TestBadSourceFile.java | 40 +++++++++++++++++++ .../smali/deobf/TestBadSourceFile/b.smali | 9 +++++ 4 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 jadx-core/src/test/java/jadx/tests/integration/deobf/TestBadSourceFile.java create mode 100644 jadx-core/src/test/smali/deobf/TestBadSourceFile/b.smali diff --git a/jadx-core/src/main/java/jadx/core/dex/visitors/rename/SourceFileRename.java b/jadx-core/src/main/java/jadx/core/dex/visitors/rename/SourceFileRename.java index 39d795f0..f2c64b3d 100644 --- a/jadx-core/src/main/java/jadx/core/dex/visitors/rename/SourceFileRename.java +++ b/jadx-core/src/main/java/jadx/core/dex/visitors/rename/SourceFileRename.java @@ -8,6 +8,7 @@ import jadx.core.deobf.NameMapper; import jadx.core.dex.attributes.AFlag; import jadx.core.dex.nodes.ClassNode; import jadx.core.dex.nodes.RootNode; +import jadx.core.utils.BetterName; import jadx.core.utils.StringUtils; public class SourceFileRename { @@ -45,6 +46,15 @@ public class SourceFileRename { if (otherCls != null) { return null; } + + if (cls.getClassInfo().hasAlias()) { + // ignore source name if current alias is "better" + String currentAlias = cls.getAlias(); + String betterName = BetterName.compareAndGet(name, currentAlias); + if (betterName.equals(currentAlias)) { + return null; + } + } cls.remove(JadxAttrType.SOURCE_FILE); return name; } diff --git a/jadx-core/src/main/java/jadx/core/utils/BetterName.java b/jadx-core/src/main/java/jadx/core/utils/BetterName.java index 48033a30..e5a08150 100644 --- a/jadx-core/src/main/java/jadx/core/utils/BetterName.java +++ b/jadx-core/src/main/java/jadx/core/utils/BetterName.java @@ -25,9 +25,9 @@ public class BetterName { boolean firstBetter = firstRating >= secondRating; if (DEBUG) { if (firstBetter) { - LOG.info("Better name: '{}' > '{}' ({} > {})", first, second, firstRating, secondRating); + LOG.debug("Better name: '{}' > '{}' ({} > {})", first, second, firstRating, secondRating); } else { - LOG.info("Better name: '{}' > '{}' ({} > {})", second, first, secondRating, firstRating); + LOG.debug("Better name: '{}' > '{}' ({} > {})", second, first, secondRating, firstRating); } } return firstBetter ? first : second; diff --git a/jadx-core/src/test/java/jadx/tests/integration/deobf/TestBadSourceFile.java b/jadx-core/src/test/java/jadx/tests/integration/deobf/TestBadSourceFile.java new file mode 100644 index 00000000..063e2626 --- /dev/null +++ b/jadx-core/src/test/java/jadx/tests/integration/deobf/TestBadSourceFile.java @@ -0,0 +1,40 @@ +package jadx.tests.integration.deobf; + +import org.junit.jupiter.api.Test; + +import jadx.tests.api.SmaliTest; + +import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat; + +public class TestBadSourceFile extends SmaliTest { + + @Test + public void test() { + // use source name disabled by default + enableDeobfuscation(); + args.setDeobfuscationMinLength(100); // rename everything + assertThat(searchCls(loadFromSmaliFiles(), "b")) + .code() + .containsOne("class C0000b {"); + } + + @Test + public void testWithUseSourceName() { + args.setUseSourceNameAsClassAlias(true); + // deobfuscation disabled + assertThat(searchCls(loadFromSmaliFiles(), "b")) + .code() + .containsOne("class a {"); + } + + @Test + public void testWithUseSourceNameAndDeobf() { + args.setUseSourceNameAsClassAlias(true); + enableDeobfuscation(); + args.setDeobfuscationMinLength(100); // rename everything + assertThat(searchCls(loadFromSmaliFiles(), "b")) + .code() + .containsOne("class C0000b {") + .containsOne("/* compiled from: a.java */"); + } +} diff --git a/jadx-core/src/test/smali/deobf/TestBadSourceFile/b.smali b/jadx-core/src/test/smali/deobf/TestBadSourceFile/b.smali new file mode 100644 index 00000000..fbad5385 --- /dev/null +++ b/jadx-core/src/test/smali/deobf/TestBadSourceFile/b.smali @@ -0,0 +1,9 @@ +.class Lb; +.super Ljava/lang/Object; +.source "a.java" + +.method constructor ()V + .registers 1 + invoke-direct {p0}, Ljava/lang/Object;->()V + return-void +.end method