fix: prevent panic in search highlighting on multibyte text#66
Merged
Conversation
buildSnippet sliced snippets at fixed byte offsets without respecting UTF-8 rune boundaries, which could split multibyte characters (e.g. Korean) and produce invalid UTF-8. highlightMatches then lowercased that text for matching, and Go's invalid-byte-to-U+FFFD substitution could make the lowercased string longer than the original, causing highlight spans computed against it to overrun the original text and panic on slice-bounds-out-of-range. Snap snippet boundaries to rune starts, and clamp highlight spans against the actual text length before slicing.
Kairo-Kim
approved these changes
Jul 13, 2026
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.
Summary
buildSnippetsliced search snippets at fixed byte offsets (firstMatch ± 40/80) without respecting UTF-8 rune boundaries, which could split a multibyte character (e.g. Korean) in half and produce invalid UTF-8.highlightMatchesthen calledstrings.ToLoweron that invalid text; Go's invalid-byte → U+FFFD substitution can make the lowercased string longer than the original in byte length.text, so a span's end could exceedlen(text), causingpanic: runtime error: slice bounds out of range.Fix
buildSnippet: snapstart/endto the nearest rune boundary viautf8.RuneStartbefore slicing.highlightMatches: clamp each span againstlen(text)(and the running cursor) before slicing, guarding against any remaining case-folding length divergence (e.g. Turkish İ, German ẞ).Test plan
TestBuildSnippet_MultibyteBoundary— exercises snippet building around Korean text at every byte offset.TestHighlightMatches_NoPanicOnCaseFoldLengthChange— Turkish İ case-folding length change, asserts no panic.go build ./...,go vet ./...,go test ./...all pass.