fix(builtins): guard against exponent-too-large in units.parse/parse_bytes - #800
Open
Anand Krishnamoorthi (anakrish) wants to merge 1 commit into
Open
fix(builtins): guard against exponent-too-large in units.parse/parse_bytes#800Anand Krishnamoorthi (anakrish) wants to merge 1 commit into
Anand Krishnamoorthi (anakrish) wants to merge 1 commit into
Conversation
…bytes Inputs like "1e9999999K" caused Number::from_str to fail after silently constructing a string with an enormous exponent, risking numeric overflow or downstream panics. Add an early check that rejects any number whose exponent field exceeds 6 digits. - Add exponent_too_large() helper: find 'e'/'E', strip optional sign, reject if digit count > 6 - Call it in parse() before the ten_exp/two_exp dispatch - Call it in parse_bytes() before the twob_exp/tenb_exp dispatch - Add YAML regression test covering: units.parse too-large exponent (error), units.parse_bytes too-large exponent (error), and a normal 1e6 exponent that must still succeed Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
Inputs like
"1e9999999K"caused downstream numeric overflow because Number::from_str was called with a string containing an enormous exponent. Guard against this by rejecting any number whose exponent field exceeds 6 digits before dispatching to the SI/binary-prefix matching logic.Changes
exponent_too_large(number: &str) -> boolhelper: finds thee/Eposition, strips an optional sign, and returnstruewhen the digit count exceeds 6parse()(units.parse) immediately after canonicalization, beforeten_exp/two_expdispatchparse_bytes()(units.parse_bytes) beforetwob_exp/tenb_expdispatchTests
New
tests/interpreter/cases/builtins/units/exponent.yamlcovering:units.parse("1e9999999K")errors with "exponent too large"units.parse_bytes("1e9999999Ki")errors with "exponent too large"units.parse("1e6K")succeeds (1e9) — boundary case that must still workCo-authored-by: Copilot 223556219+Copilot@users.noreply.github.com