Skip to content

Commit 32aff04

Browse files
committed
rust/debug: use functions from ffi crate where possible
Its not possible to use all the functions and macros from the ffi crate in the main Suricata crate, as there are conditionals around when running in test mode, and "cargo test" doesn't propagate the "cfg(test)" to test crates. Which for now means duplicating the macros and some functions.
1 parent 9ec339b commit 32aff04

3 files changed

Lines changed: 7 additions & 34 deletions

File tree

rust/Cargo.lock.in

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/Cargo.toml.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ time = "=0.3.41"
8787

8888
suricata-derive = { path = "./derive", version = "@PACKAGE_VERSION@" }
8989
suricata-sys = { path = "./sys", version = "@PACKAGE_VERSION@" }
90+
suricata-ffi = { path = "./ffi", version = "@PACKAGE_VERSION@" }
9091

9192
suricata-lua-sys = { version = "5.4.8002" }
9293

rust/src/debug.rs

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717

1818
//! Logging and debug utilities, like util-debug.c.
1919
20-
use std::{ffi::CString, path::Path};
20+
use std::path::Path;
2121

22-
use suricata_sys::sys::{SCFatalErrorOnInitStatic, SCLogLevel};
22+
use suricata_sys::sys::SCLogLevel;
2323
#[cfg(not(test))]
24-
use suricata_sys::sys::{SCError, SCLogMessage};
24+
use suricata_sys::sys::SCError;
2525

2626
pub static mut LEVEL: SCLogLevel = SCLogLevel::SC_LOG_NOTSET;
2727

@@ -44,12 +44,6 @@ fn basename(filename: &str) -> &str {
4444
return filename;
4545
}
4646

47-
pub fn fatalerror(message: &str) {
48-
unsafe {
49-
SCFatalErrorOnInitStatic(to_safe_cstring(message).as_ptr());
50-
}
51-
}
52-
5347
pub fn sclog(level: SCLogLevel, file: &str, line: u32, function: &str, message: &str) {
5448
let filename = basename(file);
5549
let noext = &filename[0..filename.len() - 3];
@@ -64,16 +58,7 @@ pub fn sc_log_message(
6458
level: SCLogLevel, filename: &str, line: std::os::raw::c_uint, function: &str, module: &str,
6559
message: &str,
6660
) -> SCError {
67-
unsafe {
68-
return SCLogMessage(
69-
level,
70-
to_safe_cstring(filename).as_ptr(),
71-
line,
72-
to_safe_cstring(function).as_ptr(),
73-
to_safe_cstring(module).as_ptr(),
74-
to_safe_cstring(message).as_ptr(),
75-
);
76-
}
61+
suricata_ffi::debug::log_message(level, filename, line, function, module, message)
7762
}
7863

7964
#[cfg(test)]
@@ -92,20 +77,6 @@ pub fn sc_log_message(
9277
return 0;
9378
}
9479

95-
// Convert a &str into a CString by first stripping NUL bytes.
96-
fn to_safe_cstring(val: &str) -> CString {
97-
let mut safe = Vec::with_capacity(val.len());
98-
for c in val.as_bytes() {
99-
if *c != 0 {
100-
safe.push(*c);
101-
}
102-
}
103-
match CString::new(safe) {
104-
Ok(cstr) => cstr,
105-
_ => CString::new("<failed to encode string>").unwrap(),
106-
}
107-
}
108-
10980
// This macro returns the function name.
11081
//
11182
// This macro has been borrowed from https://github.com/popzxc/stdext-rs, which
@@ -199,7 +170,7 @@ macro_rules! SCLogDebug {
199170
#[macro_export]
200171
macro_rules!SCFatalErrorOnInit {
201172
($($arg:tt)*) => {
202-
$crate::debug::fatalerror(&format!($($arg)*));
173+
suricata_ffi::debug::fatalerror(&format!($($arg)*));
203174
}
204175
}
205176

0 commit comments

Comments
 (0)