From a5b5a0b18f160278f7e05155ccf5f2f089d38d8a Mon Sep 17 00:00:00 2001 From: Sam Privett Date: Sun, 22 Sep 2024 13:18:36 -0700 Subject: [PATCH] Add in missing nullptr check when calling `std::slice::from_raw_parts` --- rosidl_runtime_rs/src/sequence.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/rosidl_runtime_rs/src/sequence.rs b/rosidl_runtime_rs/src/sequence.rs index 22fbfbae5..4d8a962b8 100644 --- a/rosidl_runtime_rs/src/sequence.rs +++ b/rosidl_runtime_rs/src/sequence.rs @@ -261,18 +261,26 @@ where /// /// Equivalent to `&seq[..]`. pub fn as_slice(&self) -> &[T] { - // SAFETY: self.data points to self.size consecutive, initialized elements and - // isn't modified externally. - unsafe { std::slice::from_raw_parts(self.data, self.size) } + if self.data.is_null() { + &[] + } else { + // SAFETY: self.data is not null and points to self.size consecutive, + // initialized elements and isn't modified externally. + unsafe { std::slice::from_raw_parts(self.data, self.size) } + } } /// Extracts a mutable slice containing the entire sequence. /// /// Equivalent to `&mut seq[..]`. pub fn as_mut_slice(&mut self) -> &mut [T] { - // SAFETY: self.data points to self.size consecutive, initialized elements and - // isn't modified externally. - unsafe { std::slice::from_raw_parts_mut(self.data, self.size) } + if self.data.is_null() { + &mut [] + } else { + // SAFETY: self.data is not null and points to self.size consecutive, + // initialized elements and isn't modified externally. + unsafe { std::slice::from_raw_parts_mut(self.data, self.size) } + } } }