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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions strum_macros/src/macros/enum_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,14 @@ pub fn enum_iter_inner(ast: &syn::DeriveInput) -> TokenStream {
#(#arms),*
};

self.idx += 1;
if self.idx < #variant_count {
self.idx += 1;
}
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

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.

(t, Some(t))
}
}
Expand Down
3 changes: 3 additions & 0 deletions strum_tests/tests/enum_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ fn len_test() {
i.next();

assert_eq!(0, i.len());
i.next();

assert_eq!(0, i.size_hint().1.unwrap());
}

#[test]
Expand Down