Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String concatenation visitor: fix for ArrayIndexOutOfBoundsException #427

Merged
merged 6 commits into from
Jan 12, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id 'com.github.ksoichiro.console.reporter' version '0.5.0'
id 'org.sonarqube' version '2.6.2'
id 'org.sonarqube' version '2.7'
id 'com.github.ben-manes.versions' version '0.20.0'
}

Expand Down Expand Up @@ -41,10 +41,9 @@ allprojects {

testCompile 'ch.qos.logback:logback-classic:1.2.3'
testCompile 'junit:junit:4.12'
testCompile 'org.hamcrest:hamcrest-library:1.3'
testCompile 'org.mockito:mockito-core:2.20.1'
testCompile 'org.hamcrest:hamcrest-library:2.1'
testCompile 'org.mockito:mockito-core:2.23.4'
testCompile 'org.spockframework:spock-core:1.1-groovy-2.4'
testCompile 'cglib:cglib-nodep:3.2.7'
}

repositories {
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
12 changes: 6 additions & 6 deletions jadx-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ dependencies {

compile files('lib/dx-1.16.jar')
compile 'commons-io:commons-io:2.6'
compile 'org.ow2.asm:asm:6.2'
compile 'org.jetbrains:annotations:16.0.2'
compile 'uk.com.robust-it:cloning:1.9.10'
compile 'org.ow2.asm:asm:7.0'
compile 'org.jetbrains:annotations:16.0.3'
compile 'uk.com.robust-it:cloning:1.9.11'

testCompile 'org.smali:smali:2.2.4'
testCompile 'org.smali:baksmali:2.2.4'
testCompile 'org.smali:smali:2.2.5'
testCompile 'org.smali:baksmali:2.2.5'

testCompile 'org.apache.commons:commons-lang3:3.7'
testCompile 'org.apache.commons:commons-lang3:3.8.1'
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package jadx.core.dex.instructions;

import jadx.core.dex.info.MethodInfo;

public interface CallMthInterface {

public MethodInfo getCallMth();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import jadx.core.utils.InsnUtils;
import jadx.core.utils.Utils;

public class InvokeNode extends InsnNode {
public class InvokeNode extends InsnNode implements CallMthInterface {

private final InvokeType type;
private final MethodInfo mth;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import jadx.core.dex.info.ClassInfo;
import jadx.core.dex.info.MethodInfo;
import jadx.core.dex.instructions.CallMthInterface;
import jadx.core.dex.instructions.InsnType;
import jadx.core.dex.instructions.InvokeNode;
import jadx.core.dex.instructions.args.RegisterArg;
import jadx.core.dex.nodes.InsnNode;
import jadx.core.dex.nodes.MethodNode;

public class ConstructorInsn extends InsnNode {
public class ConstructorInsn extends InsnNode implements CallMthInterface {

private final MethodInfo callMth;
private final CallType callType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,13 @@
import java.util.Collections;
import java.util.List;

import jadx.core.dex.instructions.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jadx.core.Consts;
import jadx.core.dex.info.FieldInfo;
import jadx.core.dex.info.MethodInfo;
import jadx.core.dex.instructions.ArithNode;
import jadx.core.dex.instructions.ArithOp;
import jadx.core.dex.instructions.ConstStringNode;
import jadx.core.dex.instructions.IfNode;
import jadx.core.dex.instructions.IndexInsnNode;
import jadx.core.dex.instructions.InsnType;
import jadx.core.dex.instructions.InvokeNode;
import jadx.core.dex.instructions.args.ArgType;
import jadx.core.dex.instructions.args.FieldArg;
import jadx.core.dex.instructions.args.InsnArg;
Expand Down Expand Up @@ -148,6 +142,15 @@ private static void simplifyTernary(TernaryInsn insn) {
}
}

/**
* Simplify chains of calls to StringBuilder#append() plus constructor of StringBuilder.
* Those chains are usually automatically generated by the Java compiler when you create String
* concatenations like <code>"text " + 1 + " text"</code>.
*
* @param mth
* @param insn
* @return
*/
private static InsnNode convertInvoke(MethodNode mth, InsnNode insn) {
MethodInfo callMth = ((InvokeNode) insn).getCallMth();

Expand Down Expand Up @@ -201,7 +204,15 @@ private static InsnNode convertInvoke(MethodNode mth, InsnNode insn) {
}

for (; argInd < len; argInd++) { // Add the .append(xxx) arg string to concat
concatInsn.addArg(chain.get(argInd).getArg(1));
InsnNode node = chain.get(argInd);
MethodInfo method = ((CallMthInterface) node).getCallMth();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add instanceof check before the cast, it will make code much safer.
And next time instead of merging just rebase your commits on latest master, also fix for the previous commit is better to squash together, so you will get 2 nice commits on top of master :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The missing instanceof check is on purpose. If we encounter such a case most likely can't simplify the chain. It will then raise an Exception so that anybody may notice it and we can take a closer look onto it. Therefore the effect is the same the only difference is that without the instanceof check nothing is logged.

Rebase: According what I have read you should not do this once you have pushed your commits to a public repo. And as the branch this PR bases on is public...

Therefore I will keep on using git as I do - at least most of the times it does what I want.
If they develop such a complex version control system they should make it more usable and intuitive to use.

if (!(node.getArgsCount() < 2 && method.isConstructor() || method.getName().equals("append"))) {
// The chain contains other calls to StringBuilder methods than the constructor or append.
// We can't simplify such chains, therefore we leave them as they are.
return null;
}
// process only constructor and append() calls
concatInsn.addArg(node.getArg(1));
}
concatInsn.setResult(insn.getResult());
return concatInsn;
Expand Down
21 changes: 11 additions & 10 deletions jadx-core/src/test/java/jadx/tests/api/utils/CountString.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
package jadx.tests.api.utils;

import org.hamcrest.Description;
import org.hamcrest.core.SubstringMatcher;
import org.hamcrest.TypeSafeMatcher;

public class CountString extends SubstringMatcher {
public class CountString extends TypeSafeMatcher<String> {

private final int count;
private final String substring;

public CountString(int count, String substring) {
super(substring);
this.count = count;
this.substring = substring;
}

@Override
protected boolean evalSubstringOf(String string) {
return this.count == count(string);
protected boolean matchesSafely(String item) {
return this.count == count(item);
}

@Override
protected String relationship() {
return "containing <" + count + "> occurrence of";
public void describeMismatchSafely(String item, Description mismatchDescription) {
mismatchDescription.appendText("found ").appendValue(count(item));
}

@Override
public void describeMismatchSafely(String item, Description mismatchDescription) {
mismatchDescription.appendText("found ").appendValue(count(item));
public void describeTo(Description description) {
description.appendText("containing <" + count + "> occurrence of ").appendValue(this.substring);
}

private int count(String string) {
return TestUtils.count(string, substring);
return TestUtils.count(string, this.substring);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package jadx.tests.integration;

import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.visitors.SimplifyVisitor;
import jadx.core.utils.exceptions.JadxException;
import jadx.tests.api.IntegrationTest;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;

/**
* Test the StringBuilder simplification part of {@link SimplifyVisitor}
*
* @author Jan Peter Stotz
*/
public class SimplifyVisitorStringBuilderTest extends IntegrationTest {

public static class TestCls1 {
public String test() {
return new StringBuilder("[init]").append("a1").append('c').append(2).append(0l).append(1.0f).
append(2.0d).append(true).toString();
}
}

@Test
public void test1() throws JadxException {
ClassNode cls = getClassNode(SimplifyVisitorStringBuilderTest.TestCls1.class);
SimplifyVisitor visitor = new SimplifyVisitor();
visitor.visit(cls);
String code = cls.getCode().toString();
assertThat(code, containsString("return \"[init]\" + \"a1\" + 'c' + 2 + 0 + 1.0f + 2.0d + true;"));
}

public static class TestCls2 {
public String test() {
// A chain with non-final variables
String sInit = "[init]";
String s = "a1";
char c = 'c';
int i = 1;
long l = 2;
float f = 1.0f;
double d = 2.0d;
boolean b = true;
return new StringBuilder(sInit).append(s).append(c).append(i).append(l).append(f).
append(d).append(b).toString();
}
}

@Test
public void test2() throws JadxException {
ClassNode cls = getClassNode(SimplifyVisitorStringBuilderTest.TestCls2.class);
SimplifyVisitor visitor = new SimplifyVisitor();
visitor.visit(cls);
String code = cls.getCode().toString();
assertThat(code, containsString("return \"[init]\" + \"a1\" + 'c' + 1 + 2 + 1.0f + 2.0d + true;"));
}

public static class TestClsStringUtilsReverse {

/**
* Simplified version of org.apache.commons.lang3.StringUtils.reverse()
*/
public static String reverse(final String str) {
return new StringBuilder(str).reverse().toString();
}
}

@Test
public void test3() throws JadxException {
ClassNode cls = getClassNode(SimplifyVisitorStringBuilderTest.TestClsStringUtilsReverse.class);
SimplifyVisitor visitor = new SimplifyVisitor();
visitor.visit(cls);
String code = cls.getCode().toString();
assertThat(code, containsString("return new StringBuilder(str).reverse().toString();"));
}

public static class TestClsChainWithDelete {
public String test() {
// a chain we can't simplify
return new StringBuilder("[init]").append("a1").delete(1, 2).toString();
}
}

@Test
public void testChainWithDelete() throws JadxException {
ClassNode cls = getClassNode(TestClsChainWithDelete.class);
SimplifyVisitor visitor = new SimplifyVisitor();
visitor.visit(cls);
String code = cls.getCode().toString();
assertThat(code, containsString("return new StringBuilder(\"[init]\").append(\"a1\").delete(1, 2).toString();"));
}
}
8 changes: 4 additions & 4 deletions jadx-gui/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ mainClassName = 'jadx.gui.JadxGUI'
dependencies {
compile(project(":jadx-core"))
compile(project(":jadx-cli"))
compile 'com.fifesoft:rsyntaxtextarea:2.6.1'
compile 'com.fifesoft:rsyntaxtextarea:3.0.0'
compile 'com.google.code.gson:gson:2.8.5'
compile files('libs/jfontchooser-1.0.5.jar')
compile 'hu.kazocsaba:image-viewer:1.2.3'

compile 'org.apache.commons:commons-lang3:3.7'
compile 'org.apache.commons:commons-lang3:3.8.1'

compile 'io.reactivex.rxjava2:rxjava:2.1.17'
compile "com.github.akarnokd:rxjava2-swing:0.2.16"
compile 'io.reactivex.rxjava2:rxjava:2.2.5'
compile "com.github.akarnokd:rxjava2-swing:0.3.3"
}

applicationDistribution.with {
Expand Down