Skip to content

Overflow in Range<A> size_hint() #24851

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

Closed
bluss opened this issue Apr 26, 2015 · 0 comments · Fixed by #24865
Closed

Overflow in Range<A> size_hint() #24851

bluss opened this issue Apr 26, 2015 · 0 comments · Fixed by #24865

Comments

@bluss
Copy link
Member

bluss commented Apr 26, 2015

size_hint is not implemented correctly for the ranges, it overflows:

playbot: (-1i8..127).size_hint()
-playbot:#rust-internals- (18446744073709551488, Some(18446744073709551488))

Expected result: 128

With other compilation settings, the result is:

'Error: arithmetic operation overflowed'
bluss pushed a commit to bluss/rust that referenced this issue Apr 27, 2015
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 rust-lang#24851
bluss pushed a commit to bluss/rust that referenced this issue Apr 27, 2015
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
bors added a commit that referenced this issue 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
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 a pull request may close this issue.

1 participant