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-compress.git

commit d561559e2745f8cd3c22d6bc8020a32720b80a6c
Author: Gary D. Gregory <garydgreg...@gmail.com>
AuthorDate: Sat Aug 16 07:56:00 2025 -0400

    org.apache.commons.compress.harmony.unpack200 now throws
    Pack200Exception, instead of other runtime exceptions
---
 src/changes/changes.xml                            |  4 ++--
 .../unpack200/bytecode/forms/TableSwitchForm.java  | 24 ++++++----------------
 .../harmony/unpack200/LookupSwitchFormTest.java    |  2 +-
 ...witchFormTest.java => TableSwitchFormTest.java} | 19 +++++++++--------
 4 files changed, 19 insertions(+), 30 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index f214de914..df32edcc2 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -77,9 +77,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action type="fix" dev="ggregory" due-to="Gary 
Gregory">org.apache.commons.compress.archivers.ar.ArArchiveInputStream.read(byte[],
 int, int) now throws ArchiveException instead of ArithmeticException.</action>
       <action type="fix" dev="pkarwasz" due-to="Tyler Nighswander">Simplify 
handling of special AR records in ArArchiveInputStream.</action>
       <!-- FIX unpack200 -->      
-      <action type="fix" dev="ggregory" due-to="Gary Gregory, Stanislav 
Fort">org.apache.commons.compress.harmony.unpack200 now throws 
Pack200Exception, IllegalArgumentException, and IllegalStateException instead 
of Error.</action>
+      <action type="fix" dev="ggregory" due-to="Gary Gregory, Stanislav 
Fort">org.apache.commons.compress.harmony.unpack200 now throws 
Pack200Exception, IllegalArgumentException, and IllegalStateException instead 
of other runtime exceptions and Error.</action>
       <!-- FIX pack200 -->      
-      <action type="fix" dev="ggregory" due-to="Gary 
Gregory">org.apache.commons.compress.harmony.pack200 now throws 
Pack200Exception, IllegalArgumentException, and IllegalStateException instead 
of Error.</action>
+      <action type="fix" dev="ggregory" due-to="Gary 
Gregory">org.apache.commons.compress.harmony.pack200 now throws 
Pack200Exception, IllegalArgumentException, IllegalStateException, instead of 
Error.</action>
       <!-- FIX cpio -->      
       <action type="fix" dev="ggregory" due-to="Stan, Gary 
Gregory">org.apache.commons.compress.archivers.cpio.CpioArchiveEntry now throws 
ArchiveException instead of Arithmetic exception.</action>
       <action type="fix" dev="ggregory" due-to="Stan, Gary 
Gregory">org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream.getNextEntry()
 now throws a MemoryLimitException instead of OutOfMemoryError when it can't 
process input greater than available memory.</action>
diff --git 
a/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/forms/TableSwitchForm.java
 
b/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/forms/TableSwitchForm.java
index 2bf0453e2..2ed5fc8a9 100644
--- 
a/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/forms/TableSwitchForm.java
+++ 
b/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/forms/TableSwitchForm.java
@@ -20,6 +20,7 @@
 
 import java.util.Arrays;
 
+import org.apache.commons.compress.harmony.pack200.Pack200Exception;
 import org.apache.commons.compress.harmony.unpack200.bytecode.ByteCode;
 import org.apache.commons.compress.harmony.unpack200.bytecode.OperandManager;
 
@@ -47,31 +48,25 @@ public TableSwitchForm(final int opcode, final String name) 
{
      * compress.harmony.unpack200.bytecode.ByteCode, 
org.apache.commons.compress.harmony.unpack200.bytecode.OperandManager, int)
      */
     @Override
