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 50dd50ec6e4ccd6a70ffcf46d8bf22a2d38905be Author: Gary D. Gregory <garydgreg...@gmail.com> AuthorDate: Mon Aug 4 14:07:08 2025 -0400 org.apache.commons.compress.harmony.unpack200.BandSet now throws ArchiveException instead of Arithmetic exception - org.apache.commons.compress.harmony.pack200.BHSDCodec now throws ArchiveException instead of Arithmetic exception - org.apache.commons.compress.archivers.zip.ExplodingInputStream now throws ArchiveException instead of Arithmetic exception - org.apache.commons.compress.harmony.pack200.RunCodec now throws ArchiveException instead of Arithmetic exception --- src/changes/changes.xml | 4 ++++ .../commons/compress/archivers/ArchiveException.java | 14 ++++++++++++++ .../compress/archivers/zip/ExplodingInputStream.java | 3 +-- .../commons/compress/harmony/pack200/BHSDCodec.java | 6 +++--- .../commons/compress/harmony/pack200/RunCodec.java | 20 ++++++++++---------- .../commons/compress/harmony/unpack200/BandSet.java | 4 ++-- 6 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index ba2464418..1c80e03d4 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -65,6 +65,10 @@ The <action> type attribute can be add,update,fix,remove. <action type="fix" dev="ggregory" due-to="Gary Gregory">Classes in org.apache.commons.compress.compressors now throw a subclass of IOException called CompressorException instead of IOException when a formatting problem is found.</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">Use non-deprecated ArchiveException constructor.</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">org.apache.commons.compress.archivers.ar.ArArchiveInputStream.readGNUStringTable(byte[], int, int) now provides a better exception message, wrapping the underlying exception.</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory">org.apache.commons.compress.harmony.unpack200.BandSet now throws ArchiveException instead of Arithmetic exception.</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory">org.apache.commons.compress.harmony.pack200.BHSDCodec now throws ArchiveException instead of Arithmetic exception.</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory">org.apache.commons.compress.archivers.zip.ExplodingInputStream now throws ArchiveException instead of Arithmetic exception.</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory">org.apache.commons.compress.harmony.pack200.RunCodec now throws ArchiveException instead of Arithmetic exception.</action> <!-- ADD --> <action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.compress.MemoryLimitException.MemoryLimitException(long, long).</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.compress.CompressException.CompressException(String, Object...).</action> diff --git a/src/main/java/org/apache/commons/compress/archivers/ArchiveException.java b/src/main/java/org/apache/commons/compress/archivers/ArchiveException.java index d3a37ba5f..8fdff7d1f 100644 --- a/src/main/java/org/apache/commons/compress/archivers/ArchiveException.java +++ b/src/main/java/org/apache/commons/compress/archivers/ArchiveException.java @@ -48,6 +48,20 @@ public static int addExact(final int x, final int y) throws ArchiveException { } } + /** + * Delegates to {@link Math#addExact(int, int)} wrapping its {@link ArithmeticException} in our {@link ArchiveException}. + * + * @param x the first value. + * @param y the second value. + * @return the result. + * @throws ArchiveException if the result overflows an {@code int}. + * @see Math#addExact(int, int) + * @since 1.29.0 + */ + public static int addExact(final int x, final long y) throws ArchiveException { + return addExact(x, Math.toIntExact(y)); + } + /** * Delegates to {@link Math#addExact(long, long)} wrapping its {@link ArithmeticException} in our {@link ArchiveException}. * diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ExplodingInputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/ExplodingInputStream.java index 93ccda2cf..fbf54b201 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/ExplodingInputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/ExplodingInputStream.java @@ -23,7 +23,6 @@ import java.io.InputStream; import org.apache.commons.compress.archivers.ArchiveException; -import org.apache.commons.compress.utils.ExactMath; import org.apache.commons.compress.utils.InputStreamStatistics; import org.apache.commons.io.input.BoundedInputStream; import org.apache.commons.io.input.CloseShieldInputStream; @@ -143,7 +142,7 @@ private void fillBuffer() throws IOException { // EOF return; } - length = ExactMath.add(length, nextByte); + length = ArchiveException.addExact(length, nextByte); } length += minimumMatchLength; diff --git a/src/main/java/org/apache/commons/compress/harmony/pack200/BHSDCodec.java b/src/main/java/org/apache/commons/compress/harmony/pack200/BHSDCodec.java index 6c1f3ccba..9ce89b8eb 100644 --- a/src/main/java/org/apache/commons/compress/harmony/pack200/BHSDCodec.java +++ b/src/main/java/org/apache/commons/compress/harmony/pack200/BHSDCodec.java @@ -25,7 +25,7 @@ import java.util.Arrays; import java.util.List; -import org.apache.commons.compress.utils.ExactMath; +import org.apache.commons.compress.archivers.ArchiveException; /** * A BHSD codec is a means of encoding integer values as a sequence of bytes or vice versa using a specified "BHSD" encoding mechanism. It uses a @@ -281,7 +281,7 @@ public int[] decodeInts(final int n, final InputStream in) throws IOException, P band[i] -= cardinality; } while (band[i] < smallest) { - band[i] = ExactMath.add(band[i], cardinality); + band[i] = ArchiveException.addExact(band[i], cardinality); } } } @@ -297,7 +297,7 @@ public int[] decodeInts(final int n, final InputStream in, final int firstValue) band[i] -= cardinality; } while (band[i] < smallest) { - band[i] = ExactMath.add(band[i], cardinality); + band[i] = ArchiveException.addExact(band[i], cardinality); } } } diff --git a/src/main/java/org/apache/commons/compress/harmony/pack200/RunCodec.java b/src/main/java/org/apache/commons/compress/harmony/pack200/RunCodec.java index 830c1ddfe..ab082fbfa 100644 --- a/src/main/java/org/apache/commons/compress/harmony/pack200/RunCodec.java +++ b/src/main/java/org/apache/commons/compress/harmony/pack200/RunCodec.java @@ -22,7 +22,7 @@ import java.io.InputStream; import java.util.Arrays; -import org.apache.commons.compress.utils.ExactMath; +import org.apache.commons.compress.archivers.ArchiveException; /** * A run codec is a grouping of two nested codecs; K values are decoded from the first codec, and the remaining codes are decoded from the remaining codec. Note @@ -57,18 +57,18 @@ public int decode(final InputStream in, final long last) throws IOException, Pac if (--k >= 0) { final int value = aCodec.decode(in, this.last); this.last = k == 0 ? 0 : value; - return normalise(value, aCodec); + return normalize(value, aCodec); } this.last = bCodec.decode(in, this.last); - return normalise(this.last, bCodec); + return normalize(this.last, bCodec); } @Override public int[] decodeInts(final int n, final InputStream in) throws IOException, Pack200Exception { final int[] aValues = aCodec.decodeInts(k, in); - normalise(aValues, aCodec); + normalize(aValues, aCodec); final int[] bValues = bCodec.decodeInts(n - k, in); - normalise(bValues, bCodec); + normalize(bValues, bCodec); final int[] band = new int[check(n, in)]; System.arraycopy(aValues, 0, band, 0, k); System.arraycopy(bValues, 0, band, k, n - k); @@ -98,7 +98,7 @@ public int getK() { return k; } - private int normalise(int value, final Codec codecUsed) { + private int normalize(int value, final Codec codecUsed) throws ArchiveException { if (codecUsed instanceof BHSDCodec) { final BHSDCodec bhsd = (BHSDCodec) codecUsed; if (bhsd.isDelta()) { @@ -107,14 +107,14 @@ private int normalise(int value, final Codec codecUsed) { value -= cardinality; } while (value < bhsd.smallest()) { - value = ExactMath.add(value, cardinality); + value = ArchiveException.addExact(value, cardinality); } } } return value; } - private void normalise(final int[] band, final Codec codecUsed) { + private void normalize(final int[] band, final Codec codecUsed) throws ArchiveException { if (codecUsed instanceof BHSDCodec) { final BHSDCodec bhsd = (BHSDCodec) codecUsed; if (bhsd.isDelta()) { @@ -124,7 +124,7 @@ private void normalise(final int[] band, final Codec codecUsed) { band[i] -= cardinality; } while (band[i] < bhsd.smallest()) { - band[i] = ExactMath.add(band[i], cardinality); + band[i] = ArchiveException.addExact(band[i], cardinality); } } } @@ -143,7 +143,7 @@ private void normalise(final int[] band, final Codec codecUsed) { band[i] -= cardinality; } while (band[i] < bhsd.smallest()) { - band[i] = ExactMath.add(band[i], cardinality); + band[i] = ArchiveException.addExact(band[i], cardinality); } } } 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 c919cc168..55db0ecd8 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 @@ -22,6 +22,7 @@ import java.io.InputStream; import java.util.Arrays; +import org.apache.commons.compress.archivers.ArchiveException; import org.apache.commons.compress.harmony.pack200.BHSDCodec; import org.apache.commons.compress.harmony.pack200.Codec; import org.apache.commons.compress.harmony.pack200.CodecEncoding; @@ -38,7 +39,6 @@ import org.apache.commons.compress.harmony.unpack200.bytecode.CPNameAndType; import org.apache.commons.compress.harmony.unpack200.bytecode.CPString; import org.apache.commons.compress.harmony.unpack200.bytecode.CPUTF8; -import org.apache.commons.compress.utils.ExactMath; import org.apache.commons.lang3.ArrayUtils; /** @@ -126,7 +126,7 @@ public int[] decodeBandInt(final String name, final InputStream in, final BHSDCo band[i] -= cardinality; } while (band[i] < bhsd.smallest()) { - band[i] = ExactMath.add(band[i], cardinality); + band[i] = ArchiveException.addExact(band[i], cardinality); } } }