From: Jason Ish Date: Wed, 21 Jun 2017 15:06:22 +0000 (-0600) Subject: rust: for sclog*, strip nul bytes before logging X-Git-Tag: suricata-4.0.0-rc1~24 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f5a90e26a91d030e641900c60372629a057008e4;p=thirdparty%2Fsuricata.git rust: for sclog*, strip nul bytes before logging --- diff --git a/rust/src/log.rs b/rust/src/log.rs index ec09f812a2..018888ca5f 100644 --- a/rust/src/log.rs +++ b/rust/src/log.rs @@ -144,25 +144,13 @@ pub fn sc_log_message(level: Level, { unsafe { if let Some(c) = SC { - let filename = match CString::new(filename) { - Ok(filename) => filename, - Err(_) => CString::new("").unwrap() - }; - let function = match CString::new(function) { - Ok(function) => function, - Err(_) => CString::new("").unwrap() - }; - let message = match CString::new(message) { - Ok(val) => val, - Err(_) => CString::new("").unwrap() - }; return (c.SCLogMessage)( level as i32, - filename.as_ptr(), + to_safe_cstring(filename).as_ptr(), line, - function.as_ptr(), + to_safe_cstring(function).as_ptr(), code, - message.as_ptr()); + to_safe_cstring(message).as_ptr()); } } @@ -176,3 +164,19 @@ pub fn sc_log_message(level: Level, println!("{}:{} <{:?}> -- {}", filename, line, level, message); return 0; } + +// Convert a &str into a CString by first stripping NUL bytes. +fn to_safe_cstring(val: &str) -> CString { + let mut safe = Vec::with_capacity(val.len()); + for c in val.as_bytes() { + if *c != 0 { + safe.push(*c); + } + } + match CString::new(safe) { + Ok(cstr) => cstr, + _ => { + CString::new("").unwrap() + } + } +}