net_util: Fix MAC address parsing
It wrongly allowed addresses containing a + instead of a hex character. Signed-off-by: Demi Marie Obenour <demiobenour@gmail.com>
This commit is contained in:
parent
d741cd53de
commit
aca7b01c6b
1 changed files with 6 additions and 7 deletions
|
|
@ -39,13 +39,10 @@ impl MacAddr {
|
|||
if v[i].len() != 2 {
|
||||
return common_err;
|
||||
}
|
||||
bytes[i] = u8::from_str_radix(v[i], 16).map_err(|e| {
|
||||
io::Error::other(format!(
|
||||
"parsing of {} into a MAC address failed: {}",
|
||||
s.as_ref(),
|
||||
e
|
||||
))
|
||||
})?;
|
||||
if !v[i].bytes().all(|a| a.is_ascii_hexdigit()) {
|
||||
return common_err;
|
||||
}
|
||||
bytes[i] = u8::from_str_radix(v[i], 16).unwrap();
|
||||
}
|
||||
|
||||
Ok(MacAddr { bytes })
|
||||
|
|
@ -187,6 +184,8 @@ mod unit_tests {
|
|||
let bytes = mac.get_bytes();
|
||||
assert_eq!(bytes, [0x12u8, 0x34, 0x56, 0x78, 0x9a, 0xbc]);
|
||||
|
||||
MacAddr::parse_str("12:34:56:78:9a:+c").unwrap_err();
|
||||
|
||||
let s = serde_json::to_string(&mac).expect("MacAddr serialization failed.");
|
||||
assert_eq!(s, "\"12:34:56:78:9a:bc\"");
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue