Skip to content

Commit 76e45d6

Browse files
committed
IO-484 Added filtering to getPath and getPrefix
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1694480 13f79535-47bb-0310-9956-ffa450edef68
1 parent afe78a0 commit 76e45d6

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

src/main/java/org/apache/commons/io/FilenameUtils.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ public static int indexOfExtension(final String filename) {
757757
* ie. both Unix and Windows prefixes are matched regardless.
758758
*
759759
* @param filename the filename to query, null returns null
760-
* @return the prefix of the file, null if invalid
760+
* @return the prefix of the file, null if invalid. Null bytes inside string will be removed
761761
*/
762762
public static String getPrefix(final String filename) {
763763
if (filename == null) {
@@ -768,9 +768,9 @@ public static String getPrefix(final String filename) {
768768
return null;
769769
}
770770
if (len > filename.length()) {
771-
return filename + UNIX_SEPARATOR; // we know this only happens for unix
771+
return filterNullBytes(filename + UNIX_SEPARATOR); // we know this only happens for unix
772772
}
773-
return filename.substring(0, len);
773+
return filterNullBytes(filename.substring(0, len));
774774
}
775775

776776
/**
@@ -793,7 +793,8 @@ public static String getPrefix(final String filename) {
793793
* See {@link #getFullPath(String)} for the method that retains the prefix.
794794
*
795795
* @param filename the filename to query, null returns null
796-
* @return the path of the file, an empty string if none exists, null if invalid
796+
* @return the path of the file, an empty string if none exists, null if invalid.
797+
* Null bytes inside string will be removed
797798
*/
798799
public static String getPath(final String filename) {
799800
return doGetPath(filename, 1);
@@ -820,7 +821,8 @@ public static String getPath(final String filename) {
820821
* See {@link #getFullPathNoEndSeparator(String)} for the method that retains the prefix.
821822
*
822823
* @param filename the filename to query, null returns null
823-
* @return the path of the file, an empty string if none exists, null if invalid
824+
* @return the path of the file, an empty string if none exists, null if invalid.
825+
* Null bytes inside string will be removed
824826
*/
825827
public static String getPathNoEndSeparator(final String filename) {
826828
return doGetPath(filename, 0);
@@ -831,7 +833,7 @@ public static String getPathNoEndSeparator(final String filename) {
831833
*
832834
* @param filename the filename
833835
* @param separatorAdd 0 to omit the end separator, 1 to return it
834-
* @return the path
836+
* @return the path. Null bytes inside string will be removed
835837
*/
836838
private static String doGetPath(final String filename, final int separatorAdd) {
837839
if (filename == null) {
@@ -846,7 +848,7 @@ private static String doGetPath(final String filename, final int separatorAdd) {
846848
if (prefix >= filename.length() || index < 0 || prefix >= endIndex) {
847849
return "";
848850
}
849-
return filename.substring(prefix, endIndex);
851+
return filterNullBytes(filename.substring(prefix, endIndex));
850852
}
851853

852854
/**

src/test/java/org/apache/commons/io/FilenameUtilsTestCase.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ public void testGetPrefix() {
565565
assertEquals("\\", FilenameUtils.getPrefix("\\a\\b\\c.txt"));
566566
assertEquals("~\\", FilenameUtils.getPrefix("~\\a\\b\\c.txt"));
567567
assertEquals("~user\\", FilenameUtils.getPrefix("~user\\a\\b\\c.txt"));
568+
assertEquals("~user\\", FilenameUtils.getPrefix("~u\u0000ser\\a\\b\\c.txt"));
568569
}
569570

570571
public void testGetPath() {
@@ -601,6 +602,7 @@ public void testGetPath() {
601602
assertEquals("a/b/", FilenameUtils.getPath("//server/a/b/c.txt"));
602603
assertEquals("a/b/", FilenameUtils.getPath("~/a/b/c.txt"));
603604
assertEquals("a/b/", FilenameUtils.getPath("~user/a/b/c.txt"));
605+
assertEquals("a/b/", FilenameUtils.getPath("~user/a/\u0000b/c.txt"));
604606
}
605607

606608
public void testGetPathNoEndSeparator() {
@@ -637,6 +639,7 @@ public void testGetPathNoEndSeparator() {
637639
assertEquals("a/b", FilenameUtils.getPathNoEndSeparator("//server/a/b/c.txt"));
638640
assertEquals("a/b", FilenameUtils.getPathNoEndSeparator("~/a/b/c.txt"));
639641
assertEquals("a/b", FilenameUtils.getPathNoEndSeparator("~user/a/b/c.txt"));
642+
assertEquals("a/b", FilenameUtils.getPathNoEndSeparator("~user/a\u0000/b/c.txt"));
640643
}
641644

642645
public void testGetFullPath() {

0 commit comments

Comments
 (0)