You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2025/08/13 21:50:04 UTC

(commons-compress) 04/06: org.apache.commons.compress.archivers.sevenz.SevenZFile now throws an ArchiveException if a result overflows a long (or an int, depending on the context)

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 579446c8688ac8539b021adde1a1f5cdea49651f
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Wed Aug 13 17:27:37 2025 -0400

    org.apache.commons.compress.archivers.sevenz.SevenZFile now throws an
    ArchiveException if a result overflows a long (or an int, depending on
    the context)
---
 src/changes/changes.xml                                    |  2 +-
 .../commons/compress/archivers/sevenz/SevenZFile.java      | 14 +++++++++-----
 2 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 0d4323ce9..4dfe165af 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -50,7 +50,7 @@ The <action> type attribute can be add,update,fix,remove.
       <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix kilobyte to kibibyte conversion in org.apache.commons.compress.archivers.sevenz.SevenZFile.ArchiveStatistics.assertValidity(int).</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix kilobyte to kibibyte conversion in org.apache.commons.compress.archivers.sevenz.SevenZFile.ArchiveStatistics.toString().</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix kilobyte to kibibyte conversion in org.apache.commons.compress.archivers.sevenz.SevenZFile.Builder.setMaxMemoryLimitKb(int).</action>
-      <action type="fix" dev="ggregory" due-to="Tyler Nighswander, Gary Gregory">org.apache.commons.compress.archivers.sevenz.SevenZFile now throws an ArchiveException if a result overflows a long.</action>
+      <action type="fix" dev="ggregory" due-to="Tyler Nighswander, Gary Gregory">org.apache.commons.compress.archivers.sevenz.SevenZFile now throws an ArchiveException if a result overflows a long (or an int, depending on the context).</action>
       <action type="fix" dev="ggregory" due-to="Tyler Nighswander, Gary Gregory">org.apache.commons.compress.archivers.sevenz.SevenZFile.Builder.get() now throws an ArchiveException instead of an IllegalArgumentException.</action>
       <action type="fix" dev="ggregory" due-to="Tyler Nighswander, Gary Gregory">Fix format string in org.apache.commons.compress.archivers.sevenz.SevenZFile.assertFitsIntoNonNegativeInt(String, long).</action>
       <action type="fix" dev="ggregory" due-to="Tyler Nighswander, Gary Gregory, Stanislav Fort">org.apache.commons.compress.archivers.sevenz.SevenZFile now throws a MemoryLimitException (an IOException subclass) instead of an OutOfMemoryError.</action>
diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
index 837814b86..6f686590c 100644
--- a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
+++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
@@ -435,6 +435,10 @@ private static long readUint64(final ByteBuffer in) throws IOException {
         return value;
     }
 
+    private static int readUint64ToIntExact(final ByteBuffer in) throws IOException {
+        return ArchiveException.toIntExact(readUint64(in));
+    }
+
     private static long skipBytesFully(final ByteBuffer input, long bytesToSkip) {
         if (bytesToSkip < 1) {
             return 0;
@@ -1296,7 +1300,7 @@ private ByteBuffer readEncodedHeader(final ByteBuffer header, final Archive arch
     }
 
     private void readFilesInfo(final ByteBuffer header, final Archive archive) throws IOException {
-        final int numFilesInt = (int) readUint64(header);
+        final int numFilesInt = readUint64ToIntExact(header);
         final Map<Integer, SevenZArchiveEntry> fileMap = new LinkedHashMap<>();
         BitSet isEmptyStream = null;
         BitSet isEmptyFile = null;
@@ -1578,13 +1582,13 @@ private Archive readHeaders(final byte[] password) throws IOException {
         if (tryToRecoverBrokenArchives) {
             return tryToLocateEndHeader(password);
         }
-        throw new ArchiveException("Archive seems to be invalid.\nYou may want to retry and enable the"
-                + " tryToRecoverBrokenArchives if the archive could be a multi volume archive that has been closed prematurely.");
+        throw new ArchiveException("Archive seems to be invalid. You may want to retry and enable the tryToRecoverBrokenArchives if "
+                + "the archive could be a multi volume archive that has been closed prematurely.");
     }
 
     private void readPackInfo(final ByteBuffer header, final Archive archive) throws IOException {
         archive.packPos = readUint64(header);
-        final int numPackStreamsInt = (int) readUint64(header);
+        final int numPackStreamsInt = readUint64ToIntExact(header);
         int nid = getUnsignedByte(header);
         if (nid == NID.kSize) {
             archive.packSizes = new long[numPackStreamsInt];
@@ -1723,7 +1727,7 @@ private void readSubStreamsInfo(final ByteBuffer header, final Archive archive)
 
     private void readUnpackInfo(final ByteBuffer header, final Archive archive) throws IOException {
         int nid = getUnsignedByte(header);
-        final int numFoldersInt = (int) readUint64(header);
+        final int numFoldersInt = readUint64ToIntExact(header);
         final Folder[] folders = new Folder[numFoldersInt];
         archive.folders = folders;
         /* final int external = */ getUnsignedByte(header);