fix(compile): use rune slice to truncate history summary — avoids UTF-8 boundary corruption#75
Open
mpeter wants to merge 1 commit into
Open
Conversation
Byte-slicing a string at a fixed offset (summary[:80]) corrupts multibyte UTF-8 characters that span the boundary. For example, an em-dash (U+2014, encoded as 0xE2 0x80 0x94) sliced at byte 79 or 80 produces an invalid sequence 0xE2 0x80 followed by the first byte of '...' (0x2E), yielding 0xE2 0x80 0x2E — invalid UTF-8. Fix: convert to []rune before slicing, then convert back to string. This preserves character boundaries regardless of multibyte encoding. Reproducer: a learning whose information field contains an em-dash at or near byte offset 80 in UTF-8 encoding. Tested: go test ./tools/... passes (438 tests), recompile of process-git tag produces clean UTF-8 output.
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.
Root cause
buildCompiledArticleintools/compile.gotruncates learning content for the history table using a byte slice:In Go,
string[i:j]slices by bytes, not characters. When a multibyte UTF-8 character (e.g. em-dash U+2014, encoded as0xE2 0x80 0x94) spans the byte-80 boundary, the slice cuts the character mid-encoding. Appending"..."then places0x2E(period) as the third byte of the incomplete sequence, producing0xE2 0x80 0x2E— invalid UTF-8.Fix
Convert to
[]runebefore slicing, then back tostring:Reproducer
Any learning whose
informationfield contains a multibyte character (em-dash, curly quote, arrows, CJK, etc.) where the UTF-8 encoding spans byte offset 80. Observed in production on theprocess-gitcompiled article — every compile produced0xE2 0x80 0x2EcausingUnicodeDecodeErrorin Python consumers of the article.Testing