Skip to content

Commit

Permalink
fix: always cast null objects in overloaded method (#707)
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Jul 10, 2019
1 parent e4fc677 commit 12bb632
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
22 changes: 15 additions & 7 deletions jadx-core/src/main/java/jadx/core/codegen/InsnGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -807,11 +807,22 @@ private boolean processOverloadedArg(CodeWriter code, MethodNode callMth, InsnAr
return false;
}
}
if (isCastNeeded(arg, origType)) {
code.add('(');
useType(code, origType);
code.add(") ");
return true;
}
return false;
}

private boolean isCastNeeded(InsnArg arg, ArgType origType) {
ArgType argType = arg.getType();
if (argType.equals(origType)
// null cast to object
&& (!arg.isLiteral() || ((LiteralArg) arg).getLiteral() != 0
|| (!argType.isArray() && !argType.isObject()))) {
if (arg.isLiteral() && ((LiteralArg) arg).getLiteral() == 0
&& (argType.isObject() || argType.isArray())) {
return true;
}
if (argType.equals(origType)) {
return false;
}
if (origType.isGeneric()) {
Expand All @@ -828,9 +839,6 @@ private boolean processOverloadedArg(CodeWriter code, MethodNode callMth, InsnAr
((InsnWrapArg) arg).getWrapInsn().add(AFlag.EXPLICIT_GENERICS);
}
}
code.add('(');
useType(code, origType);
code.add(") ");
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package jadx.tests.integration.others;

import java.util.List;

import org.junit.jupiter.api.Test;

import jadx.core.dex.nodes.ClassNode;
Expand All @@ -15,13 +17,17 @@ public static class TestCls {
public void test() {
m((long[]) null);
m((String) null);
m((List<String>) null);
}

public void m(long[] a) {
}

public void m(String s) {
}

public void m(List<String> list) {
}
}

@Test
Expand All @@ -31,5 +37,6 @@ public void test() {

assertThat(code, containsOne("m((long[]) null);"));
assertThat(code, containsOne("m((String) null);"));
assertThat(code, containsOne("m((List<String>) null);"));
}
}

0 comments on commit 12bb632

Please sign in to comment.