Skip to content

Commit

Permalink
Merge pull request #1027 from thvdveld/fix-lladdr-parse-panic
Browse files Browse the repository at this point in the history
Fix RawHardwareAddress panic when parsing to specific link layer address
  • Loading branch information
Dirbaio authored Jan 3, 2025
2 parents 104898b + 31fda8f commit 3a30d0c
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions src/wire/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,10 @@ pub struct RawHardwareAddress {

#[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
impl RawHardwareAddress {
/// Create a new `RawHardwareAddress` from a byte slice.
///
/// # Panics
/// Panics if `addr.len() > MAX_HARDWARE_ADDRESS_LEN`.
pub fn from_bytes(addr: &[u8]) -> Self {
let mut data = [0u8; MAX_HARDWARE_ADDRESS_LEN];
data[..addr.len()].copy_from_slice(addr);
Expand All @@ -504,7 +508,7 @@ impl RawHardwareAddress {
match medium {
#[cfg(feature = "medium-ethernet")]
Medium::Ethernet => {
if self.len() < 6 {
if self.len() != 6 {
return Err(Error);
}
Ok(HardwareAddress::Ethernet(EthernetAddress::from_bytes(
Expand All @@ -513,7 +517,7 @@ impl RawHardwareAddress {
}
#[cfg(feature = "medium-ieee802154")]
Medium::Ieee802154 => {
if self.len() < 8 {
if self.len() != 8 {
return Err(Error);
}
Ok(HardwareAddress::Ieee802154(Ieee802154Address::from_bytes(
Expand Down Expand Up @@ -559,3 +563,37 @@ impl From<HardwareAddress> for RawHardwareAddress {
Self::from_bytes(addr.as_bytes())
}
}

#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;

#[rstest]
#[cfg(feature = "medium-ethernet")]
#[case((Medium::Ethernet, &[0u8; 6][..]), Ok(HardwareAddress::Ethernet(EthernetAddress([0, 0, 0, 0, 0, 0]))))]
#[cfg(feature = "medium-ethernet")]
#[case((Medium::Ethernet, &[1u8; 5][..]), Err(Error))]
#[cfg(feature = "medium-ethernet")]
#[case((Medium::Ethernet, &[1u8; 7][..]), Err(Error))]
#[cfg(feature = "medium-ieee802154")]
#[case((Medium::Ieee802154, &[0u8; 8][..]), Ok(HardwareAddress::Ieee802154(Ieee802154Address::Extended([0, 0, 0, 0, 0, 0, 0, 0]))))]
#[cfg(feature = "medium-ieee802154")]
#[case((Medium::Ieee802154, &[1u8; 2][..]), Err(Error))]
#[cfg(feature = "medium-ieee802154")]
#[case((Medium::Ieee802154, &[1u8; 1][..]), Err(Error))]
fn parse_hardware_address(
#[case] input: (Medium, &[u8]),
#[case] expected: Result<HardwareAddress>,
) {
let (medium, input) = input;

// NOTE: we check the length since `RawHardwareAddress::parse()` panics if the length is
// invalid. MAX_HARDWARE_ADDRESS_LEN is based on the medium, and depending on the feature
// flags, it can be different.
if input.len() < MAX_HARDWARE_ADDRESS_LEN {
let raw = RawHardwareAddress::from_bytes(input);
assert_eq!(raw.parse(medium), expected);
}
}
}

0 comments on commit 3a30d0c

Please sign in to comment.