From 4f51eb55710706a3dbd941d5af1c9b4be9948d27 Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Sun, 18 Oct 2020 16:15:46 +0100 Subject: [PATCH] Strip out-of-bounds shifts from injection point results, fixes #409 --- .../asm/mixin/injection/InjectionPoint.java | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java index 5d13cda3..33ecc1b0 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java @@ -36,6 +36,7 @@ import java.util.Collection; import java.util.HashMap; import java.util.LinkedHashSet; import java.util.List; +import java.util.ListIterator; import java.util.Map; import org.apache.logging.log4j.LogManager; @@ -454,9 +455,23 @@ public abstract class InjectionPoint { List list = (nodes instanceof List) ? (List) nodes : new ArrayList(nodes); this.input.find(desc, insns, nodes); - - for (int i = 0; i < list.size(); i++) { - list.set(i, insns.get(insns.indexOf(list.get(i)) + this.shift)); + + for (ListIterator iter = list.listIterator(); iter.hasNext();) { + int sourceIndex = insns.indexOf(iter.next()); + int newIndex = sourceIndex + this.shift; + if (newIndex >= 0 && newIndex < insns.size()) { + iter.set(insns.get(newIndex)); + } else { + // Shifted beyond the start or end of the insnlist, into the dark void + iter.remove(); + + // Decorate the injector with the info in case it fails + int absShift = Math.abs(this.shift); + char operator = absShift != this.shift ? '-' : '+'; + this.input.addMessage( + "@At.shift offset outside the target bounds: Index (index(%d) %s offset(%d) = %d) is outside the allowed range (0-%d)", + sourceIndex, operator, absShift, newIndex, insns.size()); + } } if (nodes != list) {