Skip to content

InterleaveShortest: don't overflow size_hint lower bound#1102

Open
c-tonneslan wants to merge 1 commit into
rust-itertools:masterfrom
c-tonneslan:fix/interleave-shortest-size-hint-overflow
Open

InterleaveShortest: don't overflow size_hint lower bound#1102
c-tonneslan wants to merge 1 commit into
rust-itertools:masterfrom
c-tonneslan:fix/interleave-shortest-size-hint-overflow

Conversation

@c-tonneslan
Copy link
Copy Markdown

combined_lower is computed with saturating_mul, so it can already sit at usize::MAX. The next line then does an unchecked combined_lower + 1 when the current half has a larger lower bound, which panics in debug builds. Hit it with something like iter::repeat(0).take(usize::MAX).interleave_shortest(iter::repeat(1)).

Swap the + for saturating_add.

Closes #1068.

The combined lower bound was computed with saturating_mul, so could
already be at usize::MAX, and then the unchecked +1 would panic in
debug builds when both halves of the interleave reported large lower
bounds (e.g. an Iterator::take(usize::MAX) interleaved with
iter::repeat). Switch to saturating_add.

Closes rust-itertools#1068.

Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
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.

interleave_shortest's size_hint panics with large iterators.

1 participant