Better descriptor validation and trap unexpected desc errors, fixes #461

This commit is contained in:
Mumfrey committed 2021-04-21 13:32:05 +01:00
1 parent 5c9d20fe22
commit 09b23eae8c
2 files changed
+31 -7

No files matched your search

@@ -667,7 +667,11 @@ public final class MemberInfo implements ITargetSelectorRemappable, ITargetSelec
}
} else {
try {
Type.getArgumentTypes(this.desc);
for (Type argType : Type.getArgumentTypes(this.desc)) {
// getInternalName is a useful litmus test for improperly-formatted types which parse out of
// the descriptor correctly but are actually invalid, for example unterminated class names
argType.getInternalName();
}
} catch (Exception ex) {
throw new InvalidMemberDescriptorException(this.input, "Invalid descriptor: " + this.desc);
}
@@ -675,6 +679,10 @@ public final class MemberInfo implements ITargetSelectorRemappable, ITargetSelec
String retString = this.desc.substring(this.desc.indexOf(')') + 1);
try {
Type retType = Type.getType(retString);
int sort = retType.getSort();
if (sort >= Type.ARRAY) {
retType.getInternalName(); // sanity check
}
if (!retString.equals(retType.getDescriptor())) {
throw new InvalidMemberDescriptorException(this.input, "Invalid return type \"" + retString + "\" in descriptor: "
+ this.desc);
@@ -238,9 +238,9 @@ public class SignaturePrinter {
private StringBuilder appendType(StringBuilder sb, Type type, String name) {
switch (type.getSort()) {
case Type.ARRAY:
return SignaturePrinter.appendArraySuffix(this.appendType(sb, type.getElementType(), name), type);
return SignaturePrinter.appendArraySuffix(this.appendType(sb, SignaturePrinter.getElementType(type), name), type);
case Type.OBJECT:
return this.appendType(sb, type.getClassName(), name);
return this.appendType(sb, SignaturePrinter.getClassName(type), name);
default:
sb.append(SignaturePrinter.getTypeName(type, false, this.fullyQualified));
if (name != null) {
@@ -263,7 +263,7 @@ public class SignaturePrinter {
}
return sb;
}
/**
* Get the source code name for the specified type
*
@@ -310,9 +310,10 @@ public class SignaturePrinter {
case Type.FLOAT: return box ? "Float" : "float";
case Type.LONG: return box ? "Long" : "long";
case Type.DOUBLE: return box ? "Double" : "double";
case Type.ARRAY: return SignaturePrinter.getTypeName(type.getElementType(), box, fullyQualified) + SignaturePrinter.arraySuffix(type);
case Type.ARRAY:
return SignaturePrinter.getTypeName(SignaturePrinter.getElementType(type), box, fullyQualified) + SignaturePrinter.arraySuffix(type);
case Type.OBJECT:
String typeName = type.getClassName();
String typeName = SignaturePrinter.getClassName(type);
if (!fullyQualified) {
typeName = typeName.substring(typeName.lastIndexOf('.') + 1);
}
@@ -322,11 +323,26 @@ public class SignaturePrinter {
}
}
private static Type getElementType(Type type) {
try {
return type.getElementType();
} catch (Exception ex) {
return Type.getObjectType("InvalidType");
}
}
private static String getClassName(Type type) {
try {
return type.getClassName();
} catch (Exception ex) {
return "InvalidType";
}
}
private static String arraySuffix(Type type) {
return Strings.repeat("[]", type.getDimensions());
}
private static StringBuilder appendArraySuffix(StringBuilder sb, Type type) {
for (int i = 0; i < type.getDimensions(); i++) {
sb.append("[]");