This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-bcel.git

commit b576aa055f641141cacda09f534ee0c2b73cd7f0
Author: Gary Gregory <[email protected]>
AuthorDate: Mon Jan 12 09:40:56 2026 -0500

    Code.setCode(byte[]) now throws a ClassFormatException if the code array
    is greater than the JVM specification allows.
    
    - Adjust min code array test to 0
    - Clean up tests
---
 src/changes/changes.xml                                 |  1 +
 src/main/java/org/apache/bcel/classfile/Code.java       |  7 +++++--
 src/test/java/org/apache/bcel/ConstTest.java            |  8 ++++++++
 .../org/apache/bcel/classfile/CodeExceptionTest.java    | 14 ++------------
 .../classfile/{CodeExceptionTest.java => CodeTest.java} | 17 +++++++++--------
 .../java/org/apache/bcel/classfile/ConstantTest.java    |  1 +
 6 files changed, 26 insertions(+), 22 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 5fb583c3..c1193529 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -69,6 +69,7 @@ The <action> type attribute can be add,update,fix,remove.
       <action                  type="fix" dev="ggregory" due-to="Gary 
Gregory">Exception message in Args.requireU2() refers to the wrong upper range 
value.</action>
       <action                  type="fix" dev="ggregory" due-to="Gary 
Gregory">Code.Code(int, int, DataInput, ConstantPool) now throws a 
ClassFormatException if the code array is greater than the JVM specification 
allows.</action>
       <action                  type="fix" dev="ggregory" due-to="Gary 
Gregory">Code.Code(int, int, int, int, byte[], CodeException[], Attribute[], 
ConstantPool) now throws a ClassFormatException if the code array is greater 
than the JVM specification allows.</action>
+      <action                  type="fix" dev="ggregory" due-to="Gary 
Gregory">Code.setCode(byte[]) now throws a ClassFormatException if the code 
array is greater than the JVM specification allows.</action>
       <!-- ADD -->
       <action                  type="add" dev="ggregory" due-to="Gary 
Gregory">Add Const.MAJOR_26.</action>
       <action                  type="add" dev="ggregory" due-to="Gary 
Gregory">Add Const.MINOR_26.</action>
diff --git a/src/main/java/org/apache/bcel/classfile/Code.java 
b/src/main/java/org/apache/bcel/classfile/Code.java
index 86ce4b65..b812c7ae 100644
--- a/src/main/java/org/apache/bcel/classfile/Code.java
+++ b/src/main/java/org/apache/bcel/classfile/Code.java
@@ -16,6 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
+
 package org.apache.bcel.classfile;
 
 import java.io.DataInput;
