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:
|
anonrig
approved these changes
Feb 9, 2026
jasnell
approved these changes
Feb 9, 2026
cjihrig
approved these changes
Feb 10, 2026
Member
Author
|
Manual CI: https://ci.nodejs.org/job/node-test-pull-request/71336/ (no idea why request-ci did not work here) |
Member
Author
|
@nodejs/build can someone help to see why CI is not starting? |
ShogunPanda
approved these changes
Feb 26, 2026
Collaborator
Commit Queue failed- Loading data for nodejs/node/pull/61745 β Done loading data for nodejs/node/pull/61745 ----------------------------------- PR info ------------------------------------ Title stream: fix UTF-8 character corruption in fast-utf8-stream (#61745) Author Matteo Collina <matteo.collina@gmail.com> (@mcollina) Branch mcollina:fix-utf8-stream-partial-write -> nodejs:main Labels stream, needs-ci, commit-queue-squash Commits 1 - stream: fix UTF-8 character corruption in fast-utf8-stream Committers 1 - Matteo Collina <hello@matteocollina.com> PR-URL: https://github.com/nodejs/node/pull/61745 Fixes: https://github.com/nodejs/node/issues/61744 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it> ------------------------------ Generated metadata ------------------------------ PR-URL: https://github.com/nodejs/node/pull/61745 Fixes: https://github.com/nodejs/node/issues/61744 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it> -------------------------------------------------------------------------------- βΉ This PR was created on Sun, 08 Feb 2026 20:39:59 GMT β Approvals: 4 β - Yagiz Nizipli (@anonrig) (TSC): https://github.com/nodejs/node/pull/61745#pullrequestreview-3770760306 β - James M Snell (@jasnell) (TSC): https://github.com/nodejs/node/pull/61745#pullrequestreview-3774539778 β - Colin Ihrig (@cjihrig): https://github.com/nodejs/node/pull/61745#pullrequestreview-3778727542 β - Paolo Insogna (@ShogunPanda) (TSC): https://github.com/nodejs/node/pull/61745#pullrequestreview-3859183492 β Last GitHub CI successful βΉ Last Full PR CI on 2026-02-13T22:03:07Z: https://ci.nodejs.org/job/node-test-pull-request/71336/ - Querying data for job/node-test-pull-request/71336/ β Build data downloaded β 1 failure(s) on the last Jenkins CI run -------------------------------------------------------------------------------- β Aborted `git node land` session in /home/runner/work/node/node/.ncuhttps://github.com/nodejs/node/actions/runs/22433955375 |
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