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 bf274cfd Read ConstantDynamic indices as unsigned shorts (#505)
bf274cfd is described below

commit bf274cfd5742063e70d9690ba0ff966a8f884c07
Author: Dexter.k <[email protected]>
AuthorDate: Fri Jun 19 11:58:49 2026 +0000

    Read ConstantDynamic indices as unsigned shorts (#505)
---
 .../org/apache/bcel/classfile/ConstantDynamic.java |  2 +-
 .../apache/bcel/classfile/ConstantDynamicTest.java | 52 ++++++++++++++++++++++
 2 files changed, 53 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/bcel/classfile/ConstantDynamic.java 
b/src/main/java/org/apache/bcel/classfile/ConstantDynamic.java
index dc5e7552..9f498e42 100644
--- a/src/main/java/org/apache/bcel/classfile/ConstantDynamic.java
+++ b/src/main/java/org/apache/bcel/classfile/ConstantDynamic.java
@@ -50,7 +50,7 @@ public final class ConstantDynamic extends ConstantCP {
      * @throws IOException if an I/O error occurs.
      */
     ConstantDynamic(final DataInput file) throws IOException {
-        this(file.readShort(), file.readShort());
+        this(file.readUnsignedShort(), file.readUnsignedShort());
     }
 
     /**
diff --git a/src/test/java/org/apache/bcel/classfile/ConstantDynamicTest.java 
b/src/test/java/org/apache/bcel/classfile/ConstantDynamicTest.java
new file mode 100644
index 00000000..aeb81464
--- /dev/null
+++ b/src/test/java/org/apache/bcel/classfile/ConstantDynamicTest.java
@@ -0,0 +1,52 @@
+/*
+ * 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.classfile;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.io.ByteArrayInputStream;
+import java.io.DataInputStream;
+import java.io.IOException;
+
+import org.apache.bcel.Const;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link ConstantDynamic}.
+ *
+ * @see <a 
href="https://docs.oracle.com/javase/specs/jvms/se25/html/jvms-4.html#jvms-4.4.10";>JVM
 CONSTANT_Dynamic_info specification</a>
+ */
+class ConstantDynamicTest {
+
+    /**
+     * Both index fields of {@code CONSTANT_Dynamic_info} are unsigned shorts 
(u2), so a value above 0x7FFF must not be read as a
+     * negative number, matching {@link ConstantInvokeDynamic}.
+     */
+    @Test
+    void testReadConstantReadsIndicesUnsigned() throws IOException {
+        // CONSTANT_Dynamic, bootstrap_method_attr_index 0x9C40 (40000), 
name_and_type_index 0xABCD (43981)
+        final byte[] data = {Const.CONSTANT_Dynamic, (byte) 0x9C, 0x40, (byte) 
0xAB, (byte) 0xCD};
+        try (DataInputStream dis = new DataInputStream(new 
ByteArrayInputStream(data))) {
+            final ConstantDynamic c = (ConstantDynamic) 
Constant.readConstant(dis);
+            assertEquals(40000, c.getBootstrapMethodAttrIndex());
+            assertEquals(43981, c.getNameAndTypeIndex());
+        }
+    }
+}

Reply via email to