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 <[email protected]> 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"))) { + } + } + }