@@ -92,7 +93,7 @@ public final class Code extends Attribute {
         // Initialize with some default values which will be overwritten later
         this(nameIndex, length, dataInput.readUnsignedShort(), 
dataInput.readUnsignedShort(), (byte[]) null, (CodeException[]) null, 
(Attribute[]) null,
                 constantPool);
-        final int codeLength = Args.requireU4(dataInput.readInt(), 1, 
Const.MAX_CODE_SIZE, "Code length attribute");
+        final int codeLength = Args.requireU4(dataInput.readInt(), 0, 
Const.MAX_CODE_SIZE, "Code length attribute");
         code = new byte[codeLength]; // Read byte code
         dataInput.readFully(code);
         /*
@@ -137,7 +138,7 @@ public final class Code extends Attribute {
         this.maxStack = Args.requireU2(maxStack, "maxStack");
         this.maxLocals = Args.requireU2(maxLocals, "maxLocals");
         this.code = ArrayUtils.nullToEmpty(code);
-        Args.requireU4(this.code.length, 1, Const.MAX_CODE_SIZE, "Code length 
attribute");
+        Args.requireU4(this.code.length, 0, Const.MAX_CODE_SIZE, "Code length 
attribute");
         this.exceptionTable = ArrayUtils.nullToEmpty(exceptionTable, 
CodeException[].class);
         Args.requireU2(this.exceptionTable.length, "exceptionTable.length");
         this.attributes = attributes != null ? attributes : EMPTY_ARRAY;
@@ -342,9 +343,11 @@ public final class Code extends Attribute {
      * Sets the byte code.
      *
      * @param code byte code.
+     * @throws ClassFormatException if the code array is greater than {@link 
Const#MAX_CODE_SIZE}.
      */
     public void setCode(final byte[] code) {
         this.code = ArrayUtils.nullToEmpty(code);
+        Args.requireU4(this.code.length, 0, Const.MAX_CODE_SIZE, "Code length 
attribute");
         super.setLength(calculateLength()); // Adjust length
     }
 
diff --git a/src/test/java/org/apache/bcel/ConstTest.java 
b/src/test/java/org/apache/bcel/ConstTest.java
index 3881bb75..5f3e33d0 100644
--- a/src/test/java/org/apache/bcel/ConstTest.java
+++ b/src/test/java/org/apache/bcel/ConstTest.java
@@ -57,4 +57,12 @@ class ConstTest {
         assertEquals(70, Const.MAJOR_26);
         assertEquals(0, Const.MINOR_26);
     }
+
+    /**
+     * Tests that we do not break binary compatibility with BCEL-330.
+     */
+    @Test
+    void testReferenceToConstant() {
+        assertEquals(Const.AALOAD, Constants.AALOAD);
+    }
 }
diff --git a/src/test/java/org/apache/bcel/classfile/CodeExceptionTest.java 
b/src/test/java/org/apache/bcel/classfile/CodeExceptionTest.java
index 442cb686..a5d16b61 100644
--- a/src/test/java/org/apache/bcel/classfile/CodeExceptionTest.java
+++ b/src/test/java/org/apache/bcel/classfile/CodeExceptionTest.java
@@ -16,19 +16,9 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.bcel.classfile;
 
-import org.apache.bcel.Constants;
-import org.junit.jupiter.api.Test;
+package org.apache.bcel.classfile;
 
 class CodeExceptionTest {
-
-    /**
-     * Tests that we do not break binary compatibility with BCEL-330.
-     */
-    @Test
-    void testReferenceToConstant() {
-        @SuppressWarnings("unused")
-        final short referenceToConstant = Constants.AALOAD;
-    }
+    // TODO
 }
diff --git a/src/test/java/org/apache/bcel/classfile/CodeExceptionTest.java 
b/src/test/java/org/apache/bcel/classfile/CodeTest.java
similarity index 67%
copy from src/test/java/org/apache/bcel/classfile/CodeExceptionTest.java
copy to src/test/java/org/apache/bcel/classfile/CodeTest.java
index 442cb686..460f187c 100644
--- a/src/test/java/org/apache/bcel/classfile/CodeExceptionTest.java
+++ b/src/test/java/org/apache/bcel/classfile/CodeTest.java
@@ -16,19 +16,20 @@
  * specific language governing permissions and limitations
  * under the License.
  */
+
 package org.apache.bcel.classfile;
 
-import org.apache.bcel.Constants;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.apache.bcel.Const;
+import org.apache.bcel.util.SyntheticRepository;
 import org.junit.jupiter.api.Test;
 
-class CodeExceptionTest {
+public class CodeTest {
 
-    /**
-     * Tests that we do not break binary compatibility with BCEL-330.
-     */
     @Test
-    void testReferenceToConstant() {
-        @SuppressWarnings("unused")
-        final short referenceToConstant = Constants.AALOAD;
+    void testSetCode() throws ClassNotFoundException {
+        assertThrows(ClassFormatException.class,
+                () -> 
SyntheticRepository.getInstance().loadClass(getClass().getName()).getMethods()[0].getCode().setCode(new
 byte[Const.MAX_CODE_SIZE + 1]));
     }
 }
diff --git a/src/test/java/org/apache/bcel/classfile/ConstantTest.java 
b/src/test/java/org/apache/bcel/classfile/ConstantTest.java
index 8473891f..868fedb6 100644
--- a/src/test/java/org/apache/bcel/classfile/ConstantTest.java
+++ b/src/test/java/org/apache/bcel/classfile/ConstantTest.java
@@ -37,4 +37,5 @@ class ConstantTest {
         assertFalse(Constant.getComparator().equals(obj, null));
         assertFalse(Constant.getComparator().equals(null, obj));
     }
+
 }

Reply via email to