-
Notifications
You must be signed in to change notification settings - Fork 15.4k
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
protoc: fix consistency with parsing very large decimal numbers #10555
Merged
fowles
merged 11 commits into
protocolbuffers:main
from
jhump:jh/fix-consistency-with-very-large-decimal-numbers
Sep 15, 2022
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2270d3f
allow excessively large int literal values (that would otherwise over…
jhump 87f24e4
add allowed ranges to error messages
jhump 4e54ec2
change format of int range in error message; use macro to make DRY
jhump 35dd193
add test to verify parsing of extremely large decimal integers to dou…
jhump 4c69337
use template instead of macro
jhump 7702355
address latest review comments
jhump 0bc90b1
put helpers into anon namespace
jhump f82be68
avoid possible exception; error if octal or hex literal that is too l…
jhump d6acffb
use normal conditional
jhump 0362a12
initialize var to avoid undefined return val
jhump 7e745c4
oops, fix name: LOG -> GOOGLE_LOG
jhump File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -288,6 +288,16 @@ bool Parser::ConsumeInteger64(uint64_t max_value, uint64_t* output, | |
} | ||
} | ||
|
||
bool Parser::TryConsumeInteger64(uint64_t max_value, uint64_t* output) { | ||
if (LookingAtType(io::Tokenizer::TYPE_INTEGER) && | ||
io::Tokenizer::ParseInteger(input_->current().text, max_value, | ||
output)) { | ||
input_->Next(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
bool Parser::ConsumeNumber(double* output, const char* error) { | ||
if (LookingAtType(io::Tokenizer::TYPE_FLOAT)) { | ||
*output = io::Tokenizer::ParseFloat(input_->current().text); | ||
|
@@ -296,13 +306,19 @@ bool Parser::ConsumeNumber(double* output, const char* error) { | |
} else if (LookingAtType(io::Tokenizer::TYPE_INTEGER)) { | ||
// Also accept integers. | ||
uint64_t value = 0; | ||
if (!io::Tokenizer::ParseInteger(input_->current().text, | ||
if (io::Tokenizer::ParseInteger(input_->current().text, | ||
std::numeric_limits<uint64_t>::max(), | ||
&value)) { | ||
*output = value; | ||
} else if (input_->current().text[0] == '0') { | ||
// octal or hexadecimal; don't bother parsing as float | ||
AddError("Integer out of range."); | ||
// We still return true because we did, in fact, parse a number. | ||
} else if (!io::Tokenizer::TryParseFloat(input_->current().text, output)) { | ||
// out of int range, and not valid float? 🤷 | ||
AddError("Integer out of range."); | ||
// We still return true because we did, in fact, parse a number. | ||
Comment on lines
+317
to
320
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. This is "just in case". I can't think of any actual scenario, other octal or hex literal (handled above), where |
||
} | ||
*output = value; | ||
input_->Next(); | ||
return true; | ||
} else if (LookingAt("inf")) { | ||
|
@@ -1551,18 +1567,20 @@ bool Parser::ParseOption(Message* options, | |
is_negative | ||
? static_cast<uint64_t>(std::numeric_limits<int64_t>::max()) + 1 | ||
: std::numeric_limits<uint64_t>::max(); | ||
DO(ConsumeInteger64(max_value, &value, "Expected integer.")); | ||
if (is_negative) { | ||
value_location.AddPath( | ||
UninterpretedOption::kNegativeIntValueFieldNumber); | ||
uninterpreted_option->set_negative_int_value( | ||
static_cast<int64_t>(0 - value)); | ||
} else { | ||
value_location.AddPath( | ||
UninterpretedOption::kPositiveIntValueFieldNumber); | ||
uninterpreted_option->set_positive_int_value(value); | ||
if (TryConsumeInteger64(max_value, &value)) { | ||
if (is_negative) { | ||
value_location.AddPath( | ||
UninterpretedOption::kNegativeIntValueFieldNumber); | ||
uninterpreted_option->set_negative_int_value( | ||
static_cast<int64_t>(0 - value)); | ||
} else { | ||
value_location.AddPath( | ||
UninterpretedOption::kPositiveIntValueFieldNumber); | ||
uninterpreted_option->set_positive_int_value(value); | ||
} | ||
break; | ||
} | ||
break; | ||
// value too large for an integer; fall through below to treat as floating point | ||
} | ||
|
||
case io::Tokenizer::TYPE_FLOAT: { | ||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I put this here, instead of just relying on the branch below, because I think an octal literal would likely be successfully parsed by
ParseFloat
, but incorrectly interpreted as decimal.