Skip to content

Commit

Permalink
Add test to reproduce #56
Browse files Browse the repository at this point in the history
  • Loading branch information
willstott101 committed Jul 21, 2024
1 parent faa90de commit 84b1f33
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/dns_parser/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use thiserror::Error;
#[derive(Debug, Error)]
#[allow(dead_code)]
pub enum Error {
#[error("invalid compression pointer")]
BadPointer,
#[error("packet is smaller than header size")]
HeaderTooShort,
#[error("packet is has incomplete data")]
Expand Down
28 changes: 28 additions & 0 deletions src/dns_parser/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,31 @@ impl<'a> PartialEq for Name<'a> {
}

impl<'a> Eq for Name<'a> {}

#[cfg(test)]
mod test {
use super::Error;
use super::Name;

#[test]
fn parse_badpointer_same_offset() {
// A buffer where an offset points to itself,
// which is a bad compression pointer.
let same_offset = vec![192, 2, 192, 2];
let is_match = matches!(Name::scan(&same_offset, &same_offset),
Err(Error::BadPointer));

assert!(is_match);
}

#[test]
fn parse_badpointer_forward_offset() {
// A buffer where the offsets points back to each other which causes
// infinite recursion if never checked, a bad compression pointer.
let forwards_offset = vec![192, 2, 192, 4, 192, 2];
let is_match = matches!(Name::scan(&forwards_offset, &forwards_offset),
Err(Error::BadPointer));

assert!(is_match);
}
}

0 comments on commit 84b1f33

Please sign in to comment.