Skip to content

Commit

Permalink
de_net: Fix header parsing
Browse files Browse the repository at this point in the history
Fixes #754.
  • Loading branch information
Indy2222 committed Oct 7, 2023
1 parent 443524c commit b42aac2
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions crates/net/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ impl DatagramHeader {
}

/// Reads the header from the beginning of a bytes buffer.
///
/// # Panics
///
/// Panics if the buffer is smaller than header.
pub(crate) fn read(data: &[u8]) -> Result<Self, HeaderError> {
assert!(data.len() >= 4);
if data.len() < 4 {
return Err(HeaderError::Incomplete);
}

debug_assert!(u32::BITS == (HEADER_SIZE as u32) * 8);

let mask = data[0];
Expand Down Expand Up @@ -202,6 +201,8 @@ impl fmt::Display for Peers {
pub(crate) enum HeaderError {
#[error("The header is invalid")]
Invalid,
#[error("The data is too short and does not contain full header.")]
Incomplete,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
Expand Down

0 comments on commit b42aac2

Please sign in to comment.