Open
Conversation
The UTF-16 decoder was incorrectly accepting low surrogates (#xdc00-#xdfff) as the first word of a surrogate pair. According to the UTF-16 specification, only high surrogates (#xd800-#xdbff) are valid as the first word. This fix changes the validation in both UTF-16 LE and BE decoders to only accept the correct range of high surrogates, properly rejecting invalid sequences where low surrogates appear as the first word. Also added comprehensive test cases to verify that low surrogate pairs are correctly rejected with encoding errors.
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.
Summary
This PR fixes a bug in the UTF-16 decoder where low surrogates (#xdc00-#xdfff) were incorrectly accepted as the first word of a surrogate pair. According to the UTF-16 specification, only high surrogates (#xd800-#xdbff) are valid as the first word.
Problem
The UTF-16 decoder in
decode.lispwas using the range check(<= #xd800 word #xdfff)to detect surrogate pairs. This range includes both:This allowed malformed UTF-16 data with unpaired low surrogates to be incorrectly decoded as valid surrogate pairs, potentially producing garbage Unicode code points.
Solution
Changed the validation in both UTF-16 LE and BE decoders (lines 366 and 397 in decode.lisp) from:
to:
This ensures only high surrogates are accepted as the first word of a surrogate pair.
Testing
Added 8 comprehensive test cases to
test/test.lispthat verify:All existing tests pass, and the new tests correctly fail before the fix and pass after.
Test Results
Before fix: 7 new tests failed (incorrectly decoded invalid data)
After fix: All tests pass (properly reject invalid sequences)
Files Changed
decode.lisp: Fixed surrogate validation in UTF-16 LE and BE decoders (2 lines)test/test.lisp: Added 8 test cases for low surrogate validation (14 lines)