Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/org/apache/commons/lang3/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public int indexOf(final CharSequence str, final CharSequence searchStr, int sta
startPos = 0;
}
final int endLimit = str.length() - searchStr.length() + 1;
if (startPos > endLimit) {
if (startPos >= endLimit) {
return INDEX_NOT_FOUND;
}
if (searchStr.length() == 0) {
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/org/apache/commons/lang3/StringsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,26 @@ void testCaseInsensitiveConstant() {
assertFalse(Strings.CI.isCaseSensitive());
}

/**
* For an empty search the case-insensitive {@code indexOf} returned {@code startPos} unchanged once it reached
* {@code str.length() + 1}, so a start position one past the end yielded an index beyond the string instead of
* {@code -1}.
*/
@Test
void testCaseInsensitiveIndexOfEmptyOutOfRange() {
// repro: returned 4 (past the end of a length-3 string) before the fix
final String emptySearch = StringUtils.EMPTY;
assertEquals(-1, Strings.CI.indexOf("abc", emptySearch, 4));
// documented out-of-range example, also -1
assertEquals(-1, Strings.CI.indexOf("abc", emptySearch, 9));
// the end position is still a valid empty match
assertEquals(3, Strings.CI.indexOf("abc", emptySearch, 3));
assertEquals(2, Strings.CI.indexOf("aabaabaa", emptySearch, 2));
assertEquals(0, Strings.CI.indexOf(emptySearch, emptySearch, 0));
assertEquals(0, Strings.CI.indexOf(emptySearch, emptySearch, -1));
assertEquals(0, Strings.CI.indexOf("a", emptySearch, -1));
}

/**
* {@code U+0130} lower-cases to the two-char sequence {@code "i̇"} outside Turkish locales, so pre-lower-casing the
* search argument made the case-insensitive replace look for a two-char needle that no longer matches the single source
Expand Down
Loading