Skip to content

core: Fix size_hint for signed integer Range<T> iterators #24865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2015

Conversation

bluss
Copy link
Member

@bluss bluss commented Apr 27, 2015

core: Fix size_hint for signed integer Range<T> iterators

There was an overflow bug in .size_hint() for signed iterators, which
produced an hilariously incorrect size or an overflow panic.

Incorrect size is a serious bug since the iterators are marked
ExactSizeIterator. (And leads to abort() on (-1i8..127).collect() when
the collection tries to preallocate too much).

(-1i8..127).size_hint()
(18446744073709551488, Some(18446744073709551488))

Bug found using quickcheck.

Fixes #24851

@rust-highfive
Copy link
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @huonw (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see CONTRIBUTING.md for more information.

@bluss
Copy link
Member Author

bluss commented Apr 27, 2015

I think this bug fix should be merged to the beta branch.

@@ -2407,6 +2407,8 @@ pub trait Step: PartialOrd {
/// `start` should always be less than `end`, so the result should never
/// be negative.
///
/// `by` must be > 0.
///
/// Returns `None` if it is not possible to calculate steps_between
Copy link
Member Author

Choose a reason for hiding this comment

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

Explanation: "Result should never be negative" above implies this.

There was an overflow bug in .size_hint() for signed iterators, which
produced an hilariously incorrect size or an overflow panic.

Incorrect size is a serious bug since the iterators are marked
ExactSizeIterator. (And leads to abort() on (-1i8..127).collect() when
the collection tries to preallocate too much).

All signed range iterators were affected.

> (-1i8..127).size_hint()
(18446744073709551488, Some(18446744073709551488))

Bug found using quickcheck.

Fixes rust-lang#24851
@bluss
Copy link
Member Author

bluss commented Apr 27, 2015

Updated. I chose to split unsigned and signed. Alternative to that would be to do the cast to either isize or usize without overflow checking (awkward transmute?).

@alexcrichton
Copy link
Member

@bors: r+ 95be21d

Thanks!

@bors
Copy link
Collaborator

bors commented Apr 28, 2015

⌛ Testing commit 95be21d with merge 7b98743...

@bors
Copy link
Collaborator

bors commented Apr 28, 2015

💔 Test failed - auto-win-32-nopt-t

@bluss
Copy link
Member Author

bluss commented Apr 28, 2015

This is just some weird bors failure

@alexcrichton
Copy link
Member

@bors: retry

On Tue, Apr 28, 2015 at 2:42 AM, bluss [email protected] wrote:

This is just some weird bors failure


Reply to this email directly or view it on GitHub
#24865 (comment).

bors added a commit that referenced this pull request Apr 29, 2015
core: Fix size_hint for signed integer `Range<T>` iterators

There was an overflow bug in .size_hint() for signed iterators, which
produced an hilariously incorrect size or an overflow panic.

Incorrect size is a serious bug since the iterators are marked
ExactSizeIterator. (And leads to abort() on (-1i8..127).collect() when
the collection tries to preallocate too much).

> (-1i8..127).size_hint()
(18446744073709551488, Some(18446744073709551488))

Bug found using quickcheck.

Fixes #24851
@bors
Copy link
Collaborator

bors commented Apr 29, 2015

⌛ Testing commit 95be21d with merge c4b23ae...

@bors bors merged commit 95be21d into rust-lang:master Apr 29, 2015
@bluss bluss deleted the range-size branch April 29, 2015 07:59
@bluss
Copy link
Member Author

bluss commented Apr 29, 2015

I'd like to nominate this for beta inclusion. (Can I do that?)

@pnkfelix
Copy link
Member

triage: beta-nominated

(just want to ensure discussion; not directly advocating.)

@rust-highfive rust-highfive added the beta-nominated Nominated for backporting to the compiler in the beta channel. label Apr 29, 2015
@pnkfelix
Copy link
Member

not accepted for beta backport.

@pnkfelix pnkfelix removed the beta-nominated Nominated for backporting to the compiler in the beta channel. label Apr 30, 2015
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.

Overflow in Range<A> size_hint()
6 participants