Skip to content

Commit

Permalink
Fail gracefully when decompiling mangled string concat, fixes IOOBE
Browse files Browse the repository at this point in the history
  • Loading branch information
jaskarth committed Aug 17, 2024
1 parent 934ec9a commit 0625b22
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.jetbrains.java.decompiler.code.CodeConstants;
import org.jetbrains.java.decompiler.modules.decompiler.exps.*;
import org.jetbrains.java.decompiler.modules.decompiler.exps.FunctionExprent.FunctionType;
import org.jetbrains.java.decompiler.modules.decompiler.stats.RootStatement;
import org.jetbrains.java.decompiler.modules.decompiler.stats.Statement;
import org.jetbrains.java.decompiler.struct.consts.PooledConstant;
import org.jetbrains.java.decompiler.struct.consts.PrimitiveConstant;
Expand Down Expand Up @@ -32,25 +33,25 @@ public static void simplifyStringConcat(Statement stat) {

if (stat.getExprents() != null) {
for (int i = 0; i < stat.getExprents().size(); ++i) {
Exprent ret = simplifyStringConcat(stat.getExprents().get(i));
Exprent ret = simplifyStringConcat(stat.getTopParent(), stat.getExprents().get(i));
if (ret != null) {
stat.getExprents().set(i, ret);
}
}
}
}

private static Exprent simplifyStringConcat(Exprent exprent) {
private static Exprent simplifyStringConcat(RootStatement root, Exprent exprent) {
for (Exprent cexp : exprent.getAllExprents()) {
Exprent ret = simplifyStringConcat(cexp);
Exprent ret = simplifyStringConcat(root, cexp);
if (ret != null) {
exprent.replaceExprent(cexp, ret);
ret.addBytecodeOffsets(cexp.bytecode);
}
}

if (exprent instanceof InvocationExprent) {
Exprent ret = ConcatenationHelper.contractStringConcat(exprent);
Exprent ret = ConcatenationHelper.contractStringConcat(root, exprent);
if (!exprent.equals(ret)) {
return ret;
}
Expand All @@ -59,7 +60,7 @@ private static Exprent simplifyStringConcat(Exprent exprent) {
return null;
}

public static Exprent contractStringConcat(Exprent expr) {
public static Exprent contractStringConcat(RootStatement root, Exprent expr) {

Exprent exprTmp = null;
VarType cltype = null;
Expand All @@ -78,7 +79,7 @@ else if (bufferClass.equals(iex.getClassname())) {
exprTmp = iex.getInstance();
}
} else if ("makeConcatWithConstants".equals(iex.getName()) || "makeConcat".equals(iex.getName())) { // java 9 style
List<Exprent> parameters = extractParameters(iex.getBootstrapArguments(), iex);
List<Exprent> parameters = extractParameters(root, iex.getBootstrapArguments(), iex);

// Check if we need to add an empty string to the param list to convert from objects or primitives to strings.
boolean addEmptyString = true;
Expand Down Expand Up @@ -198,7 +199,7 @@ private static Exprent createConcatExprent(List<Exprent> lstOperands, BitSet byt
private static final String TAG_ARG_S = "\u0001";
private static final char TAG_CONST = '\u0002';

private static List<Exprent> extractParameters(List<PooledConstant> bootstrapArguments, InvocationExprent expr) {
private static List<Exprent> extractParameters(RootStatement root, List<PooledConstant> bootstrapArguments, InvocationExprent expr) {
List<Exprent> parameters = expr.getLstParameters();

// Remove unnecessary String.valueOf() calls to resolve Vineflower#151
Expand Down Expand Up @@ -235,6 +236,10 @@ private static List<Exprent> extractParameters(List<PooledConstant> bootstrapArg
// skip for now
}
if (c == TAG_ARG) {
if (parameterId >= parameters.size()) {
root.addComment("$VF: Could not fully resugar string concatentation!");
continue;
}
res.add(parameters.get(parameterId++));
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public static boolean identifySecondaryFunctions(Statement stat, VarProcessor va
break;
}
} else if (obj instanceof Exprent) {
Exprent retexpr = identifySecondaryFunctions((Exprent) obj, true, varProc, options);
Exprent retexpr = identifySecondaryFunctions(stat, (Exprent) obj, true, varProc, options);
if (retexpr != null) {
if (stat.getExprents() == null) {
// only head expressions can be replaced!
Expand All @@ -163,7 +163,7 @@ public static boolean identifySecondaryFunctions(Statement stat, VarProcessor va
return ret;
}

private static Exprent identifySecondaryFunctions(Exprent exprent, boolean statement_level, VarProcessor varProc, IdentifySecondaryOptions options) {
private static Exprent identifySecondaryFunctions(Statement stat, Exprent exprent, boolean statement_level, VarProcessor varProc, IdentifySecondaryOptions options) {
if (exprent instanceof FunctionExprent) {
FunctionExprent fexpr = (FunctionExprent) exprent;

Expand Down Expand Up @@ -254,7 +254,7 @@ private static Exprent identifySecondaryFunctions(Exprent exprent, boolean state
replaced = false;

for (Exprent expr : exprent.getAllExprents()) {
Exprent retexpr = identifySecondaryFunctions(expr, false, varProc, options);
Exprent retexpr = identifySecondaryFunctions(stat, expr, false, varProc, options);
if (retexpr != null) {
exprent.replaceExprent(expr, retexpr);
retexpr.addBytecodeOffsets(expr.bytecode);
Expand Down Expand Up @@ -529,7 +529,7 @@ private static Exprent identifySecondaryFunctions(Exprent exprent, boolean state
}

if (!statement_level) { // simplify if exprent is a real expression. The opposite case is pretty absurd, can still happen however (and happened at least once).
Exprent retexpr = ConcatenationHelper.contractStringConcat(exprent);
Exprent retexpr = ConcatenationHelper.contractStringConcat(stat.getTopParent(), exprent);
if (!exprent.equals(retexpr)) {
return retexpr;
}
Expand Down

0 comments on commit 0625b22

Please sign in to comment.