You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by pe...@apache.org on 2021/01/09 03:20:38 UTC

[commons-compress] 05/13: COMPRESS-540: Include fix for COMPRESS-554

This is an automated email from the ASF dual-hosted git repository.

peterlee pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git

commit 3b7f0aedf936792903bd721edb59418b057091bc
Author: theobisproject <th...@gmail.com>
AuthorDate: Thu Sep 3 19:05:11 2020 +0200

    COMPRESS-540: Include fix for COMPRESS-554
    
    This revealed the missing reset of the current entry if the header is not present and the usage of the wrong entry in the stream.
---
 .../java/org/apache/commons/compress/archivers/tar/TarFile.java   | 8 +++++++-
 .../org/apache/commons/compress/archivers/tar/TarFileTest.java    | 6 ++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarFile.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarFile.java
index 41e3db8..d8e517d 100644
--- a/src/main/java/org/apache/commons/compress/archivers/tar/TarFile.java
+++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarFile.java
@@ -241,6 +241,8 @@ public class TarFile implements Closeable {
 
         final ByteBuffer headerBuf = getRecord();
         if (null == headerBuf) {
+            /* hit EOF */
+            currEntry = null;
             return null;
         }
 
@@ -457,6 +459,10 @@ public class TarFile implements Closeable {
             globalPaxHeaders = TarUtils.parsePaxHeaders(input, globalSparseHeaders, globalPaxHeaders);
         }
         getNextTarEntry(); // Get the actual file entry
+
+        if (currEntry == null) {
+            throw new IOException("Error detected parsing the pax header");
+        }
     }
 
     /**
@@ -641,7 +647,7 @@ public class TarFile implements Closeable {
         protected int read(final long pos, final ByteBuffer buf) throws IOException {
             if (entry.isSparse()) {
                 // for sparse entries, there are actually currEntry.getRealSize() bytes to read
-                if (entryOffset >= currEntry.getRealSize()) {
+                if (entryOffset >= entry.getRealSize()) {
                     return -1;
                 }
             } else {
diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/TarFileTest.java b/src/test/java/org/apache/commons/compress/archivers/tar/TarFileTest.java
index 778d959..feccaf1 100644
--- a/src/test/java/org/apache/commons/compress/archivers/tar/TarFileTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/tar/TarFileTest.java
@@ -96,4 +96,10 @@ public class TarFileTest extends AbstractTestCase {
         }
     }
 
+    @Test(expected = IOException.class)
+    public void testThrowExceptionWithNullEntry() throws IOException {
+        try (TarFile tarFile = new TarFile(getPath("COMPRESS-554.tar"))) {
+        }
+    }
+
 }