Skip to content

std::iter::Skip::count can overflow even if the iterator would return less that usize::max_value() items #68139

Closed
@ollie27

Description

@ollie27

std::iter::Skip::count currently calls count on the inner iterator but that can overflow even if the Skip itself doesn't return more that usize::max_value() items.

For example:

(0..usize::max_value()).chain(0..10).skip(usize::max_value()).count()

This should return 10 but currently triggers an attempt to add with overflow (or returns 0 if overflow checks are disabled).

This is caused by

#[inline]
fn count(self) -> usize {
self.iter.count().saturating_sub(self.n)
}
which was added in #25035 which was merged between versions 1.0 and 1.1.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-iteratorsArea: IteratorsC-bugCategory: This is a bug.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions