-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[libc] Scanf shouldn't match just "0x" for hex int #112440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -124,13 +124,25 @@ int convert_int(Reader *reader, const FormatSection &to_conv) { | |
|
|
||
| if (to_lower(cur_char) == 'x') { | ||
| // This is a valid hex prefix. | ||
|
|
||
| is_number = false; | ||
| // A valid hex prefix is not necessarily a valid number. For the | ||
| // conversion to be valid it needs to use all of the characters it | ||
| // consumes. From the standard: | ||
| // 7.23.6.2 paragraph 9: "An input item is defined as the longest | ||
| // sequence of input characters which does not exceed any specified | ||
| // field width and which is, or is a prefix of, a matching input | ||
| // sequence." | ||
| // 7.23.6.2 paragraph 10: "If the input item is not a matching sequence, | ||
| // the execution of the directive fails: this condition is a matching | ||
| // failure" | ||
| base = 16; | ||
| if (max_width > 1) { | ||
| --max_width; | ||
| cur_char = reader->getc(); | ||
| } else { | ||
| write_int_with_length(0, to_conv); | ||
| return READ_OK; | ||
| // write_int_with_length(0, to_conv); | ||
| return MATCHING_FAILURE; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider making this an early return if you reverse the conditional.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| } | ||
|
|
||
| } else { | ||
|
|
@@ -198,6 +210,9 @@ int convert_int(Reader *reader, const FormatSection &to_conv) { | |
| // last one back. | ||
| reader->ungetc(cur_char); | ||
|
|
||
| if (!is_number) | ||
| return MATCHING_FAILURE; | ||
|
|
||
| if (has_overflow) { | ||
| write_int_with_length(MAX, to_conv); | ||
| } else { | ||
|
|
@@ -207,8 +222,6 @@ int convert_int(Reader *reader, const FormatSection &to_conv) { | |
| write_int_with_length(result, to_conv); | ||
| } | ||
|
|
||
| if (!is_number) | ||
| return MATCHING_FAILURE; | ||
| return READ_OK; | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.