Skip to content

PeekNth: don't panic on peek_nth(usize::MAX)#1103

Open
c-tonneslan wants to merge 1 commit into
rust-itertools:masterfrom
c-tonneslan:fix/peek-nth-usize-max
Open

PeekNth: don't panic on peek_nth(usize::MAX)#1103
c-tonneslan wants to merge 1 commit into
rust-itertools:masterfrom
c-tonneslan:fix/peek-nth-usize-max

Conversation

@c-tonneslan
Copy link
Copy Markdown

(n + 1).saturating_sub(...) overflows when n is usize::MAX, panicking in debug builds. We can't actually peek at index usize::MAX (would need usize::MAX + 1 buffered items), so return None up front via checked_add.

Same fix in peek_nth_mut since it has the same expression.

Closes #1067.

n + 1 overflowed when n was usize::MAX, panicking in debug builds.
We can't peek at index usize::MAX anyway (would need usize::MAX+1
buffered items), so just return None up front via checked_add.

Closes rust-itertools#1067.

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.

Peeking usize::MAX with peek_nth

1 participant