Repository navigation
Conversation
drainChars computed the number of characters to move and passed that count to getChars and delete, which both take an end index. The two only agree when startIndex is 0, so any other start index drained the wrong range, reported a count it had not copied, or threw StringIndexOutOfBoundsException for a valid range. Clamp against the characters after startIndex and pass startIndex + actualLen as the end index. Add a regression test.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TextStringBuilder.drainChars(startIndex, endIndex, target, targetIndex) works out how many characters to move and then hands that count to getChars and delete, both of which take an end index rather than a length. The two agree only when startIndex is 0, which is the only shape the existing test tries. With any other start index the method either moves nothing while still reporting a count (new TextStringBuilder("junit").drainChars(2, 4, target, 0) returns 2, but target is untouched and the builder still holds "junit"), moves only part of the range, or throws StringIndexOutOfBoundsException("end < start") for a perfectly valid range such as drainChars(3, 5, target, 0). A caller that trusts the returned count reads whatever was already sitting in the target array as if it were drained text, while the characters it meant to consume stay in the builder. The cap against the builder size has the same slip, since it compares the length with size rather than with the characters remaining after startIndex.
The fix clamps the length against size - startIndex and passes startIndex + actualLen as the end index to getChars and delete, so the startIndex == 0 behaviour and the existing out-of-range exceptions are unchanged. StringSubstitutorReader, the only in-tree caller, always drains from index 0 and is not affected. TextStringBuilderTest#testDrainCharsIntIntCharArrayIntNonZeroStartIndex fails without the main change and passes with it, and the default mvn build is green (2044 tests).
mvn; that'smvnon the command line by itself.