fix: drop partial multibyte char when write length is too small#374
Open
spokodev wants to merge 1 commit into
Open
fix: drop partial multibyte char when write length is too small#374spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
Buffer#write(string, offset, length, encoding) with an explicit length smaller than the next character's byte length wrote a partial, corrupt multibyte sequence and over-reported the byte count. utf8Write/ucs2Write passed buf.length - offset (the whole remaining buffer) as the byte cap to utf8ToBytes/utf16leToBytes, so a full multibyte char was always generated and then byte-truncated mid-sequence by blitBuffer. Node guarantees partially encoded characters are never written and counts only complete characters in the return value. Pass the effective length as the cap so an incomplete trailing character is dropped, matching node Buffer.
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.
Problem
Buffer#write(string, offset, length, encoding)with an explicitlengthsmaller than the next character's byte length writes a partial, corrupt multibyte sequence and over-reports the byte count.The Node docs state: "If
bufdid not contain enough space to fit the entire string, only part ofstringwill be written. However, partially encoded characters will not be written." The return value counts only complete characters.Repro (node Buffer oracle vs this shim)
Root cause
utf8Writeanducs2Writepassedbuf.length - offset(the whole remaining buffer) as the byte cap toutf8ToBytes/utf16leToBytes, ignoring the explicitlength:So the encoders always generated the full multibyte sequence for the next character, and
blitBufferthen copied onlylengthbytes of it, truncating the character mid-sequence and returning the truncated byte count.utf8ToBytesandutf16leToBytesalready cap output atomically per character via theirunitsargument (a multibyte char is pushed only if all of its bytes fit). Passing the effectivelengthas that cap makes the encoder drop an incomplete trailing character instead of emitting a partial one.Fix
Verification
test/write.jsasserting node parity for the cases above. It fails onmaster(6 failing assertions) and passes with the fix.npm testreports all assertions passing.Bufferover 5 encodings, 18 strings (ASCII, multibyte, surrogate pairs, emoji, lone surrogates), offsets 0 to 4, and lengths 0 to 12 (5940 cases): zero mismatches in return value and buffer contents.Closed issue #48 covered the implicit length case (a buffer too small for the whole string, no
lengthargument); that path was already fixed by thebuf.length - offsetcap. This is the distinct case where an explicitlengthargument is smaller than a single character.