reject non-digit wide code units in uint8/uint16 integer fast path#391
Merged
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.
While checking the fixed-width integer paths in
parse_int_string, I noticed theuint8_tanduint16_tbase-10 fast paths read code units byte-wise (a four-bytememcpyforuint8_t,read4_to_u32foruint16_t) and only ever look at the low byte. Forwchar_t/char16_t/char32_tthat means a non-digit code point whose low byte falls in0x30..0x39, say U+2131 through U+2139, is taken for the matching ASCII digit, sofrom_chars(u"ℱℲℳℴ", v, 10)returns 1234 withstd::errc()where it ought to fail. The genericch_to_digitloop already masks anything above0xFFand rejects these (the existing emoji tests confirm that forint), so the fast paths quietly disagree with the rest of the library and withstd::from_charssemantics.The byte-oriented SWAR is only sound when a code unit is a single byte, so both fast paths are now gated on
sizeof(UC) == 1and wider units fall through to the generic loop that already handles them. Keeping the guard at the fast-path entry leaves the byte assumption and the digit validation in one place rather than re-checking after a truncating read. Left alone this is a silent input-validation hole for anyone feeding untrusted UTF-16/UTF-32 into 8- or 16-bit integers. I added the wide-unit cases totests/fast_int.cpp; they parse as valid before the change and are rejected after it.