-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b178f2e
fix: ArrayIndexOutOfBoundsException in string concatenation visitor
jpstotz d07cfa2
fix: typo in comment
jpstotz cb45896
chore: update dependencies and gradle
skylot 3097672
Merge branch 'master' of https://github.com/skylot/jadx into stringCo…
jpstotz d3beca3
fix: StringBuilder chain processing created wrong code
jpstotz 01971c2
Merge branch 'master' into stringConcatFix
skylot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
jadx-core/src/main/java/jadx/core/dex/instructions/CallMthInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 11 additions & 10 deletions
21
jadx-core/src/test/java/jadx/tests/api/utils/CountString.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
jadx-core/src/test/java/jadx/tests/integration/SimplifyVisitorStringBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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();")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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.