Skip to content

Commit eccd0dc

Browse files
magarciaEPFLlrytz
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. Cherry-pick of b50e6b4.
1 parent 9a7e518 commit eccd0dc

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,14 @@ public void set(final AbstractInsnNode location, final AbstractInsnNode insn) {
244244
* {@link InsnList}</i>.
245245
*/
246246
public void add(final AbstractInsnNode insn) {
247+
if(insn.prev != null || insn.next != null) {
248+
// Adding an instruction that still refers to others (in the same or another InsnList) leads to hard to debug bugs.
249+
// Initially everything may look ok (e.g. iteration follows `next` thus a stale `prev` isn't noticed).
250+
// However, a stale link brings the doubly-linked into disarray e.g. upon removing an element,
251+
// which results in the `next` of a stale `prev` being updated, among other failure scenarios.
252+
// Better fail early.
253+
throw new RuntimeException("Instruction " + insn + " already belongs to some InsnList.");
254+
}
247255
++size;
248256
if (last == null) {
249257
first = insn;

0 commit comments

Comments
 (0)