Skip to content

Commit

Permalink
fix SliceLabelsIter
Browse files Browse the repository at this point in the history
The `Iterator` implementation on SliceLabelsIter was not advancing offset
with the a leading length octet.
  • Loading branch information
hunts committed May 29, 2024
1 parent f84abe0 commit 05aec7c
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/base/name/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,16 +741,17 @@ impl<'a> Iterator for SliceLabelsIter<'a> {
type Item = &'a Label;

fn next(&mut self) -> Option<Self::Item> {
if self.start == usize::MAX {
if self.start >= self.slice.len() {
return None;
}

loop {
match Label::split_from(&self.slice[self.start..]) {
Ok((label, _)) => {
if label.is_root() {
self.start = usize::MAX;
} else {
self.start += label.len();
self.start += label.len() + 1;
}
return Some(label);
}
Expand Down Expand Up @@ -882,6 +883,8 @@ impl std::error::Error for SplitLabelError {}

#[cfg(test)]
mod test {
use crate::base::Name;

use super::*;

#[test]
Expand Down Expand Up @@ -1034,4 +1037,33 @@ mod test {
],
);
}

#[test]
fn iter_slice() {
assert_eq!(None, Label::iter_slice(&[], 0).next());
assert_eq!(None, Label::iter_slice(&[], 1).next());

let name = Name::vec_from_str("example.com.").unwrap();
let buf = name.as_slice();

let mut it = Label::iter_slice(buf, 0);
assert_eq!(Label::from_slice(b"example").ok(), it.next());
assert_eq!(Label::from_slice(b"com").ok(), it.next());
assert_eq!(Some(Label::root()), it.next());
assert_eq!(None, it.next());

let mut it = Label::iter_slice(buf, b"example".len() + 1);
assert_eq!(
Label::from_slice(b"com").ok(),
it.next(),
"should jump to 2nd label"
);

let mut it = Label::iter_slice(buf, buf.len() - 1);
assert_eq!(
Some(Label::root()),
it.next(),
"should jump to last/root label"
);
}
}

0 comments on commit 05aec7c

Please sign in to comment.