Author: bodewig Date: Sun Apr 27 09:15:33 2014 New Revision: 1590361 URL: http://svn.apache.org/r1590361 Log: COMPRESS-279 detect a truncated tar archive and throw an exception
Added: commons/proper/compress/trunk/src/test/resources/COMPRESS-279.tar (with props) Modified: commons/proper/compress/trunk/src/changes/changes.xml commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java Modified: commons/proper/compress/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/changes/changes.xml?rev=1590361&r1=1590360&r2=1590361&view=diff ============================================================================== --- commons/proper/compress/trunk/src/changes/changes.xml (original) +++ commons/proper/compress/trunk/src/changes/changes.xml Sun Apr 27 09:15:33 2014 @@ -78,6 +78,11 @@ The <action> type attribute can be add,u TarArchiveInputStream failed to read archives with empty gid/uid fields. </action> + <action type="fix" date="2014-04-27" issue="COMPRESS-279"> + TarArchiveInputStream now again throws an exception when it + encounters a truncated archive while reading from the last + entry. + </action> </release> <release version="1.8" date="2014-03-12" description="Release 1.8"> Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java?rev=1590361&r1=1590360&r2=1590361&view=diff ============================================================================== --- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java (original) +++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java Sun Apr 27 09:15:33 2014 @@ -580,11 +580,14 @@ public class TarArchiveInputStream exten numToRead = Math.min(numToRead, available()); totalRead = is.read(buf, offset, numToRead); - count(totalRead); if (totalRead == -1) { + if (numToRead > 0) { + throw new IOException("Truncated TAR archive"); + } hasHitEOF = true; } else { + count(totalRead); entryOffset += totalRead; } Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java?rev=1590361&r1=1590360&r2=1590361&view=diff ============================================================================== --- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java (original) +++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java Sun Apr 27 09:15:33 2014 @@ -19,6 +19,8 @@ package org.apache.commons.compress.archivers.tar; import static org.apache.commons.compress.AbstractTestCase.getFile; +import static org.apache.commons.compress.AbstractTestCase.mkdir; +import static org.apache.commons.compress.AbstractTestCase.rmdir; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -26,7 +28,9 @@ import static org.junit.Assert.fail; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Calendar; @@ -36,6 +40,7 @@ import java.util.TimeZone; import java.util.zip.GZIPInputStream; import org.apache.commons.compress.utils.CharsetNames; +import org.apache.commons.compress.utils.IOUtils; import org.junit.Test; public class TarArchiveInputStreamTest { @@ -204,6 +209,29 @@ public class TarArchiveInputStreamTest { } } + @Test(expected = IOException.class) + public void shouldThrowAnExceptionOnTruncatedEntries() throws Exception { + File dir = mkdir("COMPRESS-279"); + TarArchiveInputStream is = getTestStream("/COMPRESS-279.tar"); + FileOutputStream out = null; + try { + TarArchiveEntry entry = is.getNextTarEntry(); + int count = 0; + while (entry != null) { + out = new FileOutputStream(new File(dir, String.valueOf(count))); + IOUtils.copy(is, out); + out.close(); + out = null; + count++; + entry = is.getNextTarEntry(); + } + } finally { + is.close(); + if (out != null) { + out.close(); + } + } + } private TarArchiveInputStream getTestStream(String name) { return new TarArchiveInputStream( Added: commons/proper/compress/trunk/src/test/resources/COMPRESS-279.tar URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/resources/COMPRESS-279.tar?rev=1590361&view=auto ============================================================================== Binary file - no diff available. Propchange: commons/proper/compress/trunk/src/test/resources/COMPRESS-279.tar ------------------------------------------------------------------------------ svn:mime-type = application/octet-stream