diff options
author | lafleur <lafleur@boum.org> | 2021-10-22 22:35:02 +0200 |
---|---|---|
committer | lafleur <lafleur@boum.org> | 2021-10-22 22:58:47 +0200 |
commit | b70868d53ed1729d4fa45ee1ee12e3b82e59ae15 (patch) | |
tree | 1e8ad81e564b2f10e48d6eca0d386a9baa120acb | |
parent | efe4317e95c9f4095bddd7c0185805dac9d9af25 (diff) |
check that LogPrefix is a 128 chars CString at most
-rw-r--r-- | rustables/src/expr/log.rs | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/rustables/src/expr/log.rs b/rustables/src/expr/log.rs index f5b961f..614e340 100644 --- a/rustables/src/expr/log.rs +++ b/rustables/src/expr/log.rs @@ -2,6 +2,7 @@ use super::{Expression, Rule}; use rustables_sys as sys; use std::os::raw::c_char; use std::ffi::CString; +use thiserror::Error; /// A Log expression will log all packets that match the rule. pub struct Log { @@ -35,6 +36,12 @@ impl Expression for Log { } } +#[derive(Error, Debug)] +pub enum LogPrefixError { + #[error("The log prefix string is more than 128 characters long")] + TooLongPrefix, + #[error("The log prefix string contains an invalid Nul character.")] + PrefixContainsANul(#[from] std::ffi::NulError) } @@ -47,13 +54,13 @@ pub struct LogGroup(pub u16); pub struct LogPrefix(pub CString); impl LogPrefix { - /// Create a new LogPrefix from a String. Converts it to CString as needed by nftables. - pub fn new(prefix: &str) -> Result<Self, std::ffi::NulError> { - // TODO check for prefix size constraints. - match CString::new(prefix) { - Ok(string) => Ok(LogPrefix(string)), - Err(error)=> Err(error) + /// Create a new LogPrefix from a String. Converts it to CString as needed by nftnl. Note + /// that LogPrefix should not be more than 127 characters long. + pub fn new(prefix: &str) -> Result<Self, LogPrefixError> { + if prefix.chars().count() > 127 { + return Err(LogPrefixError::TooLongPrefix) } + Ok(LogPrefix(CString::new(prefix)?)) } } |