This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-bcel.git
The following commit(s) were added to refs/heads/master by this push:
new 3a49a2da set length in INVOKEDYNAMIC constructor (#521)
3a49a2da is described below
commit 3a49a2da8e65d3e712e37cc0b30e5e294c4783f0
Author: Naveed Khan <[email protected]>
AuthorDate: Mon Jul 13 15:36:12 2026 +0000
set length in INVOKEDYNAMIC constructor (#521)
The constructor never set the length, so a generated invokedynamic reported
getLength() 3 while dump() writes 5 bytes, shifting the offsets of every
following instruction in InstructionList.setPositions(). Match INVOKEINTERFACE,
MULTIANEWARRAY and this class's own initFromFile.
---
.../org/apache/bcel/generic/INVOKEDYNAMIC.java | 1 +
.../org/apache/bcel/generic/INVOKEDYNAMICTest.java | 56 ++++++++++++++++++++++
2 files changed, 57 insertions(+)
diff --git a/src/main/java/org/apache/bcel/generic/INVOKEDYNAMIC.java
b/src/main/java/org/apache/bcel/generic/INVOKEDYNAMIC.java
index beba5324..7f7e9992 100644
--- a/src/main/java/org/apache/bcel/generic/INVOKEDYNAMIC.java
+++ b/src/main/java/org/apache/bcel/generic/INVOKEDYNAMIC.java
@@ -51,6 +51,7 @@ public class INVOKEDYNAMIC extends InvokeInstruction {
*/
public INVOKEDYNAMIC(final int index) {
super(Const.INVOKEDYNAMIC, index);
+ super.setLength(5);
}
/**
diff --git a/src/test/java/org/apache/bcel/generic/INVOKEDYNAMICTest.java
b/src/test/java/org/apache/bcel/generic/INVOKEDYNAMICTest.java
new file mode 100644
index 00000000..3881f21c
--- /dev/null
+++ b/src/test/java/org/apache/bcel/generic/INVOKEDYNAMICTest.java
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.bcel.generic;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link INVOKEDYNAMIC}.
+ *
+ * @see <a
href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.invokedynamic">The
invokedynamic instruction</a>
+ */
+class INVOKEDYNAMICTest {
+
+ /**
+ * {@code invokedynamic} occupies 5 bytes (opcode, u2 index, two zero
bytes), which is what {@code dump()} writes and
+ * {@code initFromFile()} sets, so the constructed instruction must also
report a length of 5.
+ */
+ @Test
+ void testConstructorSetsLength() {
+ assertEquals(5, new INVOKEDYNAMIC(1).getLength());
+ }
+
+ /**
+ * {@link InstructionList#setPositions()} adds up {@code getLength()} to
place each instruction, so an understated
+ * invokedynamic length shifts the offset of every following instruction.
+ */
+ @Test
+ void testInstructionListPositionsAccountForFullLength() {
+ final InstructionList il = new InstructionList();
+ il.append(new NOP());
+ il.append(new INVOKEDYNAMIC(1));
+ final InstructionHandle after = il.append(new NOP());
+ il.setPositions();
+ // NOP (1) + invokedynamic (5) places the following NOP at offset 6
+ assertEquals(6, after.getPosition());
+ }
+}