From a5b5a0b18f160278f7e05155ccf5f2f089d38d8a Mon Sep 17 00:00:00 2001 From: Sam Privett Date: Sun, 22 Sep 2024 13:18:36 -0700 Subject: [PATCH 1/2] 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) } + } } } From 54180dc8018ac4172ac5285553880a4f7ccc6fb6 Mon Sep 17 00:00:00 2001 From: Sam Privett Date: Mon, 23 Sep 2024 17:47:23 -0700 Subject: [PATCH 2/2] Added missing testcase --- rosidl_runtime_rs/src/sequence.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/rosidl_runtime_rs/src/sequence.rs b/rosidl_runtime_rs/src/sequence.rs index 4d8a962b8..3d7cd5832 100644 --- a/rosidl_runtime_rs/src/sequence.rs +++ b/rosidl_runtime_rs/src/sequence.rs @@ -674,6 +674,12 @@ mod tests { } } + #[test] + fn test_empty_sequence() { + assert!(Sequence::::default().is_empty()); + assert!(BoundedSequence::::default().is_empty()); + } + quickcheck! { fn test_extend(xs: Vec, ys: Vec) -> bool { let mut xs_seq = Sequence::new(xs.len());