Skip to content

Commit

Permalink
Add checks to guard against overflow in iterator methods. (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
Herman Venter authored and peter-glotfelty committed Nov 17, 2019
1 parent 9310855 commit e981f24
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
6 changes: 4 additions & 2 deletions strum_macros/src/macros/enum_iter.rs
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 };
(t, Some(t))
}
}
Expand Down
3 changes: 3 additions & 0 deletions strum_tests/tests/enum_iter.rs
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

0 comments on commit e981f24

Please sign in to comment.