fix: clamp Substring negative offset by rune length, not byte length#909
Open
DucMinhNe wants to merge 1 commit into
Open
fix: clamp Substring negative offset by rune length, not byte length#909DucMinhNe wants to merge 1 commit into
DucMinhNe wants to merge 1 commit into
Conversation
substring() compared a negative offset against len(str), the byte length,
while Substring has rune/character semantics. For multibyte strings the
byte length exceeds the rune length, so offsets in the band
-byteLen < offset <= -runeLen skipped the clamp-to-start branch and fell
through to the count-from-end path, overshooting the front and returning a
wrong or empty result (e.g. Substring("héllo", -6, 2) returned "h" instead
of "hé"). Compare against utf8.RuneCountInString instead.
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.
What
Substringis documented and tested with rune (character) semantics, but the internalsubstringhelper compares a negative offset againstlen(str), which is the byte length:For an ASCII string byte length and rune length are equal, so this is fine. For a multibyte string the byte length is larger, so offsets in the band
-byteLen < offset <= -runeLenfail this case, fall through to the count-from-the-end branch, walk-offsetrunes backwards, overshoot the start of the string and return a wrong or empty result.The existing test
Substring("hello", -10, 2) == "he"already pins the intended behaviour: a negative offset more negative than the length clamps to the start and takeslengthrunes. Multibyte strings just were not covered.Repro
Fix
Compare against
utf8.RuneCountInString(string(str))instead oflen(str).utf8is already imported and thefallthroughfrom the positive-offset case is unaffected (a fallthrough ignores the case expression).Added four assertions to
TestSubstringcovering the multibyte negative-offset band.go test ./...is green,gofmt/go vetclean.