]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
rust: return empty slice without using from_raw_parts
authorPhilippe Antoine <pantoine@oisf.net>
Thu, 23 May 2024 12:05:08 +0000 (14:05 +0200)
committerPhilippe Antoine <pantoine@oisf.net>
Thu, 23 May 2024 20:14:00 +0000 (22:14 +0200)
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

rust/src/applayer.rs
rust/src/nfs/nfs.rs
rust/src/smb/smb.rs

index 71d4e6e1b833111cc443ef029120ac13272f5610..5cedff55741f38758b2156876fc6fb79966d533f 100644 (file)
@@ -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
index 377b33984669ca39ac7c2c60dcb7f3f88c99aabf..581b9dc2c419545cdaf7340b29100a5fb07d6f68 100644 (file)
@@ -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)
 }
 
index 4d665a47e321ccff9f670da2cc6c857b6f209bf0..0096c3384aeb6cb3868a787bfbbd97461b21c916 100644 (file)
@@ -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) {