fix: additional checks to forbid inline of null consts (#1828)

This commit is contained in:
Skylot
2023-04-19 15:49:06 +01:00
parent 3fa3e5acec
commit 50283ab543
2 changed files with 61 additions and 5 deletions
@@ -125,17 +125,30 @@ public class ConstInlineVisitor extends AbstractVisitor {
int k = 0;
for (RegisterArg useArg : useList) {
InsnNode insn = useArg.getParentInsn();
if (insn == null) {
continue;
}
if (!canUseNull(insn, useArg)) {
useArg.add(AFlag.DONT_INLINE_CONST);
if (insn != null && forbidNullArgInline(insn, useArg)) {
k++;
}
}
return k == useList.size();
}
private static boolean forbidNullArgInline(InsnNode insn, RegisterArg useArg) {
switch (insn.getType()) {
case MOVE:
case CAST:
case CHECK_CAST:
// result is null, chain checks
return forbidNullInlines(insn.getResult().getSVar());
default:
if (!canUseNull(insn, useArg)) {
useArg.add(AFlag.DONT_INLINE_CONST);
return true;
}
return false;
}
}
private static boolean canUseNull(InsnNode insn, RegisterArg useArg) {
switch (insn.getType()) {
case INVOKE:
@@ -0,0 +1,43 @@
package jadx.tests.integration.others;
import org.junit.jupiter.api.Test;
import jadx.tests.api.IntegrationTest;
import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;
public class TestNullInline extends IntegrationTest {
@SuppressWarnings({ "RedundantCast", "DataFlowIssue", "unused" })
public static class TestCls {
public static Long test(Double d1) {
T1<T2, Byte> t1 = (T1<T2, Byte>) null;
return t1.t2.l;
}
static class T2 {
public long l;
}
static class T1<H, P extends Byte> {
public T2 t2;
public T1(T2 t2) {
this.t2 = t2;
}
}
}
@Test
public void test() {
assertThat(getClassNode(TestCls.class))
.code()
.containsOne("Long.valueOf(t1.t2.l);");
}
@Test
public void testNoDebug() {
noDebugInfo();
getClassNode(TestCls.class);
}
}