diff --git a/rust/utils/socket_ipc_rust_ffi/src/stream_buffer.rs b/rust/utils/socket_ipc_rust_ffi/src/stream_buffer.rs index b7ac6a3cab5069a34db039402c421a5a452f60da..71695c943e7e2d0b659c9461d4214c44788eb285 100644 --- a/rust/utils/socket_ipc_rust_ffi/src/stream_buffer.rs +++ b/rust/utils/socket_ipc_rust_ffi/src/stream_buffer.rs @@ -163,10 +163,12 @@ impl StreamBuffer { } } fn get_error_status_remark(&self) -> *const c_char { - let s: &[c_char] = match self.rw_error_status { - ErrorStatus::Ok => b"OK\0", - ErrorStatus::Read => b"READ_ERROR\0", - ErrorStatus::Write => b"WRITE_ERROR\0", + // Creating a new C-compatible string will never fail, + // because the supplied bytes always contain greater than 0. + let s: CString = match self.rw_error_status { + ErrorStatus::Ok => CString::new("OK").unwrap_or_default(), + ErrorStatus::Read => CString::new("READ_ERROR").unwrap_or_default(), + ErrorStatus::Write => CString::new("WRITE_ERROR").unwrap_or_default(), }; s.as_ptr() } diff --git a/rust/utils/socket_ipc_rust_ffi/src/stream_buffer/ffi.rs b/rust/utils/socket_ipc_rust_ffi/src/stream_buffer/ffi.rs index a56ef5dc5bfed0dde1f8edaf18a5a55d5b93a72c..a72de8fd503959e33d74e7d80d32f112342d966b 100644 --- a/rust/utils/socket_ipc_rust_ffi/src/stream_buffer/ffi.rs +++ b/rust/utils/socket_ipc_rust_ffi/src/stream_buffer/ffi.rs @@ -176,6 +176,7 @@ pub unsafe extern "C" fn StreamBufferChkRWError(object: *const StreamBuffer) -> pub unsafe extern "C" fn StreamBufferGetErrorStatusRemark(object: *const StreamBuffer) -> *const c_char { info!(LOG_LABEL, "enter StreamBufferGetErrorStatusRemark"); if let Some(obj) = StreamBuffer::as_ref(object) { + // SAFETY: The Rust side creates a CString string and this function should be called only here obj.get_error_status_remark() } else { std::ptr::null() diff --git a/utils/ipc/include/stream_socket.h b/utils/ipc/include/stream_socket.h index 9eee37fd65bc6aae2315711fb0e2eb584a186749..6cc36778aa7ee1a5d528c7d3b5990708cd184cf4 100644 --- a/utils/ipc/include/stream_socket.h +++ b/utils/ipc/include/stream_socket.h @@ -45,7 +45,8 @@ public: protected: #ifdef OHOS_BUILD_ENABLE_RUST struct RustDelete { - void operator() (RustStreamSocket* raw) { + void operator() (RustStreamSocket* raw) + { StreamSocketDelete(raw); } }; @@ -57,4 +58,4 @@ protected: }; } // namespace Sensors } // namespace OHOS -#endif // STREAM_SOCKET_H \ No newline at end of file +#endif // STREAM_SOCKET_H