fix(utf32): reassemble split codepoint from overflow buffer, not source index#393
Open
spokodev wants to merge 1 commit into
Open
fix(utf32): reassemble split codepoint from overflow buffer, not source index#393spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
When a 4-byte UTF-32 unit is split across two stream chunks, the decoder fills `this.overflow` to four bytes and then read it back with the source index `i` (`overflow[i]`...`overflow[i + 3]`) instead of `overflow[0]`... `overflow[3]`. Since `overflow` only holds indices 0-3, the read landed out of range whenever `i > 0`, so every codepoint straddling a chunk boundary decoded to U+0000 (LE) or a byte-shifted character (BE). This block was copied from the main loop (which correctly uses `src[i]`); the index just was not adjusted for the overflow buffer. Whole-buffer decode was unaffected, which is why existing tests passed.
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
When decoding UTF-32 from a stream, any 4-byte unit that straddles a chunk boundary is corrupted:
In big-endian the same split yields a byte-shifted character instead of
A. Whole-buffericonv.decode(buf, 'utf-32le')is unaffected — only the streaming /decodeStreampath, where chunk boundaries are arbitrary (sockets, files), hits this.Cause
encodings/utf32.jsfillsthis.overflowto four bytes, then reassembles the codepoint using the source indexi:overflowonly holds indices 0–3, but after the fill loopiis the offset intosrc, sooverflow[i]reads out of range (→undefined→0) wheneveri > 0. The code comment notes this block was copied from the main loop (which correctly usessrc[i]); the index was just never adjusted for the overflow buffer.Fix
Read the reassembled bytes from
overflow[0..3]:Verification
test/utf32-test.jsdecodeutf32leBuf/utf32beBufsplit at every byte offset; they fail before, pass after.