Skip to content
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

Add checks to guard against overflow in iterator methods. #76

Merged
merged 1 commit into from Nov 17, 2019
Merged

Add checks to guard against overflow in iterator methods. #76

merged 1 commit into from Nov 17, 2019

Conversation

hermanventer
Copy link
Contributor

@hermanventer hermanventer commented Nov 8, 2019

When a generated iterator runs of the end it keeps on incrementing the index variable, which is undesirable. Furthermore the size_hint method does not guard against the index being greater than the variant count, which causes an overflow.

These issues cause our static analyses tool to generate warnings when analyzing code generated by strum, which is annoying.

Thanks for considering this.

@Peternator7 Peternator7 merged commit e981f24 into Peternator7:master Nov 17, 2019
1 check passed
@Peternator7
Copy link
Owner

@Peternator7 Peternator7 commented Nov 17, 2019

Looks great! Thanks for the PR :)

@hermanventer hermanventer deleted the guard_against_overflow branch Nov 17, 2019
output
}

fn size_hint(&self) -> (usize, Option<usize>) {
let t = #variant_count - self.idx;
let t = if self.idx >= #variant_count { 0 } else { #variant_count - self.idx };
Copy link
Contributor

@Ten0 Ten0 Nov 23, 2021

Choose a reason for hiding this comment

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

Looks like this is unnecessary given that the above guarantees this will never underflow.

Additionally, this property is FusedIterator (which is not a requirement for Iterator, and it's the responsibility of the caller to stop calling it). If we choose to have it, then we should also signify that we have it by implementing FusedIterator.

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.

None yet

3 participants