Author: krosenvold Date: Thu Aug 6 12:22:42 2015 New Revision: 1694480 URL: http://svn.apache.org/r1694480 Log: IO-484 Added filtering to getPath and getPrefix
Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/FilenameUtils.java commons/proper/io/trunk/src/test/java/org/apache/commons/io/FilenameUtilsTestCase.java Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/FilenameUtils.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/FilenameUtils.java?rev=1694480&r1=1694479&r2=1694480&view=diff ============================================================================== --- commons/proper/io/trunk/src/main/java/org/apache/commons/io/FilenameUtils.java (original) +++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/FilenameUtils.java Thu Aug 6 12:22:42 2015 @@ -757,7 +757,7 @@ public class FilenameUtils { * ie. both Unix and Windows prefixes are matched regardless. * * @param filename the filename to query, null returns null - * @return the prefix of the file, null if invalid + * @return the prefix of the file, null if invalid. Null bytes inside string will be removed */ public static String getPrefix(final String filename) { if (filename == null) { @@ -768,9 +768,9 @@ public class FilenameUtils { return null; } if (len > filename.length()) { - return filename + UNIX_SEPARATOR; // we know this only happens for unix + return filterNullBytes(filename + UNIX_SEPARATOR); // we know this only happens for unix } - return filename.substring(0, len); + return filterNullBytes(filename.substring(0, len)); } /** @@ -793,7 +793,8 @@ public class FilenameUtils { * See {@link #getFullPath(String)} for the method that retains the prefix. * * @param filename the filename to query, null returns null - * @return the path of the file, an empty string if none exists, null if invalid + * @return the path of the file, an empty string if none exists, null if invalid. + * Null bytes inside string will be removed */ public static String getPath(final String filename) { return doGetPath(filename, 1); @@ -820,7 +821,8 @@ public class FilenameUtils { * See {@link #getFullPathNoEndSeparator(String)} for the method that retains the prefix. * * @param filename the filename to query, null returns null - * @return the path of the file, an empty string if none exists, null if invalid + * @return the path of the file, an empty string if none exists, null if invalid. + * Null bytes inside string will be removed */ public static String getPathNoEndSeparator(final String filename) { return doGetPath(filename, 0); @@ -831,7 +833,7 @@ public class FilenameUtils { * * @param filename the filename * @param separatorAdd 0 to omit the end separator, 1 to return it - * @return the path + * @return the path. Null bytes inside string will be removed */ private static String doGetPath(final String filename, final int separatorAdd) { if (filename == null) { @@ -846,7 +848,7 @@ public class FilenameUtils { if (prefix >= filename.length() || index < 0 || prefix >= endIndex) { return ""; } - return filename.substring(prefix, endIndex); + return filterNullBytes(filename.substring(prefix, endIndex)); } /** Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/FilenameUtilsTestCase.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/FilenameUtilsTestCase.java?rev=1694480&r1=1694479&r2=1694480&view=diff ============================================================================== --- commons/proper/io/trunk/src/test/java/org/apache/commons/io/FilenameUtilsTestCase.java (original) +++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/FilenameUtilsTestCase.java Thu Aug 6 12:22:42 2015 @@ -565,6 +565,7 @@ public class FilenameUtilsTestCase exten assertEquals("\\", FilenameUtils.getPrefix("\\a\\b\\c.txt")); assertEquals("~\\", FilenameUtils.getPrefix("~\\a\\b\\c.txt")); assertEquals("~user\\", FilenameUtils.getPrefix("~user\\a\\b\\c.txt")); + assertEquals("~user\\", FilenameUtils.getPrefix("~u\u0000ser\\a\\b\\c.txt")); } public void testGetPath() { @@ -601,6 +602,7 @@ public class FilenameUtilsTestCase exten assertEquals("a/b/", FilenameUtils.getPath("//server/a/b/c.txt")); assertEquals("a/b/", FilenameUtils.getPath("~/a/b/c.txt")); assertEquals("a/b/", FilenameUtils.getPath("~user/a/b/c.txt")); + assertEquals("a/b/", FilenameUtils.getPath("~user/a/\u0000b/c.txt")); } public void testGetPathNoEndSeparator() { @@ -637,6 +639,7 @@ public class FilenameUtilsTestCase exten assertEquals("a/b", FilenameUtils.getPathNoEndSeparator("//server/a/b/c.txt")); assertEquals("a/b", FilenameUtils.getPathNoEndSeparator("~/a/b/c.txt")); assertEquals("a/b", FilenameUtils.getPathNoEndSeparator("~user/a/b/c.txt")); + assertEquals("a/b", FilenameUtils.getPathNoEndSeparator("~user/a\u0000/b/c.txt")); } public void testGetFullPath() {