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 3bf8acf7a TarArchiveStructSparse.TarArchiveStructSparse(long, long) 
now throws ArchiveException instead of IllegalArgumentException.
3bf8acf7a is described below

commit 3bf8acf7ab387a6f2d856779dcd8eccd6aee4197
Author: Gary Gregory <[email protected]>
AuthorDate: Sun Aug 9 10:15:57 2026 -0400

    TarArchiveStructSparse.TarArchiveStructSparse(long, long) now throws
    ArchiveException instead of IllegalArgumentException.
    
    Simplify input validation.
---
 .../archivers/tar/TarArchiveStructSparse.java      | 16 ++++++-------
 .../commons/compress/archivers/tar/TarUtils.java   | 26 ++++++++++------------
 .../compress/archivers/tar/TarUtilsTest.java       |  2 +-
 3 files changed, 20 insertions(+), 24 deletions(-)

diff --git 
a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveStructSparse.java
 
b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveStructSparse.java
index 43d411036..b2cdaf9a2 100644
--- 
a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveStructSparse.java
+++ 
b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveStructSparse.java
@@ -20,6 +20,8 @@
 
 import java.util.Objects;
 
+import org.apache.commons.compress.archivers.ArchiveException;
+
 /**
  * A {@code struct sparse} in a <a 
href="https://www.gnu.org/software/tar/manual/html_node/Standard.html";>TAR 
archive</a>.
  * <p>
@@ -35,6 +37,7 @@
  * @since 1.20
  */
 public final class TarArchiveStructSparse {
+
     private final long offset;
     private final long numbytes;
 
@@ -43,16 +46,11 @@ public final class TarArchiveStructSparse {
      *
      * @param offset An offset greater or equal to zero.
      * @param numBytes A count greater or equal to zero.
+     * @throws ArchiveException Thrown if either {@code offset} or {@code 
numBytes} is negative.
      */
-    public TarArchiveStructSparse(final long offset, final long numBytes) {
-        if (offset < 0) {
-            throw new IllegalArgumentException("offset must not be negative");
-        }
-        if (numBytes < 0) {
-            throw new IllegalArgumentException("numBytes must not be 
negative");
-        }
-        this.offset = offset;
-        this.numbytes = numBytes;
+    public TarArchiveStructSparse(final long offset, final long numBytes) 
throws ArchiveException {
+        this.offset = ArchiveException.requireNonNegative(offset, 
"TarArchiveStructSparse offset must not be negative.");
+        this.numbytes = ArchiveException.requireNonNegative(numBytes, 
"TarArchiveStructSparse numbytes must not be negative.");
     }
 
     @Override
diff --git 
a/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java 
b/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java
index f24db5496..c5e01ff4a 100644
--- a/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java
+++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java
@@ -468,11 +468,8 @@ static List<TarArchiveStructSparse> 
parseFromPAX01SparseHeaders(final String spa
             throw new ArchiveException("Corrupted TAR archive. Bad format in 
GNU.sparse.map PAX Header");
         }
         for (int i = 0; i < sparseHeaderStrings.length; i += 2) {
-            final long sparseOffset = 
ArchiveException.requireNonNegative(ParsingUtils.parseLongValue(sparseHeaderStrings[i]),
-                    "Corrupted TAR archive. Sparse struct offset contains 
negative value");
-            final long sparseNumbytes = 
ArchiveException.requireNonNegative(ParsingUtils.parseLongValue(sparseHeaderStrings[i
 + 1]),
-                    "Corrupted TAR archive. Sparse struct numbytes contains 
negative value");
-            sparseHeaders.add(new TarArchiveStructSparse(sparseOffset, 
sparseNumbytes));
+            sparseHeaders.add(
+                    new 
TarArchiveStructSparse(ParsingUtils.parseLongValue(sparseHeaderStrings[i]), 
ParsingUtils.parseLongValue(sparseHeaderStrings[i + 1])));
         }
         return Collections.unmodifiableList(sparseHeaders);
     }
@@ -618,7 +615,7 @@ public static long parseOctalOrBinary(final byte[] buffer, 
final int offset, fin
      * @param inputStream parsing source.
      * @param recordSize  The size the TAR header.
      * @return sparse headers.
-     * @throws IOException if an I/O error occurs.
+     * @throws IOException if an I/O error occurs or the sparse header is 
malformed.
      */
     static List<TarArchiveStructSparse> parsePAX1XSparseHeaders(final 
InputStream inputStream, final int recordSize) throws IOException {
         // for 1.X PAX Headers
@@ -626,16 +623,14 @@ static List<TarArchiveStructSparse> 
parsePAX1XSparseHeaders(final InputStream in
         long bytesRead = 0;
         long[] readResult = readLineOfNumberForPax1x(inputStream);
         // overflow while reading number?
-        long sparseHeadersCount = 
ArchiveException.requireNonNegative(readResult[0], "Corrupted TAR archive: 
Negative value in sparse headers block.");
+        long sparseHeadersCount = readResult[0];
         bytesRead += readResult[1];
         while (sparseHeadersCount-- > 0) {
             readResult = readLineOfNumberForPax1x(inputStream);
-            final long sparseOffset = 
ArchiveException.requireNonNegative(readResult[0],
-                    "Corrupted TAR archive: Sparse header block offset 
contains negative value.");
+            final long sparseOffset = readResult[0];
             bytesRead += readResult[1];
             readResult = readLineOfNumberForPax1x(inputStream);
-            final long sparseNumbytes = 
ArchiveException.requireNonNegative(readResult[0],
-                    "Corrupted TAR archive: Sparse header block numbytes 
contains negative value.");
+            final long sparseNumbytes = readResult[0];
             bytesRead += readResult[1];
             sparseHeaders.add(new TarArchiveStructSparse(sparseOffset, 
sparseNumbytes));
         }
@@ -789,9 +784,10 @@ static Map<String, String> parsePaxHeaders(final 
InputStream inputStream, final
      * @param buffer The buffer from which to parse.
      * @param offset The offset into the buffer from which to parse.
      * @return A parsed sparse struct.
+     * @throws ArchiveException Thrown if either TarArchiveStructSparse's 
{@code offset} or {@code numBytes} is negative.
      * @since 1.20
      */
-    public static TarArchiveStructSparse parseSparse(final byte[] buffer, 
final int offset) {
+    public static TarArchiveStructSparse parseSparse(final byte[] buffer, 
final int offset) throws ArchiveException {
         final long sparseOffset = parseOctalOrBinary(buffer, offset, 
TarConstants.SPARSE_OFFSET_LEN);
         final long sparseNumbytes = parseOctalOrBinary(buffer, offset + 
TarConstants.SPARSE_OFFSET_LEN, TarConstants.SPARSE_NUMBYTES_LEN);
         return new TarArchiveStructSparse(sparseOffset, sparseNumbytes);
@@ -802,7 +798,7 @@ public static TarArchiveStructSparse parseSparse(final 
byte[] buffer, final int
      * delimited by newlines.
      *
      * @param inputStream The input stream of the tar file.
-     * @return The decimal number delimited by '\n', and the bytes read from 
input stream.
+     * @return The decimal number delimited by '\n', and the bytes read from 
input stream, a long array of size 2.
      * @throws IOException if an I/O error occurs.
      */
     private static long[] readLineOfNumberForPax1x(final InputStream 
inputStream) throws IOException {
@@ -820,7 +816,9 @@ private static long[] readLineOfNumberForPax1x(final 
InputStream inputStream) th
             result = result * 10 + (number - '0');
         }
         bytesRead += 1;
-        return new long[] { result, bytesRead };
+        return new long[] {
+                ArchiveException.requireNonNegative(result, "Corrupted TAR 
archive: Sparse header block offset contains negative value."),
+                ArchiveException.requireNonNegative(bytesRead, "Corrupted TAR 
archive: Sparse header block numbytes contains negative value.") };
     }
 
     /**
diff --git 
a/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java 
b/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java
index 4506813e8..2ef512fa4 100644
--- a/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java
@@ -440,7 +440,7 @@ void testParsePAX1XSparseHeadersRejectsNonNumericOffset() 
throws Exception {
     }
 
     @Test
-    void testParseSparse() {
+    void testParseSparse() throws ArchiveException {
         final long expectedOffset = 0100000;
         final long expectedNumbytes = 0111000;
         final byte[] buffer = { ' ', ' ', ' ', ' ', ' ', '0', '1', '0', '0', 
'0', '0', '0', // sparseOffset

Reply via email to