Fix GH-21738: undefined behavior in isxdigit() callsites with non-ASCII bytes#21861
Open
lacatoire wants to merge 2 commits intophp:masterfrom
Open
Fix GH-21738: undefined behavior in isxdigit() callsites with non-ASCII bytes#21861lacatoire wants to merge 2 commits intophp:masterfrom
lacatoire wants to merge 2 commits intophp:masterfrom
Conversation
The isxdigit() family requires its argument to be representable as unsigned char (0-255) or EOF. Casting a signed char value holding a high-bit byte (e.g. 0x80) to int produces a negative number (-128) which triggers undefined behavior, and on some libc implementations (e.g. NetBSD) can lead to out-of-bounds reads through the internal character classification table.
Same signed-char UB as in url_decode: isxdigit() is called on byte values from char-pointers (str_in in quoted_printable_decode, source in php_stripcslashes), which sign-extend to negative int on signed-char platforms when the byte has its high bit set.
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.
Cast the byte to
unsigned charbefore passing it toisxdigit().<ctype.h>functions require their argument to be representable asunsigned char(0-255) orEOF; passing a sign-extendedcharfor high-bit bytes like\x80is undefined behavior.Callsites fixed:
php_url_decode_exandphp_raw_url_decode_exinext/standard/url.cquoted_printable_decodeinext/standard/quot_print.cphp_stripcslashesinext/standard/string.cFixes GH-21738