src: fix UTF-8 two byte threshold in encodeInto - #65067
Open
shani-singh1 wants to merge 1 commit into
Open
Conversation
`simpleUtfEncodingLength()` returned 3 for every code point at or above U+0400, but UTF-8 encodes U+0080 through U+07FF in two bytes. The comment on `findBestFit()` a few lines below already states the correct rule. The helper is the boundary refinement step of `findBestFit()`, which decides how many code units of the input fit in the destination of `TextEncoder.prototype.encodeInto()`. Over-costing Cyrillic, Hebrew, Arabic and the other two byte blocks made it stop early, so `encodeInto()` left the destination short and under-reported `read` and `written`. The helper only ever over-estimates, so the destination is never overrun. Signed-off-by: Shani Singh <teamdeveloperworld@gmail.com>
shani-singh1
force-pushed
the
encoding-encodeinto-utf8-threshold
branch
from
August 6, 2026 11:18
9eae892 to
5ad8645
Compare
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.
simpleUtfEncodingLength()insrc/encoding_binding.ccuses the wrong boundary for the two byte range:UTF-8 encodes U+0080 through U+07FF in two bytes. The comment on
findBestFit()a few lines below already states the correct rule:So every code point from U+0400 upwards is costed as three bytes when it actually takes two. That covers Cyrillic (U+0400 to U+04FF), Hebrew, Arabic, Syriac, Thaana and NKo, among others.
Why it matters
simpleUtfEncodingLength()is the character-by-character boundary refinement step offindBestFit()(src/encoding_binding.cc:165), andfindBestFit()decides how many code units of the input fit in the destination ofTextEncoder.prototype.encodeInto()(call sites at:252,:276and:292). Its return value becomesread, andwrittenis derived from converting exactly that many code units.Over-costing makes the loop break earlier than it should, so
encodeInto()stops short of filling the destination and reportsreadandwrittenvalues that are too small. The WHATWG Encoding Standard requiresencodeInto()to write as many code points as fit.This is a correctness bug only. The helper always over-estimates and never under-estimates, so the destination is never overrun.
Measured effect
findBestFit()transcribed faithfully into JavaScript and run with both thresholds, for a string of U+0431 (Cyrillic small letter ve, two UTF-8 bytes per character):The two byte destination is the clearest case: a two byte character is currently not written at all into a buffer that has exactly enough room for it.
Two checks on that model, both against node itself:
Buffer.byteLength(s, 'utf8')for every code point in U+0000 to U+0FFF;simpleUtfEncodingLength(cp)equals the real UTF-8 length for every non-surrogate code point in the BMP, with zero mismatches.Test
test/parallel/test-text-encoder-encode-into-two-byte.jschecksencodeInto()against destinations of 2, 3, 4, 10, 40 and 100 bytes for Cyrillic, Hebrew and U+07FF, assertingread,writtenand the written bytes. It also keeps a three byte code point (U+0800) costed at three bytes.The test passes on v24.11.1, which predates
findBestFit()and does not have the defect, so the assertions describe behaviour that already held before this code was introduced.