fix: don't generate string concatenation without assign to variable

This commit is contained in:
Skylot
2020-05-12 22:51:38 +01:00
parent f482b8b95c
commit 58722d372e
3 changed files with 45 additions and 1 deletions
@@ -52,7 +52,7 @@ public class InitCodeVariables extends AbstractVisitor {
}
}
private static void initCodeVar(SSAVar ssaVar) {
public static void initCodeVar(SSAVar ssaVar) {
if (ssaVar.isCodeVarSet()) {
return;
}
@@ -401,6 +401,7 @@ public class SimplifyVisitor extends AbstractVisitor {
concatInsn.copyAttributesFrom(toStrInsn);
concatInsn.remove(AFlag.DONT_GENERATE);
concatInsn.remove(AFlag.REMOVE);
checkResult(mth, concatInsn);
return concatInsn;
} catch (Exception e) {
LOG.warn("Can't convert string concatenation: {} insn: {}", mth, toStrInsn, e);
@@ -408,6 +409,17 @@ public class SimplifyVisitor extends AbstractVisitor {
return null;
}
/* String concat without assign to variable will cause compilation error */
private static void checkResult(MethodNode mth, InsnNode concatInsn) {
if (concatInsn.getResult() == null) {
RegisterArg resArg = InsnArg.reg(0, ArgType.STRING);
SSAVar ssaVar = mth.makeNewSVar(resArg);
InitCodeVariables.initCodeVar(ssaVar);
ssaVar.setType(ArgType.STRING);
concatInsn.setResult(resArg);
}
}
/**
* Remove and unbind all instructions with StringBuilder
*/
@@ -0,0 +1,32 @@
package jadx.tests.integration.others;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jadx.tests.api.IntegrationTest;
import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;
public class TestStringConcatWithoutResult extends IntegrationTest {
private static final Logger LOG = LoggerFactory.getLogger(TestStringConcatWithoutResult.class);
public static class TestCls {
public static final boolean LOG_DEBUG = false;
public void test(int i) {
String msg = "Input arg value: " + i;
if (LOG_DEBUG) {
LOG.debug(msg);
}
}
}
@Test
public void test() {
noDebugInfo();
assertThat(getClassNode(TestCls.class))
.code()
.containsOne(" = \"Input arg value: \" + i;");
}
}