Fix possible NPEs in some file filters when paths have no parent or no
elements.
MODIFY
src/main/java/org/apache/commons/io/IOCase.java
src/main/java/org/apache/commons/io/IOCase.java
@@ -171,13 +171,12 @@ public boolean checkEquals(final String str1, final String str2) {
* into account.
* </p>
*
- * @param str the string to check, not null
- * @param start the start to compare against, not null
- * @return true if equal using the case rules
- * @throws NullPointerException if either string is null
+ * @param str the string to check
+ * @param start the start to compare against
+ * @return true if equal using the case rules, false if either input is null
*/
public boolean checkStartsWith(final String str, final String start) {
- return str.regionMatches(!sensitive, 0, start, 0, start.length());
+ return str != null && start != null && str.regionMatches(!sensitive, 0, start, 0, start.length());
}
/**
@@ -187,12 +186,14 @@ public boolean checkStartsWith(final String str, final String start) {
* into account.
* </p>
*
- * @param str the string to check, not null
- * @param end the end to compare against, not null
- * @return true if equal using the case rules
- * @throws NullPointerException if either string is null
+ * @param str the string to check
+ * @param end the end to compare against
+ * @return true if equal using the case rules, false if either input is null
*/
public boolean checkEndsWith(final String str, final String end) {
+ if (str == null || end == null) {
+ return false;
+ }
final int endLen = end.length();
return str.regionMatches(!sensitive, str.length() - endLen, end, 0, endLen);
}

MODIFY
src/main/java/org/apache/commons/io/filefilter/NameFileFilter.java
src/main/java/org/apache/commons/io/filefilter/NameFileFilter.java
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;
+import java.util.Objects;
import org.apache.commons.io.IOCase;
@@ -187,7 +188,7 @@ public boolean accept(final File dir, final String name) {
*/
@Override
public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
- return toFileVisitResult(acceptBaseName(file.getFileName().toString()), file);
+ return toFileVisitResult(acceptBaseName(Objects.toString(file.getFileName(), null)), file);
}
private boolean acceptBaseName(final String baseName) {

MODIFY
src/main/java/org/apache/commons/io/filefilter/PrefixFileFilter.java
src/main/java/org/apache/commons/io/filefilter/PrefixFileFilter.java
@@ -168,7 +168,7 @@ public PrefixFileFilter(final String[] prefixes, final IOCase caseSensitivity) {
*/
@Override
public boolean accept(final File file) {
- return accept(file.getName());
+ return accept(file == null ? null : file.getName());
}
/**
@@ -192,7 +192,8 @@ public boolean accept(final File file, final String name) {
*/
@Override
public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
- return toFileVisitResult(accept(file.getFileName().toFile()), file);
+ final Path fileName = file.getFileName();
+ return toFileVisitResult(accept(fileName == null ? null : fileName.toFile()), file);
}
private boolean accept(final String name) {

MODIFY
src/main/java/org/apache/commons/io/filefilter/SuffixFileFilter.java
src/main/java/org/apache/commons/io/filefilter/SuffixFileFilter.java
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;
+import java.util.Objects;
import org.apache.commons.io.IOCase;
@@ -193,7 +194,7 @@ public boolean accept(final File file, final String name) {
*/
@Override
public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
- return toFileVisitResult(accept(file.getFileName().toString()), file);
+ return toFileVisitResult(accept(Objects.toString(file.getFileName(), null)), file);
}
private boolean accept(final String name) {

MODIFY
src/main/java/org/apache/commons/io/filefilter/WildcardFileFilter.java
src/main/java/org/apache/commons/io/filefilter/WildcardFileFilter.java
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;
+import java.util.Objects;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOCase;
@@ -195,7 +196,7 @@ public boolean accept(final File dir, final String name) {
*/
@Override
public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
- return toFileVisitResult(accept(file.getFileName().toString()), file);
+ return toFileVisitResult(accept(Objects.toString(file.getFileName(), null)), file);
}
private boolean accept(final String name) {

MODIFY
src/main/java/org/apache/commons/io/filefilter/WildcardFilter.java
src/main/java/org/apache/commons/io/filefilter/WildcardFilter.java
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;
+import java.util.Objects;
import org.apache.commons.io.FilenameUtils;
@@ -158,7 +159,7 @@ public FileVisitResult accept(final Path file, final BasicFileAttributes attribu
}
for (final String wildcard : wildcards) {
- if (FilenameUtils.wildcardMatch(file.getFileName().toString(), wildcard)) {
+ if (FilenameUtils.wildcardMatch(Objects.toString(file.getFileName(), null), wildcard)) {
return FileVisitResult.CONTINUE;
}
}

MODIFY
src/test/java/org/apache/commons/io/IOCaseTestCase.java
src/test/java/org/apache/commons/io/IOCaseTestCase.java
@@ -173,18 +173,9 @@ public void test_checkStartsWith_functionality() throws Exception {
assertFalse(IOCase.SENSITIVE.checkStartsWith("", "ABC"));
assertTrue(IOCase.SENSITIVE.checkStartsWith("", ""));
- try {
- IOCase.SENSITIVE.checkStartsWith("ABC", null);
- fail();
- } catch (final NullPointerException ignore) {}
- try {
- IOCase.SENSITIVE.checkStartsWith(null, "ABC");
- fail();
- } catch (final NullPointerException ignore) {}
- try {
- IOCase.SENSITIVE.checkStartsWith(null, null);
- fail();
- } catch (final NullPointerException ignore) {}
+ assertFalse(IOCase.SENSITIVE.checkStartsWith("ABC", null));
+ assertFalse(IOCase.SENSITIVE.checkStartsWith(null, "ABC"));
+ assertFalse(IOCase.SENSITIVE.checkStartsWith(null, null));
}
@Test
@@ -212,18 +203,9 @@ public void test_checkEndsWith_functionality() throws Exception {
assertFalse(IOCase.SENSITIVE.checkEndsWith("", "ABC"));
assertTrue(IOCase.SENSITIVE.checkEndsWith("", ""));
- try {
- IOCase.SENSITIVE.checkEndsWith("ABC", null);
- fail();
- } catch (final NullPointerException ignore) {}
- try {
- IOCase.SENSITIVE.checkEndsWith(null, "ABC");
- fail();
- } catch (final NullPointerException ignore) {}
- try {
- IOCase.SENSITIVE.checkEndsWith(null, null);
- fail();
- } catch (final NullPointerException ignore) {}
+ assertFalse(IOCase.SENSITIVE.checkEndsWith("ABC", null));
+ assertFalse(IOCase.SENSITIVE.checkEndsWith(null, "ABC"));
+ assertFalse(IOCase.SENSITIVE.checkEndsWith(null, null));
}
@Test

Fix possible NPEs in some file filters when paths have no parent or no
elements.