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


The following commit(s) were added to refs/heads/master by this push:
     new ae30dac8f reject negative element counts in BandSet band decoding 
(#782)
ae30dac8f is described below

commit ae30dac8f55866034dbe679a85331d7b6b4b0238
Author: KALI 834X <[email protected]>
AuthorDate: Fri Jul 10 18:55:42 2026 +0530

    reject negative element counts in BandSet band decoding (#782)
---
 .../commons/compress/harmony/unpack200/BandSet.java       |  9 +++++++++
 .../commons/compress/harmony/unpack200/BandSetTest.java   | 15 +++++++++++++++
 2 files changed, 24 insertions(+)

diff --git 
a/src/main/java/org/apache/commons/compress/harmony/unpack200/BandSet.java 
b/src/main/java/org/apache/commons/compress/harmony/unpack200/BandSet.java
index 7c6c387f1..1c5552529 100644
--- a/src/main/java/org/apache/commons/compress/harmony/unpack200/BandSet.java
+++ b/src/main/java/org/apache/commons/compress/harmony/unpack200/BandSet.java
@@ -151,6 +151,9 @@ public int[][] decodeBandInt(final String name, final 
InputStream in, final BHSD
         final int[][] result = new int[counts.length][];
         int totalCount = 0;
         for (final int count : counts) {
+            if (count < 0) {
+                throw new Pack200Exception("count < 0");
+            }
             totalCount += count;
         }
         final int[] twoDResult = decodeBandInt(name, in, defaultCodec, 
totalCount);
@@ -549,6 +552,9 @@ public long[][] parseFlags(final String name, final 
InputStream in, final int[]
         int sum = 0;
         final long[][] result = new long[count][];
         for (int i = 0; i < count; i++) {
+            if (counts[i] < 0) {
+                throw new Pack200Exception("count < 0");
+            }
             sum += counts[i];
         }
         int[] hi = null;
@@ -630,6 +636,9 @@ public String[][] parseReferences(final String name, final 
InputStream in, final
         }
         int sum = 0;
         for (int i = 0; i < count; i++) {
+            if (counts[i] < 0) {
+                throw new Pack200Exception("count < 0");
+            }
             sum += counts[i];
         }
         // TODO Merge the decode and parsing of a multiple structure into one
diff --git 
a/src/test/java/org/apache/commons/compress/harmony/unpack200/BandSetTest.java 
b/src/test/java/org/apache/commons/compress/harmony/unpack200/BandSetTest.java
index eb60c729f..48e0b8393 100644
--- 
a/src/test/java/org/apache/commons/compress/harmony/unpack200/BandSetTest.java
+++ 
b/src/test/java/org/apache/commons/compress/harmony/unpack200/BandSetTest.java
@@ -19,6 +19,7 @@
 package org.apache.commons.compress.harmony.unpack200;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
@@ -63,6 +64,20 @@ void testDecodeBandInt() throws IOException, 
Pack200Exception {
         }
     }
 
+    @Test
+    void testDecodeBandIntRejectsNegativeCount() {
+        final BHSDCodec codec = Codec.BYTE1;
+        // A per-element count decoded from a signed band can be negative. It 
must be rejected the
+        // same way the scalar decodeBandInt(..., int) overload rejects count 
< 0, instead of
+        // reaching new int[]/new long[]/new String[] and surfacing a 
NegativeArraySizeException.
+        assertThrows(Pack200Exception.class,
+                () -> bandSet.decodeBandInt("Test", new 
ByteArrayInputStream(new byte[0]), codec, new int[] { -1, 1 }));
+        assertThrows(Pack200Exception.class,
+                () -> bandSet.parseFlags("Test", new ByteArrayInputStream(new 
byte[0]), new int[] { -1, 1 }, codec, false));
+        assertThrows(Pack200Exception.class,
+                () -> bandSet.parseReferences("Test", new 
ByteArrayInputStream(new byte[0]), codec, new int[] { -1, 1 }, new String[] { 
"a" }));
+    }
+
     @Test
     @Disabled("TODO: Implement")
     void testParseFlags1() {

Reply via email to