mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
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:
committed by
Rob Bradford
parent
d741cd53de
commit
aca7b01c6b
+6
-7
@@ -39,13 +39,10 @@ impl MacAddr {
|
|||||||
if v[i].len() != 2 {
|
if v[i].len() != 2 {
|
||||||
return common_err;
|
return common_err;
|
||||||
}
|
}
|
||||||
bytes[i] = u8::from_str_radix(v[i], 16).map_err(|e| {
|
if !v[i].bytes().all(|a| a.is_ascii_hexdigit()) {
|
||||||
io::Error::other(format!(
|
return common_err;
|
||||||
"parsing of {} into a MAC address failed: {}",
|
}
|
||||||
s.as_ref(),
|
bytes[i] = u8::from_str_radix(v[i], 16).unwrap();
|
||||||
e
|
|
||||||
))
|
|
||||||
})?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(MacAddr { bytes })
|
Ok(MacAddr { bytes })
|
||||||
@@ -187,6 +184,8 @@ mod unit_tests {
|
|||||||
let bytes = mac.get_bytes();
|
let bytes = mac.get_bytes();
|
||||||
assert_eq!(bytes, [0x12u8, 0x34, 0x56, 0x78, 0x9a, 0xbc]);
|
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.");
|
let s = serde_json::to_string(&mac).expect("MacAddr serialization failed.");
|
||||||
assert_eq!(s, "\"12:34:56:78:9a:bc\"");
|
assert_eq!(s, "\"12:34:56:78:9a:bc\"");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user