You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/12/22 02:38:14 UTC
(commons-io) 03/04: Avoid NullPointerException in IOCase.checkRegionMatches(String, int, String) on null input
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git
commit 3c13a07575642aa8ddbb6ab5c75b8da7b2f8e56b
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Dec 21 21:32:10 2023 -0500
Avoid NullPointerException in IOCase.checkRegionMatches(String, int,
String) on null input
---
src/changes/changes.xml | 1 +
src/main/java/org/apache/commons/io/IOCase.java | 7 +++----
src/test/java/org/apache/commons/io/IOCaseTest.java | 12 ++++++------
3 files changed, 10 insertions(+), 10 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 79481855..07e700e0 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -73,6 +73,7 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="fix" due-to="Gary Gregory">Avoid NullPointerException in HiddenFileFilter.accept(File) on null input.</action>
<action dev="ggregory" type="fix" due-to="Gary Gregory">Avoid NullPointerException in HiddenFileFilter.accept(Path, BasicFileAttributes) on null input.</action>
<action dev="ggregory" type="fix" due-to="Gary Gregory">Avoid NullPointerException in IOCase.checkIndexOf(String, int, String) on null input.</action>
+ <action dev="ggregory" type="fix" due-to="Gary Gregory">Avoid NullPointerException in IOCase.checkRegionMatches(String, int, String) on null input.</action>
<!-- Add -->
<action dev="ggregory" type="fix" due-to="Gary Gregory">Add and use PathUtils.getFileName(Path, Function<Path, R>).</action>
<action dev="ggregory" type="fix" due-to="Gary Gregory">Add and use PathUtils.getFileNameString().</action>
diff --git a/src/main/java/org/apache/commons/io/IOCase.java b/src/main/java/org/apache/commons/io/IOCase.java
index c13acce2..b1673669 100644
--- a/src/main/java/org/apache/commons/io/IOCase.java
+++ b/src/main/java/org/apache/commons/io/IOCase.java
@@ -208,14 +208,13 @@ public enum IOCase {
* but takes case-sensitivity into account.
* </p>
*
- * @param str the string to check, not null.
+ * @param str the string to check.
* @param strStartIndex the index to start at in str.
- * @param search the start to search for, not null.
+ * @param search the start to search for,.
* @return true if equal using the case rules.
- * @throws NullPointerException if either string is null.
*/
public boolean checkRegionMatches(final String str, final int strStartIndex, final String search) {
- return str.regionMatches(!sensitive, strStartIndex, search, 0, search.length());
+ return str != null && search != null && str.regionMatches(!sensitive, strStartIndex, search, 0, search.length());
}
/**
diff --git a/src/test/java/org/apache/commons/io/IOCaseTest.java b/src/test/java/org/apache/commons/io/IOCaseTest.java
index 1112dbe2..afd85cc6 100644
--- a/src/test/java/org/apache/commons/io/IOCaseTest.java
+++ b/src/test/java/org/apache/commons/io/IOCaseTest.java
@@ -241,12 +241,12 @@ public class IOCaseTest {
assertFalse(IOCase.SENSITIVE.checkRegionMatches("", 1, "ABC"));
assertFalse(IOCase.SENSITIVE.checkRegionMatches("", 1, ""));
- assertThrows(NullPointerException.class, () -> IOCase.SENSITIVE.checkRegionMatches("ABC", 0, null));
- assertThrows(NullPointerException.class, () -> IOCase.SENSITIVE.checkRegionMatches(null, 0, "ABC"));
- assertThrows(NullPointerException.class, () -> IOCase.SENSITIVE.checkRegionMatches(null, 0, null));
- assertThrows(NullPointerException.class, () -> IOCase.SENSITIVE.checkRegionMatches("ABC", 1, null));
- assertThrows(NullPointerException.class, () -> IOCase.SENSITIVE.checkRegionMatches(null, 1, "ABC"));
- assertThrows(NullPointerException.class, () -> IOCase.SENSITIVE.checkRegionMatches(null, 1, null));
+ assertFalse(IOCase.SENSITIVE.checkRegionMatches("ABC", 0, null));
+ assertFalse(IOCase.SENSITIVE.checkRegionMatches(null, 0, "ABC"));
+ assertFalse(IOCase.SENSITIVE.checkRegionMatches(null, 0, null));
+ assertFalse(IOCase.SENSITIVE.checkRegionMatches("ABC", 1, null));
+ assertFalse(IOCase.SENSITIVE.checkRegionMatches(null, 1, "ABC"));
+ assertFalse(IOCase.SENSITIVE.checkRegionMatches(null, 1, null));
}
@Test