Improve the Iterator for IPv6 options

- Add a helper function for returning an error in the implementation of
   next
 - Add an assert in Ipv6OptionsIterator::new that ensures that an
   iterator is not created with a length field larger than that of the
   data buffer we're iterating over.

Closes: #230
Approved by: dlrobertson
v0.7.x
Dan Robertson 2018-06-06 19:22:03 +00:00 committed by Homu
parent 938fd55763
commit e88f58739b
1 changed files with 28 additions and 5 deletions

View File

@ -292,7 +292,15 @@ pub struct Ipv6OptionsIterator<'a> {
}
impl<'a> Ipv6OptionsIterator<'a> {
/// Create a new `Ipv6OptionsIterator`, used to iterate over the
/// options contained in a IPv6 Extension Header (e.g. the Hop-by-Hop
/// header).
///
/// # Panics
/// This function panics if the `length` provided is larger than the
/// length of the `data` buffer.
pub fn new(data: &'a [u8], length: usize) -> Ipv6OptionsIterator<'a> {
assert!(length <= data.len());
Ipv6OptionsIterator {
pos: 0,
hit_error: false,
@ -301,6 +309,16 @@ impl<'a> Ipv6OptionsIterator<'a> {
}
}
impl<'a> Ipv6OptionsIterator<'a> {
/// Helper function to return an error in the implementation
/// of `Iterator`.
#[inline]
fn return_err(&mut self, err: Error) -> Option<Result<Repr<'a>>> {
self.hit_error = true;
Some(Err(err))
}
}
impl<'a> Iterator for Ipv6OptionsIterator<'a> {
type Item = Result<Repr<'a>>;
@ -315,15 +333,13 @@ impl<'a> Iterator for Ipv6OptionsIterator<'a> {
self.pos += repr.buffer_len();
Some(Ok(repr))
}
err => {
self.hit_error = true;
Some(err)
Err(e) => {
self.return_err(e)
}
}
}
Err(e) => {
self.hit_error = true;
Some(Err(e))
self.return_err(e)
}
}
} else {
@ -500,4 +516,11 @@ mod test {
}
}
}
#[test]
#[should_panic(expected = "length <= data.len()")]
fn test_options_iter_truncated() {
let options = [0x01, 0x02, 0x00, 0x00];
let _ = Ipv6OptionsIterator::new(&options, 5);
}
}