-    public void setByteCodeOperands(final ByteCode byteCode, final 
OperandManager operandManager, final int codeLength) {
+    public void setByteCodeOperands(final ByteCode byteCode, final 
OperandManager operandManager, final int codeLength) throws Pack200Exception {
         final int caseCount = operandManager.nextCaseCount();
         final int defaultPc = operandManager.nextLabel();
-        int caseValue = -1;
-        caseValue = operandManager.nextCaseValues();
-
-        final int[] casePcs = new int[caseCount];
+        final int caseValue = operandManager.nextCaseValues();
+        final int[] casePcs = new 
int[Pack200Exception.checkIntArray(caseCount)];
         Arrays.setAll(casePcs, i -> operandManager.nextLabel());
-
-        final int[] labelsArray = new int[caseCount + 1];
+        final int[] labelsArray = new 
int[Pack200Exception.checkIntArray(caseCount + 1)];
         labelsArray[0] = defaultPc;
         System.arraycopy(casePcs, 0, labelsArray, 1, caseCount + 1 - 1);
         byteCode.setByteCodeTargets(labelsArray);
-
         final int lowValue = caseValue;
         final int highValue = lowValue + caseCount - 1;
         // All this gets dumped into the rewrite bytes of the
         // poor bytecode.
-
         // Unlike most byte codes, the TableSwitch is a
         // variable-sized bytecode. Because of this, the
         // rewrite array has to be defined here individually
         // for each bytecode, rather than in the ByteCodeForm
         // class.
-
         // First, there's the bytecode. Then there are 0-3
         // bytes of padding so that the first (default)
         // label is on a 4-byte offset.
@@ -80,36 +75,29 @@ public void setByteCodeOperands(final ByteCode byteCode, 
final OperandManager op
                 + 4 // lowbyte
                 + 4 // highbyte
                 + 4 * casePcs.length;
-
-        final int[] newRewrite = new int[rewriteSize];
+        final int[] newRewrite = new 
int[Pack200Exception.checkIntArray(rewriteSize)];
         int rewriteIndex = 0;
-
         // Fill in what we can now
         // opcode
         newRewrite[rewriteIndex++] = byteCode.getOpcode();
-
         // padding
         for (int index = 0; index < padLength; index++) {
             newRewrite[rewriteIndex++] = 0;
         }
-
         // defaultbyte
         // This gets overwritten by fixUpByteCodeTargets
         newRewrite[rewriteIndex++] = -1;
         newRewrite[rewriteIndex++] = -1;
         newRewrite[rewriteIndex++] = -1;
         newRewrite[rewriteIndex++] = -1;
-
         // lowbyte
         final int lowbyteIndex = rewriteIndex;
         setRewrite4Bytes(lowValue, lowbyteIndex, newRewrite);
         rewriteIndex += 4;
-
         // highbyte
         final int highbyteIndex = rewriteIndex;
         setRewrite4Bytes(highValue, highbyteIndex, newRewrite);
         rewriteIndex += 4;
-
         // jump offsets
         // The case_pcs will get overwritten by fixUpByteCodeTargets
         for (int index = 0; index < caseCount; index++) {
diff --git 
a/src/test/java/org/apache/commons/compress/harmony/unpack200/LookupSwitchFormTest.java
 
b/src/test/java/org/apache/commons/compress/harmony/unpack200/LookupSwitchFormTest.java
index 3c5cb2e69..c3fce2d29 100644
--- 
a/src/test/java/org/apache/commons/compress/harmony/unpack200/LookupSwitchFormTest.java
+++ 
b/src/test/java/org/apache/commons/compress/harmony/unpack200/LookupSwitchFormTest.java
@@ -45,7 +45,7 @@ void test() throws Pack200Exception {
         final int[] labels = { 0 };
         final int[] empty = {};
         // The OperandManager requires all 21 band arrays.
-        // Most can be empty for this PoC.
+        // Most can be empty for this test.
         final OperandManager om = new OperandManager(caseCount, empty, empty, 
empty, empty, labels, empty, empty, empty, empty, empty, empty, empty, empty,
                 empty, empty, empty, empty, empty, empty, empty);
         final ByteCode bc = ByteCode.getByteCode(171); // lookupswitch
diff --git 
a/src/test/java/org/apache/commons/compress/harmony/unpack200/LookupSwitchFormTest.java
 
b/src/test/java/org/apache/commons/compress/harmony/unpack200/TableSwitchFormTest.java
similarity index 73%
copy from 
src/test/java/org/apache/commons/compress/harmony/unpack200/LookupSwitchFormTest.java
copy to 
src/test/java/org/apache/commons/compress/harmony/unpack200/TableSwitchFormTest.java
index 3c5cb2e69..a8fce0a56 100644
--- 
a/src/test/java/org/apache/commons/compress/harmony/unpack200/LookupSwitchFormTest.java
+++ 
b/src/test/java/org/apache/commons/compress/harmony/unpack200/TableSwitchFormTest.java
@@ -24,16 +24,16 @@
 import org.apache.commons.compress.harmony.pack200.Pack200Exception;
 import org.apache.commons.compress.harmony.unpack200.bytecode.ByteCode;
 import org.apache.commons.compress.harmony.unpack200.bytecode.OperandManager;
-import 
org.apache.commons.compress.harmony.unpack200.bytecode.forms.LookupSwitchForm;
+import 
org.apache.commons.compress.harmony.unpack200.bytecode.forms.TableSwitchForm;
 import org.junit.jupiter.api.Test;
 
 /**
- * Tests {@link LookupSwitchForm}.
+ * Tests {@link TableSwitchForm}.
  */
-class LookupSwitchFormTest {
+class TableSwitchFormTest {
 
     /**
-     * Run with {@copde -Xmx64m} too get a 
Pack200Exception(MemoryLimitException).
+     * Run with {@code -Xmx64m} to get a 
Pack200Exception(MemoryLimitException).
      *
      * @throws Pack200Exception
      */
@@ -43,13 +43,14 @@ void test() throws Pack200Exception {
         final int[] caseCount = { large };
         // Provide a dummy label for the defaultPc branch taken before the 
allocation.
         final int[] labels = { 0 };
+        final int[] caseValues = { 0 }; // TableSwitch needs at least one case 
value.
         final int[] empty = {};
         // The OperandManager requires all 21 band arrays.
-        // Most can be empty for this PoC.
-        final OperandManager om = new OperandManager(caseCount, empty, empty, 
empty, empty, labels, empty, empty, empty, empty, empty, empty, empty, empty,
+        // Most can be empty for this test.
+        final OperandManager om = new OperandManager(caseCount, caseValues, 
empty, empty, empty, labels, empty, empty, empty, empty, empty, empty, empty, 
empty,
                 empty, empty, empty, empty, empty, empty, empty);
-        final ByteCode bc = ByteCode.getByteCode(171); // lookupswitch
-        // Run with -Xmx64m too get a Pack200Exception(MemoryLimitException).
-        assertThrows(Exception.class, () -> new LookupSwitchForm(171, 
"lookupswitch").setByteCodeOperands(bc, om, 0));
+        final ByteCode bc = ByteCode.getByteCode(170); // tableswitch
+        // Run with -Xmx64m to get a Pack200Exception(MemoryLimitException).
+        assertThrows(Exception.class, () -> new TableSwitchForm(170, 
"tableswitch").setByteCodeOperands(bc, om, 0));
     }
 }

Reply via email to