commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject commons-compress git commit: COMPRESS-351 extract method that sanitizes file names
Date Mon, 25 Apr 2016 04:31:51 GMT
Repository: commons-compress
Updated Branches:
  refs/heads/master d0501577d -> 318de9ae8


COMPRESS-351 extract method that sanitizes file names


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

Branch: refs/heads/master
Commit: 318de9ae84f1731fda8ce0785ae537f149ea5fcf
Parents: d050157
Author: Stefan Bodewig <bodewig@apache.org>
Authored: Mon Apr 25 06:31:16 2016 +0200
Committer: Stefan Bodewig <bodewig@apache.org>
Committed: Mon Apr 25 06:31:16 2016 +0200

----------------------------------------------------------------------
 .../archivers/zip/ZipArchiveInputStream.java    | 20 ++----------
 .../commons/compress/utils/ArchiveUtils.java    | 33 ++++++++++++++++++++
 2 files changed, 36 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/318de9ae/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 47668fc..54aa0ab 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
@@ -34,6 +34,7 @@ import java.util.zip.ZipException;
 import org.apache.commons.compress.archivers.ArchiveEntry;
 import org.apache.commons.compress.archivers.ArchiveInputStream;
 import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
+import org.apache.commons.compress.utils.ArchiveUtils;
 import org.apache.commons.compress.utils.IOUtils;
 
 import static org.apache.commons.compress.archivers.zip.ZipConstants.DWORD;
@@ -653,12 +654,8 @@ public class ZipArchiveInputStream extends ArchiveInputStream {
         while (remaining > 0) {
             final long n = in.read(buf.array(), 0, (int) Math.min(buf.capacity(), remaining));
             if (n < 0) {
-                String name = current.entry.getName();
-                int idx = firstUnprintableCharacter(current.entry.getName());
-                if (idx >= 0) {
-                    name = "corrupted name starting with '" + name.substring(0, idx) + "'";
-                }
-                throw new EOFException("Truncated ZIP entry: " + name);
+                throw new EOFException("Truncated ZIP entry: "
+                                       + ArchiveUtils.sanitize(current.entry.getName()));
             }
             count(n);
             remaining -= n;
@@ -988,17 +985,6 @@ public class ZipArchiveInputStream extends ArchiveInputStream {
         return b == ZipArchiveOutputStream.EOCD_SIG[0];
     }
 
-    private int firstUnprintableCharacter(String name) {
-        final char[] chars = name.toCharArray();
-        final int len = chars.length;
-        for (int i = 0; i < len; i++) {
-            if (Character.isISOControl(chars[i]) || !Character.isDefined(chars[i])) {
-                return i;
-            }
-        }
-        return -1;
-    }
-
     /**
      * Structure collecting information for the entry that is
      * currently being read.

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/318de9ae/src/main/java/org/apache/commons/compress/utils/ArchiveUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/utils/ArchiveUtils.java b/src/main/java/org/apache/commons/compress/utils/ArchiveUtils.java
index e39e4f3..107bf0b 100644
--- a/src/main/java/org/apache/commons/compress/utils/ArchiveUtils.java
+++ b/src/main/java/org/apache/commons/compress/utils/ArchiveUtils.java
@@ -251,4 +251,37 @@ public class ArchiveUtils {
         }
         return true;
     }
+
+    /**
+     * Returns a "sanitized" version of the string given as arguments,
+     * where sanitized means non-printable characters have been
+     * replaced with a question mark.
+     *
+     * <p>This method is used to clean up file names when they are
+     * used in exception messages as they may end up in log files or
+     * as console output and may have been read from a corrupted
+     * input.</p>
+     *
+     * @param s the string to sanitize
+     * @return a sanitized version of the argument
+     * @since Compress 1.12
+     */
+    public static String sanitize(String s) {
+        final char[] chars = s.toCharArray();
+        final int len = chars.length;
+        final StringBuilder sb = new StringBuilder();
+        for (int i = 0; i < len; i++) {
+            final char c = chars[i];
+            if (!Character.isISOControl(c)) {
+                Character.UnicodeBlock block = Character.UnicodeBlock.of(c);
+                if (block != null && block != Character.UnicodeBlock.SPECIALS) {
+                    sb.append(c);
+                    continue;
+                }
+            }
+            sb.append('?');
+        }
+        return sb.toString();
+    }
+
 }


Mime
View raw message