You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bo...@apache.org on 2018/05/01 10:53:35 UTC

[1/2] commons-compress git commit: COMPRESS-450 throw specialized exception in case of a bad tar header

Repository: commons-compress
Updated Branches:
  refs/heads/COMPRESS-450 [created] 3a0ac2ca8


COMPRESS-450 throw specialized exception in case of a bad tar header


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

Branch: refs/heads/COMPRESS-450
Commit: 27ff59931984fed98c4cc3971819ad63f81d35de
Parents: ffb618d
Author: Stefan Bodewig <bo...@apache.org>
Authored: Tue May 1 12:51:36 2018 +0200
Committer: Stefan Bodewig <bo...@apache.org>
Committed: Tue May 1 12:51:36 2018 +0200

----------------------------------------------------------------------
 .../tar/InvalidTarHeaderException.java          | 34 ++++++++++++++++++++
 .../archivers/tar/TarArchiveInputStream.java    |  2 +-
 2 files changed, 35 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/27ff5993/src/main/java/org/apache/commons/compress/archivers/tar/InvalidTarHeaderException.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/InvalidTarHeaderException.java b/src/main/java/org/apache/commons/compress/archivers/tar/InvalidTarHeaderException.java
new file mode 100644
index 0000000..0edd80d
--- /dev/null
+++ b/src/main/java/org/apache/commons/compress/archivers/tar/InvalidTarHeaderException.java
@@ -0,0 +1,34 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.commons.compress.archivers.tar;
+
+import java.io.IOException;
+
+/**
+ * Specialized {@link IOException} thrown by {@link
+ * TarArchiveInputStream#getNextTarEntry} if the block read where a
+ * tar header was expected couldn't be parsed.
+ *
+ * @since 1.17
+ */
+public class InvalidTarHeaderException extends IOException {
+    private static final long serialVersionUID = 20180501L;
+    public InvalidTarHeaderException(Exception cause) {
+        super("Error detected parsing the header", cause);
+    }
+}

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/27ff5993/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
----------------------------------------------------------------------
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 daaf729..a5ade6e 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
@@ -282,7 +282,7 @@ public class TarArchiveInputStream extends ArchiveInputStream {
         try {
             currEntry = new TarArchiveEntry(headerBuf, zipEncoding);
         } catch (final IllegalArgumentException e) {
-            throw new IOException("Error detected parsing the header", e);
+            throw new InvalidTarHeaderException(e);
         }
 
         entryOffset = 0;


[2/2] commons-compress git commit: COMPRESS-450 allow tar to search for the next good tar header

Posted by bo...@apache.org.
COMPRESS-450 allow tar to search for the next good tar header


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

Branch: refs/heads/COMPRESS-450
Commit: 3a0ac2ca83436ebe59cbe91e241f6a245125f0b1
Parents: 27ff599
Author: Stefan Bodewig <bo...@apache.org>
Authored: Tue May 1 12:52:48 2018 +0200
Committer: Stefan Bodewig <bo...@apache.org>
Committed: Tue May 1 12:52:48 2018 +0200

----------------------------------------------------------------------
 .../archivers/tar/TarArchiveInputStream.java    | 27 +++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/3a0ac2ca/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
----------------------------------------------------------------------
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 a5ade6e..d5d9ca0 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
@@ -80,6 +80,9 @@ public class TarArchiveInputStream extends ArchiveInputStream {
     // the global PAX header
     private Map<String, String> globalPaxHeaders = new HashMap<>();
 
+    /** Whether the last attempt to read an ArchiveEntry failed */
+    private boolean headerErrorOccurred = false;
+
     /**
      * Constructor for TarInputStream.
      * @param is the input stream to use
@@ -257,6 +260,10 @@ public class TarArchiveInputStream extends ArchiveInputStream {
      *
      * @return The next TarEntry in the archive, or null.
      * @throws IOException on error
+     * @throws InvalidTarHeaderException if the next block cannot be
+     * parsed as a tar header. In this case it may be possible to skip
+     * some corrupted blocks and process the next valid tar header by
+     * calling this method again.
      */
     public TarArchiveEntry getNextTarEntry() throws IOException {
         if (isAtEOF()) {
@@ -269,9 +276,27 @@ public class TarArchiveInputStream extends ArchiveInputStream {
 
             /* skip to the end of the last record */
             skipRecordPadding();
+
+            /* Set currEntry to null, to make sure we don't skip again
+               if reading the next header fails */
+            currEntry = null;
+            entrySize = 0;
         }
 
-        final byte[] headerBuf = getRecord();
+        byte[] headerBuf = getRecord();
+
+        if (headerErrorOccurred) {
+            do {
+                try {
+                    if (TarUtils.verifyCheckSum(headerBuf)) {
+                        break;
+                    }
+                } catch (IllegalArgumentException e) { //NOSONAR
+                    // next record is not a valid tar header either
+                }
+                entryOffset += recordSize;
+            } while ((headerBuf = getRecord()) != null);
+        }
 
         if (headerBuf == null) {
             /* hit EOF */