From: Philippe Antoine Date: Thu, 23 May 2024 12:05:08 +0000 (+0200) Subject: rust: return empty slice without using from_raw_parts X-Git-Tag: suricata-6.0.20~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=65733e2a93a7ac44c0a35724bf14a480703f5fcb;p=thirdparty%2Fsuricata.git rust: return empty slice without using from_raw_parts As this triggers rustc 1.78 unsafe precondition(s) violated: slice::from_raw_parts requires the pointer to be aligned and non-null, and the total size of the slice not to exceed `isize::MAX` Ticket: 7013 inspired from commit 5dc8dea8695786daec491a6655f99c0791e47f5c --- diff --git a/rust/src/applayer.rs b/rust/src/applayer.rs index 71d4e6e1b8..5cedff5574 100644 --- a/rust/src/applayer.rs +++ b/rust/src/applayer.rs @@ -263,7 +263,13 @@ pub struct RustParser { /// UNSAFE ! #[macro_export] macro_rules! build_slice { - ($buf:ident, $len:expr) => ( unsafe{ std::slice::from_raw_parts($buf, $len) } ); + ($buf:ident, $len:expr) => ( + if $buf.is_null() && $len == 0 { + &[] + } else { + unsafe{ std::slice::from_raw_parts($buf, $len) } + } + ); } /// Cast pointer to a variable, as a mutable reference to an object diff --git a/rust/src/nfs/nfs.rs b/rust/src/nfs/nfs.rs index 377b339846..581b9dc2c4 100644 --- a/rust/src/nfs/nfs.rs +++ b/rust/src/nfs/nfs.rs @@ -1471,7 +1471,7 @@ pub extern "C" fn rs_nfs_parse_request(flow: &mut Flow, _data: *mut std::os::raw::c_void) -> AppLayerResult { - let buf = unsafe{std::slice::from_raw_parts(input, input_len as usize)}; + let buf = build_slice!(input, input_len as usize); SCLogDebug!("parsing {} bytes of request data", input_len); state.update_ts(flow.get_last_time().as_secs()); @@ -1497,7 +1497,7 @@ pub extern "C" fn rs_nfs_parse_response(flow: &mut Flow, -> AppLayerResult { SCLogDebug!("parsing {} bytes of response data", input_len); - let buf = unsafe{std::slice::from_raw_parts(input, input_len as usize)}; + let buf = build_slice!(input, input_len as usize); state.update_ts(flow.get_last_time().as_secs()); state.parse_tcp_data_tc(buf) @@ -1522,7 +1522,7 @@ pub extern "C" fn rs_nfs_parse_request_udp(_flow: *mut Flow, _data: *mut std::os::raw::c_void) -> AppLayerResult { - let buf = unsafe{std::slice::from_raw_parts(input, input_len as usize)}; + let buf = build_slice!(input, input_len as usize); SCLogDebug!("parsing {} bytes of request data", input_len); state.parse_udp_ts(buf) } @@ -1537,7 +1537,7 @@ pub extern "C" fn rs_nfs_parse_response_udp(_flow: *mut Flow, -> AppLayerResult { SCLogDebug!("parsing {} bytes of response data", input_len); - let buf = unsafe{std::slice::from_raw_parts(input, input_len as usize)}; + let buf = build_slice!(input, input_len as usize); state.parse_udp_tc(buf) } diff --git a/rust/src/smb/smb.rs b/rust/src/smb/smb.rs index 4d665a47e3..0096c3384a 100644 --- a/rust/src/smb/smb.rs +++ b/rust/src/smb/smb.rs @@ -1924,7 +1924,7 @@ pub extern "C" fn rs_smb_parse_request_tcp(flow: &mut Flow, flags: u8) -> AppLayerResult { - let buf = unsafe{std::slice::from_raw_parts(input, input_len as usize)}; + let buf = build_slice!(input, input_len as usize); SCLogDebug!("parsing {} bytes of request data", input_len); /* START with MISTREAM set: record might be starting the middle. */ @@ -1957,7 +1957,7 @@ pub extern "C" fn rs_smb_parse_response_tcp(flow: &mut Flow, -> AppLayerResult { SCLogDebug!("parsing {} bytes of response data", input_len); - let buf = unsafe{std::slice::from_raw_parts(input, input_len as usize)}; + let buf = build_slice!(input, input_len as usize); /* START with MISTREAM set: record might be starting the middle. */ if flags & (STREAM_START|STREAM_MIDSTREAM) == (STREAM_START|STREAM_MIDSTREAM) {