Skip to content

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

Open
anakrish wants to merge 1 commit into
mainfrom
fix/units-exponent-limit
Open

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

Conversation

@anakrish

Copy link
Copy Markdown
Owner

Inputs like "1e9999999K" caused Number::from_str to fail after constructing a string with an enormous exponent, risking numeric overflow or downstream panics.

Fix: Add an early check in units.parse and units.parse_bytes that rejects any number whose exponent field exceeds 6 digits, returning an error before the problematic conversion.

Files changed:

  • src/builtins/units.rs — exponent length guard
  • tests/interpreter/cases/builtins/units/exponent.yaml — YAML tests for oversized exponents

Future upstream PR: microsoft#800

…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>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The guards incorrectly turn unsupported unit suffixes from undefined results into errors.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Adds safeguards against oversized scientific exponents in unit parsing.

Changes:

  • Rejects exponent fields longer than six digits.
  • Adds oversized and valid-exponent tests.
File summaries
File Description
src/builtins/units.rs Adds exponent-length validation.
tests/interpreter/cases/builtins/units/exponent.yaml Covers exponent limits.
Review details

Suppressed comments (1)

src/builtins/units.rs:180

  • This similarly changes unsupported-suffix behavior: units.parse_bytes("1e9999999bogus") used to return undefined via the final branch, but now errors before the suffix is classified. Since that path never calls Number::from_str, gate this check on a recognized byte suffix.
    if exponent_too_large(canonical_part.as_ref()) {
        bail!(params[0]
            .span()
            .error("units.parse_bytes: exponent too large"));
    }
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Balanced

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/builtins/units.rs
Comment on lines +98 to +100
if exponent_too_large(canonical_part.as_ref()) {
bail!(params[0].span().error("units.parse: exponent too large"));
}
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.

2 participants