Fix overflow when a parameter range ends at the numeric type's maximum - #905
Closed
santhreal wants to merge 1 commit into
Closed
Fix overflow when a parameter range ends at the numeric type's maximum#905santhreal wants to merge 1 commit into
santhreal wants to merge 1 commit into
Conversation
`RangeStep::next` yielded the final in-range value and then unconditionally computed `state + step`. When `end` is at (or within `step` of) the numeric type's maximum — e.g. `--parameter-scan x 2147483645 2147483647` — that final increment overflows: it panics in debug builds and wraps around in release, causing the iterator to run far past `end`. Track a `done` flag and only advance `state` while `state + step` stays within `end`, so the last element is yielded without an overflowing increment. `end - state` is non-negative (state <= end) and `step` is positive (validated in `new`), so the guard itself never overflows. Add a regression test covering a range that ends at `i32::MAX`.
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.
Problem
RangeStep::nextyields the current value and then unconditionally computesstate + step:When
endis at (or withinstepof) the numeric type's maximum, that final increment overflows.RangeStep::newallows such a range through as long as it's small enough (the size check passes), e.g.--parameter-scan x 2147483645 2147483647:attempt to add with overflowstatewraps around (toi32::MIN), sostate > endstays false and the iterator runs far pastendinstead of terminating.Fix
Track a
doneflag and only advancestatewhilestate + stepstays withinend; otherwise the current value is the last element.end - stateis non-negative (state <= endis already checked) andstepis positive (validated innew), so the guard itself can't overflow.Tests
Adds
test_range_reaching_type_max_terminates(a range ending ati32::MAX), which panics on the old code and passes now. Existingrange_steptests are unchanged and still pass;cargo fmt --checkis clean.