-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add in missing nullptr check when calling std::slice::from_raw_parts
#416
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, now that I'm looking at this, I'm not sure this would work. If someone started pushing to this mutable slice There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to write a unit test for this scenario? Just to make sure that it's working, and also as a warning sign in case future changes cause it to break? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Good point, added a unit test for the base cases. But also, just digging around the slice API, doesn't seem like this is really a concern to mutate the slice as its empty, and the only mutation that can be done will be with the fixed size. i.e.
|
||
} 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) } | ||
} | ||
} | ||
} | ||
|
||
|
@@ -666,6 +674,12 @@ mod tests { | |
} | ||
} | ||
|
||
#[test] | ||
fn test_empty_sequence() { | ||
assert!(Sequence::<i32>::default().is_empty()); | ||
assert!(BoundedSequence::<i32, 5>::default().is_empty()); | ||
} | ||
|
||
quickcheck! { | ||
fn test_extend(xs: Vec<i32>, ys: Vec<i32>) -> bool { | ||
let mut xs_seq = Sequence::new(xs.len()); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Taken from this example in the stdlib
https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html#ffi-handling-null-pointers