Skip to content

Commit 33c75f4

Browse files
lrytzSethTisue
authored andcommitted
[asm-cherry-pick] Ensure instructions belong only to one list
Fail early when adding an instruction that already belongs to a different instruction list. Originally added in lrytz/scala@eccd0dc
1 parent a837556 commit 33c75f4

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/main/java/scala/tools/asm/tree/InsnList.java

+8
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,14 @@ public void set(final AbstractInsnNode oldInsnNode, final AbstractInsnNode newIn
223223
* @param insnNode an instruction, <i>which must not belong to any {@link InsnList}</i>.
224224
*/
225225
public void add(final AbstractInsnNode insnNode) {
226+
if(insnNode.previousInsn != null || insnNode.nextInsn != null) {
227+
// Adding an instruction that still refers to others (in the same or another InsnList) leads to hard to debug bugs.
228+
// Initially everything may look ok (e.g. iteration follows `next` thus a stale `prev` isn't noticed).
229+
// However, a stale link brings the doubly-linked into disarray e.g. upon removing an element,
230+
// which results in the `next` of a stale `prev` being updated, among other failure scenarios.
231+
// Better fail early.
232+
throw new RuntimeException("Instruction " + insnNode + " already belongs to some InsnList.");
233+
}
226234
++size;
227235
if (lastInsn == null) {
228236
firstInsn = insnNode;

0 commit comments

Comments
 (0)