diff --git a/common/Cargo.toml b/common/Cargo.toml index d7a7bc0a..8ca71f6b 100644 --- a/common/Cargo.toml +++ b/common/Cargo.toml @@ -21,5 +21,4 @@ slog-term.workspace = true slog-async.workspace = true smf.workspace = true thiserror.workspace = true -tokio.workspace = true - +tokio = { workspace = true, features = [ "full" ] } diff --git a/common/src/ports.rs b/common/src/ports.rs index 2d7fccf6..cc2d78ef 100644 --- a/common/src/ports.rs +++ b/common/src/ports.rs @@ -92,7 +92,13 @@ macro_rules! make_port_type { if s.len() <= $prefix.len() { return Err("Invalid port kind"); } - let (head, tail) = s.split_at($prefix.len()); + if !s.is_ascii() { + return Err("Port IDs must be ASCII"); + } + let Some((head, tail)) = s.split_at_checked($prefix.len()) + else { + return Err("Invalid port kind"); + }; if head.eq_ignore_ascii_case($prefix) { tail.parse::() .map_err(|_| "Invalid port index") @@ -606,3 +612,15 @@ pub struct XcvrSettings { /// Equalization settings pub tx_eq: Option, } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn port_ids_are_ascii() { + let e = PortId::try_from("abč0") + .expect_err("Should have returned an error"); + assert_eq!(e.to_string(), "Invalid switch port ID"); + } +}