fix(builtins): guard against exponent-too-large in units.parse/parse_bytes - #87
Open
anakrish wants to merge 1 commit into
Open
fix(builtins): guard against exponent-too-large in units.parse/parse_bytes#87anakrish wants to merge 1 commit into
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>
There was a problem hiding this comment.
🟡 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 callsNumber::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 on lines
+98
to
+100
| if exponent_too_large(canonical_part.as_ref()) { | ||
| bail!(params[0].span().error("units.parse: exponent too large")); | ||
| } |
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"causedNumber::from_strto fail after constructing a string with an enormous exponent, risking numeric overflow or downstream panics.Fix: Add an early check in
units.parseandunits.parse_bytesthat rejects any number whose exponent field exceeds 6 digits, returning an error before the problematic conversion.Files changed:
src/builtins/units.rs— exponent length guardtests/interpreter/cases/builtins/units/exponent.yaml— YAML tests for oversized exponentsFuture upstream PR: microsoft#800