Add error for UTF-8 parsing

This commit is contained in:
Nils Andreas Svee committed 2022-10-13 17:15:13 +02:00
1 parent c190d2ec04
commit 432a9f2bac
1 file changed
+6 -3
+6 -3
View File
@@ -10,6 +10,7 @@ use neli::socket::NlSocketHandle;
use neli::types::{Buffer, RtBuffer}; use neli::types::{Buffer, RtBuffer};
use serde::Deserialize; use serde::Deserialize;
use std::io; use std::io;
use std::str::Utf8Error;
use thiserror::Error; use thiserror::Error;
use bincode::deserialize; use bincode::deserialize;
@@ -40,6 +41,9 @@ pub enum NetlinkError {
#[error("Serialization error")] #[error("Serialization error")]
Serialization(#[from] SerError), Serialization(#[from] SerError),
#[error("Error happened while parsing UTF-8 string")]
Utf8Error(#[from] Utf8Error),
#[error("Invalid Rtm type (expected {expected:?}, found {found:?})")] #[error("Invalid Rtm type (expected {expected:?}, found {found:?})")]
WrongType { expected: Rtm, found: Rtm }, WrongType { expected: Rtm, found: Rtm },
} }
@@ -214,9 +218,8 @@ impl Netlink {
for attr in p.rtattrs.iter() { for attr in p.rtattrs.iter() {
if attr.rta_type == Tca::Kind { if attr.rta_type == Tca::Kind {
let buff = attr.rta_payload.as_ref(); let buff = attr.rta_payload.as_ref();
_type = std::str::from_utf8(buff) _type = std::str::from_utf8(buff)?.trim_end_matches('\0');
.expect("Found invalid UTF-8 string") // Null terminator is valid UTF-8, but breaks comparison, so we remove it
.trim_end_matches('\0'); // Null terminator is valid UTF-8, but breaks comparison, so we remove it
} }
} }