guard is_space against negative signed code units#395
Open
sahvx655-wq wants to merge 1 commit into
Open
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.
is_space gates its space_lut lookup with
c < 256, but that comparison is signed whenever UC is a signed type, and on Linux/macOS wchar_t is a signed 32-bit int. A negative code unit therefore passes the guard and is then narrowed with uint8_t(c), so a wide unit whose low byte is 0x20 but whose full value is negative (e.g. 0xFFFFFF20) is classified as whitespace. With skip_white_space enabled, from_chars silently skips that leading unit and parses the remainder, so an input that should be rejected comes back as a successful parse.Comparing the value through its unsigned type before the lookup keeps the guard honest for every code unit and leaves char/char16_t/char32_t behaviour unchanged. ch_to_digit a few lines below already takes this precaution for the same signed-char reason, so this brings is_space into line rather than asking callers to pre-sanitise. I added a regression case alongside the existing wide-char tests; it fails on the old code (the bogus prefix is accepted) and passes with the unsigned compare.