Skip to content

util: fix TextEncoder.encodeInto underfilling - #65997

Open
XadillaX wants to merge 1 commit into
nodejs:mainfrom
XadillaX:fix-textencoder-encodeinto-underfill
Open

util: fix TextEncoder.encodeInto underfilling#65997
XadillaX wants to merge 1 commit into
nodejs:mainfrom
XadillaX:fix-textencoder-encodeinto-underfill

Conversation

@XadillaX

Copy link
Copy Markdown
Contributor

TextEncoder.encodeInto() could underfill narrow destination buffers on the optimized path because:

  • UTF-16 code units below U+0800 were incorrectly treated as three-byte UTF-8 sequences starting at U+0400.
  • Latin-1 input was exposed as signed char while calculating the scalar tail.
  • Surrogate pairs in the scalar tail were adjusted only after sizing, which could leave usable destination space unfilled.

Keep one-byte input unsigned, use the correct UTF-8 boundary, and account for surrogate pairs atomically. Use replacement-aware UTF-16 sizing and validate during conversion, avoiding a separate validation pass for valid input. A TODO records the preferred future simdutf bounded-conversion API that would return both consumed and written counts.

Fixes: #65994

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Sep 12, 2026
@XadillaX
XadillaX force-pushed the fix-textencoder-encodeinto-underfill branch from 8afb2c6 to 6417dc3 Compare September 12, 2026 06:30
Correct the two-byte UTF-8 boundary and keep Latin-1 input unsigned
while finding the prefix that fits. Handle surrogate pairs atomically
in the scalar tail.

Use replacement-aware UTF-16 sizing and validate during conversion so
valid input avoids a separate validation pass.

Fixes: nodejs#65994
Signed-off-by: XadillaX <i@2333.moe>
@XadillaX
XadillaX force-pushed the fix-textencoder-encodeinto-underfill branch from 6417dc3 to fe02f30 Compare September 12, 2026 06:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TextEncoder.encodeInto() underfills the destination for some non-ASCII text

2 participants