Repository navigation
Count a pending U+0000 in the UTF-16 max length queries - #148
Merged
hsivonen merged 1 commit intoOct 30, 2026
Merged
Conversation
After an unpaired high surrogate followed by a BMP code unit, the UTF-16 decoder keeps the BMP code unit pending in `lead_surrogate` with `pending_bmp` set. `additional_from_state()` tested only `lead_surrogate != 0`, so a pending U+0000 was not counted, and a buffer of exactly `max_utf8_buffer_length*()` or `max_utf16_buffer_length()` could return `OutputFull`. Count the pending code unit whenever `pending_bmp` is set.
Owner
|
Thank you! |
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.
After an unpaired high surrogate followed by a BMP code unit, the UTF-16 decoder reports
Malformed(2, 2)and keeps the BMP unit pending inlead_surrogatewithpending_bmpset.additional_from_state()counts the pending unit only whenlead_surrogate != 0, so a pending U+0000 is not counted. A buffer of exactlymax_utf8_buffer_length(),max_utf8_buffer_length_without_replacement()ormax_utf16_buffer_length()can then getOutputFull, which the docs say won't happen:With U+0041 in the same spot,
maxis 10 and the call succeeds. The fix counts the pending unit wheneverpending_bmpis set.The new test
test_utf_16le_pending_nul_max_lengthfails without the change (OutputFull).cargo testpasses on stable, 1.88,--no-default-features,std,simd-accel(nightly), and thefast-legacy-encode/less-slow-*features.cargo fmt --checkpasses too.I found this by fuzzing the streaming decoders with random chunking and output buffers just at the documented bounds. The test is dedicated to the public domain like the rest of the test module.