Skip to content

Commit

Permalink
Add new error kind BufferTooSmall, and raise error if buffer capacity…
Browse files Browse the repository at this point in the history
… is too small (Closes #29)
  • Loading branch information
chifflier committed Jan 22, 2024
1 parent b4b1d27 commit 56aa4cc
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 4 deletions.
Binary file added assets/err-buffertoosmall.pcapng
Binary file not shown.
10 changes: 9 additions & 1 deletion src/capture_pcap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,15 @@ where
Err(PcapError::UnexpectedEof)
} else {
match n {
Needed::Size(n) => Err(PcapError::Incomplete(n.into())),
Needed::Size(n) => {
if self.buffer.available_data() + usize::from(n)
>= self.buffer.capacity()
{
Err(PcapError::BufferTooSmall)
} else {
Err(PcapError::Incomplete(n.into()))
}
}
Needed::Unknown => Err(PcapError::Incomplete(0)),
}
}
Expand Down
14 changes: 11 additions & 3 deletions src/capture_pcapng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::traits::PcapReaderIterator;
use circular::Buffer;
use nom::combinator::{complete, map};
use nom::multi::many1;
use nom::{IResult, Offset, Needed};
use nom::{IResult, Needed, Offset};
use std::fmt;
use std::io::Read;

Expand Down Expand Up @@ -179,11 +179,19 @@ where
Err(PcapError::UnexpectedEof)
} else {
match n {
Needed::Size(n) => Err(PcapError::Incomplete(n.into())),
Needed::Size(n) => {
if self.buffer.available_data() + usize::from(n)
>= self.buffer.capacity()
{
Err(PcapError::BufferTooSmall)
} else {
Err(PcapError::Incomplete(n.into()))
}
}
Needed::Unknown => Err(PcapError::Incomplete(0)),
}
}
},
}
}
}
fn consume(&mut self, offset: usize) {
Expand Down
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::fmt;
pub enum PcapError<I: Sized> {
/// No more data available
Eof,
/// Buffer capacity is too small, and some full frame cannot be stored
BufferTooSmall,
/// Expected more data but got EOF
UnexpectedEof,
/// An error happened during a `read` operation
Expand Down Expand Up @@ -38,6 +40,7 @@ where
pub fn to_owned_vec(&self) -> PcapError<&'static [u8]> {
match self {
PcapError::Eof => PcapError::Eof,
PcapError::BufferTooSmall => PcapError::BufferTooSmall,
PcapError::UnexpectedEof => PcapError::UnexpectedEof,
PcapError::ReadError => PcapError::ReadError,
PcapError::Incomplete(n) => PcapError::Incomplete(*n),
Expand Down Expand Up @@ -66,6 +69,7 @@ where
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PcapError::Eof => write!(f, "End of file"),
PcapError::BufferTooSmall => write!(f, "Buffer is too small"),
PcapError::UnexpectedEof => write!(f, "Unexpected end of file"),
PcapError::ReadError => write!(f, "Read error"),
PcapError::Incomplete(n) => write!(f, "Incomplete read: {n}"),

Check failure on line 75 in src/error.rs

View workflow job for this annotation

GitHub Actions / Check (1.53.0)

there is no argument named `n`
Expand Down
28 changes: 28 additions & 0 deletions tests/pcapng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,31 @@ fn err_eof() {
let res = parse_block_le(data).expect_err("expected incomplete");
assert!(res.is_incomplete());
}

// related issue: https://github.com/rusticata/pcap-parser/issues/29
#[test]
fn test_reader_buffer_too_small() {
let file = File::open("assets/err-buffertoosmall.pcapng").unwrap();
let mut reader = create_reader(1024, file).expect("PcapNGReader");
let mut num_blocks = 0;
let mut num_refills = 0;
const MAX_REFILLS: usize = 20;
// the only expected way to exit this loop is to encounter BufferTooSmall
// check number of refills to detect infinite loops
loop {
match reader.next() {
Ok((offset, _block)) => {
num_blocks += 1;
reader.consume(offset)
}
Err(PcapError::Incomplete(_)) => {
num_refills += 1;
assert!(num_refills < MAX_REFILLS);
reader.refill().unwrap();
}
Err(PcapError::BufferTooSmall) => break,
Err(e) => panic!("Unexpected error {:?}", e),
}
}
assert_eq!(num_blocks, 9);
}

0 comments on commit 56aa4cc

Please sign in to comment.