COMPRESS-279 detect a truncated tar archive and throw an exception

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/compress/trunk@1590361 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index eb24ae7..80a705f 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -78,6 +78,11 @@
         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">
diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
index fe2c113..6f0c6a4 100644
--- a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
@@ -580,11 +580,14 @@
         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;
         }
 
diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java
index 0de9bef..d8734a6 100644
--- a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java
@@ -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 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.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 @@
         }
     }
 
+    @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(
diff --git a/src/test/resources/COMPRESS-279.tar b/src/test/resources/COMPRESS-279.tar
new file mode 100644
index 0000000..eeb0932
--- /dev/null
+++ b/src/test/resources/COMPRESS-279.tar
Binary files differ