Fix UTF-8 decoder rejecting valid U+0800 three-byte sequence#664
Merged
Conversation
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.
This PR fixes an off-by-one error in the UTF-8 three-byte decoding validation logic.
Problem
Transcoder::decodeincorrectly rejected the canonical UTF-8 encoding ofU+0800.The three-byte overlong validation check used:
which treated
0x0800itself as invalid, even though it is the smallest valid code point that legitimately requires a three-byte UTF-8 sequence.As a result, valid UTF-8 input containing the bytes:
(the canonical encoding of
U+0800) was decoded as0xFFFF, causing the caller to substituteTranscoder::LOSSCHARand silently corrupt the decoded output.Fix
Change the validation condition to:
This preserves rejection of true overlong encodings while correctly accepting
U+0800.Tests
Added
testDecodeUTF8_U0800regression coverage which:E0 A0 80Transcoder::encode(0x0800, …)LOSSCHARis introduced