Add some simple validation (idiot checking) to MemberInfo

This commit is contained in:
Mumfrey committed 2015-11-23 22:57:43 +00:00
1 parent c804069f5c
commit 1d3a5345e9
6 files changed
+128 -4

No files matched your search

@@ -34,7 +34,7 @@ import net.minecraft.launchwrapper.LaunchClassLoader;
/**
* A collection of {@link IMixinLaunchAgent launch agents)
* A collection of {@link IMixinLaunchAgent} launch agents)
*/
public class MixinTweakContainer {
@@ -139,7 +139,7 @@ public abstract class InjectionInfo {
throw new InvalidInjectionException(this, type + " annotation on " + this.method.name + " is missing 'at' value(s)");
}
MemberInfo targetMember = MemberInfo.parse(method, this.mixin);
MemberInfo targetMember = MemberInfo.parseAndValidate(method, this.mixin);
if (targetMember.owner != null && !targetMember.owner.equals(this.mixin.getTargetClassRef())) {
throw new InvalidInjectionException(this, type + " annotation on " + this.method.name + " specifies a target class '"
@@ -102,7 +102,7 @@ public class InjectionPointData {
}
public MemberInfo get(String key) {
return MemberInfo.parse(this.get(key, ""), this.mixin);
return MemberInfo.parseAndValidate(this.get(key, ""), this.mixin);
}
private int parseInt(String string, int defaultValue) {
@@ -122,7 +122,7 @@ public class InjectionPointData {
}
public MemberInfo getTarget() {
return MemberInfo.parse(this.target, this.mixin);
return MemberInfo.parseAndValidate(this.target, this.mixin);
}
public int getOrdinal() {
@@ -0,0 +1,43 @@
/*
* This file is part of Mixin, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.asm.mixin.injection.struct;
public class InvalidMemberDescriptorException extends RuntimeException {
private static final long serialVersionUID = 1L;
public InvalidMemberDescriptorException(String message) {
super(message);
}
public InvalidMemberDescriptorException(Throwable cause) {
super(cause);
}
public InvalidMemberDescriptorException(String message, Throwable cause) {
super(message, cause);
}
}
@@ -26,6 +26,7 @@ package org.spongepowered.asm.mixin.injection.struct;
import net.minecraftforge.srg2source.rangeapplier.MethodData;
import org.spongepowered.asm.lib.Type;
import org.spongepowered.asm.lib.tree.AbstractInsnNode;
import org.spongepowered.asm.lib.tree.FieldInsnNode;
import org.spongepowered.asm.lib.tree.MethodInsnNode;
@@ -257,6 +258,50 @@ public class MemberInfo {
public boolean isField() {
return this.desc != null && !this.desc.startsWith("(");
}
/**
* Perform ultra-simple validation of the descriptor, checks that the parts
* of the descriptor are basically sane.
*
* @return fluent
*
* @throws InvalidMemberDescriptorException if any validation check fails
*/
public MemberInfo validate() throws InvalidMemberDescriptorException {
// Extremely naive class name validation, just to spot really egregious errors
if (this.owner != null && !this.owner.matches("(?i)^[\\w\\p{Sc}/]+$")) {
throw new InvalidMemberDescriptorException("Invalid owner: " + this.owner);
}
// Also naive validation, we're looking for stupid errors here
if (this.name != null && !this.name.matches("(?i)^<?[\\w\\p{Sc}]+>?$")) {
throw new InvalidMemberDescriptorException("Invalid name: " + this.name);
}
if (this.desc != null) {
if (!this.desc.matches("^(\\([\\w\\[/;]*\\))?\\[?[\\w/;]+$")) {
throw new InvalidMemberDescriptorException("Invalid descriptor: " + this.desc);
}
if (this.isField()) {
if (!this.desc.equals(Type.getType(this.desc).getDescriptor())) {
throw new InvalidMemberDescriptorException("Invalid field type in descriptor: " + this.desc);
}
} else {
try {
Type.getArgumentTypes(this.desc);
} catch (Exception ex) {
throw new InvalidMemberDescriptorException("Invalid descriptor: " + this.desc);
}
String retString = this.desc.substring(this.desc.indexOf(')') + 1);
if (!retString.equals(Type.getType(retString).getDescriptor())) {
throw new InvalidMemberDescriptorException("Invalid return type \"" + retString + "\" in descriptor: " + this.desc);
}
}
}
return this;
}
/**
* Test whether this MemberInfo matches the supplied values. Null values are
@@ -338,6 +383,27 @@ public class MemberInfo {
return new MemberInfo(this, methodData, setOwner);
}
/**
* Parse a MemberInfo from a string and perform validation
*
* @param string String to parse MemberInfo from
* @return parsed MemberInfo
*/
public static MemberInfo parseAndValidate(String string) throws InvalidMemberDescriptorException {
return MemberInfo.parse(string, null, null).validate();
}
/**
* Parse a MemberInfo from a string and perform validation
*
* @param string String to parse MemberInfo from
* @param context Context to use for reference mapping
* @return parsed MemberInfo
*/
public static MemberInfo parseAndValidate(String string, IReferenceMapperContext context) throws InvalidMemberDescriptorException {
return MemberInfo.parse(string, context.getReferenceMapper(), context.getClassRef()).validate();
}
/**
* Parse a MemberInfo from a string
*
@@ -371,6 +437,8 @@ public class MemberInfo {
String desc = null;
String owner = null;
name = name.replaceAll("\\s", "");
if (refMapper != null) {
name = refMapper.remap(mixinClass, name);
}