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 restrict length of sanitized entry name
Date Tue, 07 Jun 2016 16:24:27 GMT
Repository: commons-compress
Updated Branches:
  refs/heads/master 593339ab6 -> 11840dfde


COMPRESS-351 restrict length of sanitized entry name


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

Branch: refs/heads/master
Commit: 11840dfde044fec90b0cb4a715ce9d213acea3ca
Parents: 593339a
Author: Stefan Bodewig <bodewig@apache.org>
Authored: Tue Jun 7 18:23:48 2016 +0200
Committer: Stefan Bodewig <bodewig@apache.org>
Committed: Tue Jun 7 18:23:48 2016 +0200

----------------------------------------------------------------------
 .../apache/commons/compress/utils/ArchiveUtils.java  | 14 ++++++++++++--
 .../apache/commons/compress/ArchiveUtilsTest.java    | 15 +++++++++++++++
 2 files changed, 27 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/11840dfd/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 107bf0b..cd0165f 100644
--- a/src/main/java/org/apache/commons/compress/utils/ArchiveUtils.java
+++ b/src/main/java/org/apache/commons/compress/utils/ArchiveUtils.java
@@ -19,6 +19,7 @@
 package org.apache.commons.compress.utils;
 
 import java.io.UnsupportedEncodingException;
+import java.util.Arrays;
 
 import org.apache.commons.compress.archivers.ArchiveEntry;
 
@@ -27,6 +28,8 @@ import org.apache.commons.compress.archivers.ArchiveEntry;
  */
 public class ArchiveUtils {
 
+    private static final int MAX_SANITIZED_NAME_LENGTH = 255;
+
     /** Private constructor to prevent instantiation of this utility class. */
     private ArchiveUtils(){
     }
@@ -255,7 +258,8 @@ public class ArchiveUtils {
     /**
      * Returns a "sanitized" version of the string given as arguments,
      * where sanitized means non-printable characters have been
-     * replaced with a question mark.
+     * replaced with a question mark and the outcome is not longer
+     * than 255 chars.
      *
      * <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
@@ -267,7 +271,13 @@ public class ArchiveUtils {
      * @since Compress 1.12
      */
     public static String sanitize(String s) {
-        final char[] chars = s.toCharArray();
+        final char[] cs = s.toCharArray();
+        final char[] chars = cs.length <= MAX_SANITIZED_NAME_LENGTH ? cs : Arrays.copyOf(cs,
MAX_SANITIZED_NAME_LENGTH);
+        if (cs.length > MAX_SANITIZED_NAME_LENGTH) {
+            for (int i = MAX_SANITIZED_NAME_LENGTH - 3; i < MAX_SANITIZED_NAME_LENGTH;
i++) {
+                chars[i] = '.';
+            }
+        }
         final int len = chars.length;
         final StringBuilder sb = new StringBuilder();
         for (int i = 0; i < len; i++) {

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/11840dfd/src/test/java/org/apache/commons/compress/ArchiveUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/compress/ArchiveUtilsTest.java b/src/test/java/org/apache/commons/compress/ArchiveUtilsTest.java
index 413b52b..db383ed 100644
--- a/src/test/java/org/apache/commons/compress/ArchiveUtilsTest.java
+++ b/src/test/java/org/apache/commons/compress/ArchiveUtilsTest.java
@@ -67,6 +67,21 @@ public class ArchiveUtilsTest extends AbstractTestCase {
         asciiToByteAndBackFail("\u8025");
     }
 
+    @Test
+    public void sanitizeShortensString() {
+        String input = "012345678901234567890123456789012345678901234567890123456789"
+            + "012345678901234567890123456789012345678901234567890123456789"
+            + "012345678901234567890123456789012345678901234567890123456789"
+            + "012345678901234567890123456789012345678901234567890123456789"
+            + "012345678901234567890123456789012345678901234567890123456789";
+        String expected = "012345678901234567890123456789012345678901234567890123456789"
+            + "012345678901234567890123456789012345678901234567890123456789"
+            + "012345678901234567890123456789012345678901234567890123456789"
+            + "012345678901234567890123456789012345678901234567890123456789"
+            + "012345678901...";
+        assertEquals(expected, ArchiveUtils.sanitize(input));
+    }
+
     private void asciiToByteAndBackOK(final String inputString) {
         assertEquals(inputString, ArchiveUtils.toAsciiString(ArchiveUtils.toAsciiBytes(inputString)));
     }


Mime
View raw message