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 593db13a3 Reject negative counts in pack200
parseCPUTF8/SignatureReferences (#789)
593db13a3 is described below
commit 593db13a39ea966a3ee2bfd209b999e5a286f298
Author: KALI 834X <[email protected]>
AuthorDate: Thu Jul 23 08:32:34 2026 +0530
Reject negative counts in pack200 parseCPUTF8/SignatureReferences (#789)
---
.../apache/commons/compress/harmony/unpack200/BandSet.java | 11 ++---------
.../commons/compress/harmony/unpack200/BandSetTest.java | 13 +++++++++++++
2 files changed, 15 insertions(+), 9 deletions(-)
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 a885208c3..d8975fe6e 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
@@ -431,10 +431,7 @@ public CPUTF8[] parseCPSignatureReferences(final String
name, final InputStream
*/
protected CPUTF8[][] parseCPSignatureReferences(final String name, final
InputStream in, final BHSDCodec codec, final int[] counts)
throws IOException, Pack200Exception {
- int sum = 0;
- for (final int count : counts) {
- sum += count;
- }
+ final int sum = sumNonNegative(counts);
final int[] indices = decodeBandInt(name, in, codec, sum);
final CpBands cpBands = segment.getCpBands();
final CPUTF8[] result1 = ArrayUtils.setAll(new CPUTF8[sum], i ->
cpBands.cpSignatureValue(indices[i]));
@@ -498,12 +495,8 @@ public CPUTF8[] parseCPUTF8References(final String name,
final InputStream in, f
*/
public CPUTF8[][] parseCPUTF8References(final String name, final
InputStream in, final BHSDCodec codec, final int[] counts)
throws IOException, Pack200Exception {
+ final int sum = sumNonNegative(counts);
final CPUTF8[][] result = new CPUTF8[counts.length][];
- int sum = 0;
- for (int i = 0; i < counts.length; i++) {
- result[i] = new CPUTF8[counts[i]];
- sum += counts[i];
- }
final int[] indices = decodeBandInt(name, in, codec, sum);
final CpBands cpBands = segment.getCpBands();
final CPUTF8[] result1 = ArrayUtils.setAll(new CPUTF8[sum], i ->
cpBands.cpUTF8Value(indices[i]));
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 a96e21f52..4347bab73 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
@@ -78,6 +78,19 @@ void testDecodeBandIntRejectsNegativeCount() {
() -> bandSet.parseReferences("Test", new
ByteArrayInputStream(new byte[0]), codec, new int[] { -1, 1 }, new String[] {
"a" }));
}
+ @Test
+ void testParseCPUTF8AndSignatureReferencesRejectNegativeCount() {
+ final BHSDCodec codec = Codec.BYTE1;
+ // The int[] overloads of parseCPUTF8References and
parseCPSignatureReferences size each sub-array straight
+ // from a per-entry count. A count decoded through a signed band can
be negative, so it must be rejected
+ // here the same way decodeBandInt/parseFlags/parseReferences reject
it, instead of reaching new CPUTF8[-1]
+ // and surfacing a NegativeArraySizeException that escapes the
declared Pack200Exception contract.
+ assertThrows(Pack200Exception.class,
+ () -> bandSet.parseCPUTF8References("Test", new
ByteArrayInputStream(new byte[0]), codec, new int[] { -1, 1 }));
+ assertThrows(Pack200Exception.class,
+ () -> bandSet.parseCPSignatureReferences("Test", new
ByteArrayInputStream(new byte[0]), codec, new int[] { -1, 1 }));
+ }
+
@Test
void testGetReferencesRejectsOutOfRangeIndex() throws Exception {
// getReferences resolves band-decoded indices into a constant-pool
array. An index at or past the