From 432a9f2bacf7dfd1741b725c23bcfa23b01b92bc Mon Sep 17 00:00:00 2001 From: Nils Andreas Svee Date: Thu, 13 Oct 2022 17:15:13 +0200 Subject: [PATCH] Add error for UTF-8 parsing --- src/netlink.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/netlink.rs b/src/netlink.rs index 4cddb7c..63a62e4 100644 --- a/src/netlink.rs +++ b/src/netlink.rs @@ -10,6 +10,7 @@ use neli::socket::NlSocketHandle; use neli::types::{Buffer, RtBuffer}; use serde::Deserialize; use std::io; +use std::str::Utf8Error; use thiserror::Error; use bincode::deserialize; @@ -40,6 +41,9 @@ pub enum NetlinkError { #[error("Serialization error")] Serialization(#[from] SerError), + #[error("Error happened while parsing UTF-8 string")] + Utf8Error(#[from] Utf8Error), + #[error("Invalid Rtm type (expected {expected:?}, found {found:?})")] WrongType { expected: Rtm, found: Rtm }, } @@ -214,9 +218,8 @@ impl Netlink { for attr in p.rtattrs.iter() { if attr.rta_type == Tca::Kind { let buff = attr.rta_payload.as_ref(); - _type = std::str::from_utf8(buff) - .expect("Found invalid UTF-8 string") - .trim_end_matches('\0'); // Null terminator is valid UTF-8, but breaks comparison, so we remove it + _type = std::str::from_utf8(buff)?.trim_end_matches('\0'); + // Null terminator is valid UTF-8, but breaks comparison, so we remove it } }