stream: fix UTF-8 character corruption in fast-utf8-stream#61745
Open
mcollina wants to merge 1 commit intonodejs:mainfrom
Open
stream: fix UTF-8 character corruption in fast-utf8-stream#61745mcollina wants to merge 1 commit intonodejs:mainfrom
mcollina wants to merge 1 commit intonodejs:mainfrom
Conversation
Fix releaseWritingBuf() to correctly handle partial writes that split multi-byte UTF-8 characters. The previous implementation incorrectly converted byte counts to character counts, causing: - 3-byte characters (CJK) to be silently dropped - 4-byte characters (emoji) to leave lone surrogates in the buffer The fix backs up from the byte position to find a valid UTF-8 character boundary by checking for continuation bytes (pattern 10xxxxxx), then decodes the properly-aligned bytes to get the correct character count. Also fixes a typo where this._asyncDrainScheduled was used instead of the private field this.#asyncDrainScheduled. Fixes: nodejs#61744
Collaborator
|
Review requested:
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #61745 +/- ##
==========================================
- Coverage 89.74% 89.74% -0.01%
==========================================
Files 674 675 +1
Lines 204360 204537 +177
Branches 39265 39308 +43
==========================================
+ Hits 183412 183569 +157
- Misses 13251 13257 +6
- Partials 7697 7711 +14
🚀 New features to boost your workflow:
|
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
Fix
releaseWritingBuf()to correctly handle partial writes that split multi-byte UTF-8 characters.The previous implementation incorrectly converted byte counts to character counts by using:
When
nbytes cuts through a multi-byte character, the incomplete UTF-8 sequence becomes U+FFFD (replacement character) via.toString(), which has a different.lengththan the original character. This caused:The fix backs up from the byte position to find a valid UTF-8 character boundary by checking for continuation bytes (pattern
10xxxxxx), then decodes the properly-aligned bytes to get the correct character count.Also fixes a typo where
this._asyncDrainScheduledwas used instead of the private fieldthis.#asyncDrainScheduled.Fixes: #61744