Skip to content

Commit 3c13a07

Browse files
committed
Avoid NullPointerException in IOCase.checkRegionMatches(String, int,
String) on null input
1 parent b8fdf4e commit 3c13a07

3 files changed

Lines changed: 10 additions & 10 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ The <action> type attribute can be add,update,fix,remove.
7373
<action dev="ggregory" type="fix" due-to="Gary Gregory">Avoid NullPointerException in HiddenFileFilter.accept(File) on null input.</action>
7474
<action dev="ggregory" type="fix" due-to="Gary Gregory">Avoid NullPointerException in HiddenFileFilter.accept(Path, BasicFileAttributes) on null input.</action>
7575
<action dev="ggregory" type="fix" due-to="Gary Gregory">Avoid NullPointerException in IOCase.checkIndexOf(String, int, String) on null input.</action>
76+
<action dev="ggregory" type="fix" due-to="Gary Gregory">Avoid NullPointerException in IOCase.checkRegionMatches(String, int, String) on null input.</action>
7677
<!-- Add -->
7778
<action dev="ggregory" type="fix" due-to="Gary Gregory">Add and use PathUtils.getFileName(Path, Function&lt;Path, R&gt;).</action>
7879
<action dev="ggregory" type="fix" due-to="Gary Gregory">Add and use PathUtils.getFileNameString().</action>

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,13 @@ public int checkIndexOf(final String str, final int strStartIndex, final String
208208
* but takes case-sensitivity into account.
209209
* </p>
210210
*
211-
* @param str the string to check, not null.
211+
* @param str the string to check.
212212
* @param strStartIndex the index to start at in str.
213-
* @param search the start to search for, not null.
213+
* @param search the start to search for,.
214214
* @return true if equal using the case rules.
215-
* @throws NullPointerException if either string is null.
216215
*/
217216
public boolean checkRegionMatches(final String str, final int strStartIndex, final String search) {
218-
return str.regionMatches(!sensitive, strStartIndex, search, 0, search.length());
217+
return str != null && search != null && str.regionMatches(!sensitive, strStartIndex, search, 0, search.length());
219218
}
220219

221220
/**

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,12 @@ public void test_checkRegionMatches_functionality() {
241241
assertFalse(IOCase.SENSITIVE.checkRegionMatches("", 1, "ABC"));
242242
assertFalse(IOCase.SENSITIVE.checkRegionMatches("", 1, ""));
243243

244-
assertThrows(NullPointerException.class, () -> IOCase.SENSITIVE.checkRegionMatches("ABC", 0, null));
245-
assertThrows(NullPointerException.class, () -> IOCase.SENSITIVE.checkRegionMatches(null, 0, "ABC"));
246-
assertThrows(NullPointerException.class, () -> IOCase.SENSITIVE.checkRegionMatches(null, 0, null));
247-
assertThrows(NullPointerException.class, () -> IOCase.SENSITIVE.checkRegionMatches("ABC", 1, null));
248-
assertThrows(NullPointerException.class, () -> IOCase.SENSITIVE.checkRegionMatches(null, 1, "ABC"));
249-
assertThrows(NullPointerException.class, () -> IOCase.SENSITIVE.checkRegionMatches(null, 1, null));
244+
assertFalse(IOCase.SENSITIVE.checkRegionMatches("ABC", 0, null));
245+
assertFalse(IOCase.SENSITIVE.checkRegionMatches(null, 0, "ABC"));
246+
assertFalse(IOCase.SENSITIVE.checkRegionMatches(null, 0, null));
247+
assertFalse(IOCase.SENSITIVE.checkRegionMatches("ABC", 1, null));
248+
assertFalse(IOCase.SENSITIVE.checkRegionMatches(null, 1, "ABC"));
249+
assertFalse(IOCase.SENSITIVE.checkRegionMatches(null, 1, null));
250250
}
251251

252252
@Test

0 commit comments

Comments
 (0)