commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject [1/2] commons-compress git commit: ZipArchiveInputStream.closeEntry fails to advance
Date Fri, 07 Oct 2016 16:20:56 GMT
Repository: commons-compress
Updated Branches:
  refs/heads/master f0102601d -> 9e32a2ce7


ZipArchiveInputStream.closeEntry fails to advance

Properly advances to next entry upon calling closeEntry when there
are extra junk bytes after the compressed data section of a
zip entry. This applies to the ZipArchiveInputStream implementation.


Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/658812c8
Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/658812c8
Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/658812c8

Branch: refs/heads/master
Commit: 658812c85fe762e361c998161cfb61bd477e1b24
Parents: f010260
Author: Mike Mole <michael.mole@lookout.com>
Authored: Fri Aug 19 09:08:42 2016 -0400
Committer: Stefan Bodewig <bodewig@apache.org>
Committed: Fri Oct 7 18:18:13 2016 +0200

----------------------------------------------------------------------
 .../archivers/zip/ZipArchiveInputStream.java    |  21 ++++++++++--
 .../zip/ZipArchiveInputStreamTest.java          |  34 +++++++++++++++++++
 .../resources/archive_with_bytes_after_data.zip | Bin 0 -> 670 bytes
 3 files changed, 53 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/658812c8/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
index 54aa0ab..5d4c0a8 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
@@ -616,8 +616,7 @@ public class ZipArchiveInputStream extends ArchiveInputStream {
         }
 
         // Ensure all entry bytes are read
-        if (current.bytesReadFromStream <= current.entry.getCompressedSize()
-                && !current.hasDataDescriptor) {
+        if (currentEntryHasOutstandingBytes()) {
             drainCurrentEntryData();
         } else {
             skip(Long.MAX_VALUE);
@@ -632,6 +631,12 @@ public class ZipArchiveInputStream extends ArchiveInputStream {
             // Pushback any required bytes
             if (diff > 0) {
                 pushback(buf.array(), buf.limit() - diff, diff);
+                current.bytesReadFromStream -= diff;
+            }
+
+            // Drain remainder of entry if not all data bytes were required
+            if (currentEntryHasOutstandingBytes()) {
+                drainCurrentEntryData();
             }
         }
 
@@ -646,6 +651,18 @@ public class ZipArchiveInputStream extends ArchiveInputStream {
     }
 
     /**
+     * If the compressed size of the current entry is included in the entry header
+     * and there are any outstanding bytes in the underlying stream, then
+     * this returns true.
+     *
+     * @return true, if current entry is determined to have outstanding bytes, false otherwise
+     */
+    private boolean currentEntryHasOutstandingBytes() {
+        return current.bytesReadFromStream <= current.entry.getCompressedSize()
+                && !current.hasDataDescriptor;
+    }
+
+    /**
      * Read all data of the current entry from the underlying stream
      * that hasn't been read, yet.
      */

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/658812c8/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java
b/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java
index 931387c..297de9a 100644
--- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java
@@ -200,4 +200,38 @@ public class ZipArchiveInputStreamTest {
             assertArrayEquals(expected, IOUtils.toByteArray(in));
         }
     }
+
+    /**
+     * Test case for
+     * <a href="https://issues.apache.org/jira/browse/COMPRESS-364"
+     * >COMPRESS-364</a>.
+     */
+    @Test
+    public void testWithBytesAfterData() throws Exception {
+        final int expectedNumEntries = 2;
+        final InputStream is = ZipArchiveInputStreamTest.class
+                .getResourceAsStream("/archive_with_bytes_after_data.zip");
+        final ZipArchiveInputStream zip = new ZipArchiveInputStream(is);
+
+        try {
+            int actualNumEntries = 0;
+            ZipArchiveEntry zae = zip.getNextZipEntry();
+            while (zae != null) {
+                actualNumEntries++;
+                readEntry(zip, zae);
+                zae = zip.getNextZipEntry();
+            }
+            assertEquals(expectedNumEntries, actualNumEntries);
+        } finally {
+            zip.close();
+        }
+    }
+
+    private static byte[] readEntry(ZipArchiveInputStream zip, ZipArchiveEntry zae) throws
IOException {
+        final int len = (int)zae.getSize();
+        final byte[] buff = new byte[len];
+        zip.read(buff, 0, len);
+
+        return buff;
+    }
 }

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/658812c8/src/test/resources/archive_with_bytes_after_data.zip
----------------------------------------------------------------------
diff --git a/src/test/resources/archive_with_bytes_after_data.zip b/src/test/resources/archive_with_bytes_after_data.zip
new file mode 100644
index 0000000..8937ede
Binary files /dev/null and b/src/test/resources/archive_with_bytes_after_data.zip differ


Mime
View raw message