Skip to content

fix(builtins): guard against exponent-too-large in units.parse/parse_bytes - #800

Open
Anand Krishnamoorthi (anakrish) wants to merge 1 commit into
microsoft:mainfrom
anakrish:fix/units-exponent-limit
Open

fix(builtins): guard against exponent-too-large in units.parse/parse_bytes#800
Anand Krishnamoorthi (anakrish) wants to merge 1 commit into
microsoft:mainfrom
anakrish:fix/units-exponent-limit

Conversation

@anakrish

Copy link
Copy Markdown
Collaborator

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

  • New exponent_too_large(number: &str) -> bool helper: finds the e/E position, strips an optional sign, and returns true when the digit count exceeds 6
  • Call it in parse() (units.parse) immediately after canonicalization, before ten_exp/two_exp dispatch
  • Call it in parse_bytes() (units.parse_bytes) before twob_exp/tenb_exp dispatch
  • Both sites emit a clear error message rather than propagating through Number arithmetic

Tests

New tests/interpreter/cases/builtins/units/exponent.yaml covering:

  • 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 work

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com

…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant