Skip to content

guard is_space against negative signed code units#395

Open
sahvx655-wq wants to merge 1 commit into
fastfloat:mainfrom
sahvx655-wq:is-space-signed-compare
Open

guard is_space against negative signed code units#395
sahvx655-wq wants to merge 1 commit into
fastfloat:mainfrom
sahvx655-wq:is-space-signed-compare

Conversation

@sahvx655-wq

Copy link
Copy Markdown
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant