Skip to content

Commit bcf8fbb

Browse files
committed
Avoid NullPointerException in IOCase.checkIndexOf(String, int, String)
on null input
1 parent 87ef955 commit bcf8fbb

3 files changed

Lines changed: 14 additions & 9 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ The <action> type attribute can be add,update,fix,remove.
7272
<action dev="ggregory" type="fix" due-to="Gary Gregory">Avoid NullPointerException in FileFileFilter.accept(Path, BasicFileAttributes) on null input.</action>
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>
75+
<action dev="ggregory" type="fix" due-to="Gary Gregory">Avoid NullPointerException in IOCase.checkIndexOf(String, int, String) on null input.</action>
7576
<!-- Add -->
7677
<action dev="ggregory" type="fix" due-to="Gary Gregory">Add and use PathUtils.getFileName(Path, Function&lt;Path, R&gt;).</action>
7778
<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: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,16 @@ public boolean checkEquals(final String str1, final String str2) {
185185
* @param search the start to search for, not null
186186
* @return the first index of the search String,
187187
* -1 if no match or {@code null} string input
188-
* @throws NullPointerException if either string is null
189188
* @since 2.0
190189
*/
191190
public int checkIndexOf(final String str, final int strStartIndex, final String search) {
192-
final int endIndex = str.length() - search.length();
193-
if (endIndex >= strStartIndex) {
194-
for (int i = strStartIndex; i <= endIndex; i++) {
195-
if (checkRegionMatches(str, i, search)) {
196-
return i;
191+
if (str != null && search != null) {
192+
final int endIndex = str.length() - search.length();
193+
if (endIndex >= strStartIndex) {
194+
for (int i = strStartIndex; i <= endIndex; i++) {
195+
if (checkRegionMatches(str, i, search)) {
196+
return i;
197+
}
197198
}
198199
}
199200
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ public void test_checkEquals_functionality() {
152152
public void test_checkIndexOf_case() {
153153
assertEquals(1, IOCase.SENSITIVE.checkIndexOf("ABC", 0, "BC"));
154154
assertEquals(-1, IOCase.SENSITIVE.checkIndexOf("ABC", 0, "Bc"));
155+
assertEquals(-1, IOCase.SENSITIVE.checkIndexOf(null, 0, "Bc"));
156+
assertEquals(-1, IOCase.SENSITIVE.checkIndexOf(null, 0, null));
157+
assertEquals(-1, IOCase.SENSITIVE.checkIndexOf("ABC", 0, null));
155158

156159
assertEquals(1, IOCase.INSENSITIVE.checkIndexOf("ABC", 0, "BC"));
157160
assertEquals(1, IOCase.INSENSITIVE.checkIndexOf("ABC", 0, "Bc"));
@@ -199,9 +202,9 @@ public void test_checkIndexOf_functionality() {
199202
// too long
200203
assertEquals(-1, IOCase.SENSITIVE.checkIndexOf("DEF", 0, "ABCDEFGHIJ"));
201204

202-
assertThrows(NullPointerException.class, () -> IOCase.SENSITIVE.checkIndexOf("ABC", 0, null));
203-
assertThrows(NullPointerException.class, () -> IOCase.SENSITIVE.checkIndexOf(null, 0, "ABC"));
204-
assertThrows(NullPointerException.class, () -> IOCase.SENSITIVE.checkIndexOf(null, 0, null));
205+
assertEquals(-1, IOCase.SENSITIVE.checkIndexOf("ABC", 0, null));
206+
assertEquals(-1, IOCase.SENSITIVE.checkIndexOf(null, 0, "ABC"));
207+
assertEquals(-1, IOCase.SENSITIVE.checkIndexOf(null, 0, null));
205208
}
206209

207210
@Test

0 commit comments

Comments
 (0)