Skip to content

fix(tui): preserve invalid UTF-8 bytes in word-diff tokenizing - #5023

Merged
rumpl merged 1 commit into
docker:mainfrom
TDnorthgarden:fix/wordiff-invalid-utf8
Nov 24, 2026
Merged

rumpl merged 1 commit into
docker:mainfrom
TDnorthgarden:fix/wordiff-invalid-utf8

Conversation

@TDnorthgarden

Copy link
Copy Markdown
Contributor

Problem

tokenizeForWordDiff re-encoded every decoded rune with WriteRune while promising to preserve every byte. An invalid byte decodes as a one-byte U+FFFD, which WriteRune expands to three bytes, so the token stream grew longer than the source and applyWordEmphasis's byte cursor drifted: word emphasis landed shifted by two bytes per invalid byte and could slice through a multi-byte rune, emitting invalid UTF-8 to the terminal.

Reproducer (against the real package):

$ go test ./pkg/tui/components/tool/editfile -run TestTokenizeForWordDiffPreservesInvalidUTF8Bytes                                            
expected: "a\xffb"                                                                                                                            
actual  : "ab"                                                                                                                                

This is the same class as the wrapTokens fix (#4956), on the word-diff path introduced alongside it.

Fix

  • Slice tokens from the source instead of re-encoding, so invalid bytes stay one byte and segment offsets line up with the text being styled.
  • TestTokenizeForWordDiffPreservesInvalidUTF8Bytes pins the byte-preserving contract. It fails before the fix.

tokenizeForWordDiff re-encoded every decoded rune with WriteRune while
promising to preserve every byte. An invalid byte decodes as a one-byte
U+FFFD, which WriteRune expands to three bytes, so the token stream grew
longer than the source and applyWordEmphasis's byte cursor drifted: word
emphasis landed shifted by two bytes per invalid byte and could slice
through a multi-byte rune, emitting invalid UTF-8 to the terminal. This
is the same class as the wrapTokens fix (docker#4956), on the word-diff path
introduced alongside it.

Slice tokens from the source instead of re-encoding, so invalid bytes
stay one byte and segment offsets line up with the text being styled.

Signed-off-by: Aucoto Zhang <tdnorthgarden@gmail.com>
@TDnorthgarden
TDnorthgarden requested a review from a team as a code owner November 24, 2026 02:18
@aheritier aheritier added area/tui For features/issues/fixes related to the TUI kind/fix PR fixes a bug (maps to fix:). Use on PRs only. labels Nov 24, 2026
@rumpl
rumpl enabled auto-merge November 24, 2026 06:46
@rumpl

rumpl commented Nov 24, 2026

Copy link
Copy Markdown
Member

Thanks!

@rumpl
rumpl added this pull request to the merge queue Nov 24, 2026
Merged via the queue into docker:main with commit 31fcd50 Nov 24, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/tui For features/issues/fixes related to the TUI kind/fix PR fixes a bug (maps to fix:). Use on PRs only.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants