Skip to content

Commit

Permalink
Fix potential UB in AlignedVec::into_boxed_slice
Browse files Browse the repository at this point in the history
  • Loading branch information
djkoloski committed Mar 3, 2022
1 parent f552fab commit a941719
Showing 1 changed file with 2 additions and 7 deletions.
9 changes: 2 additions & 7 deletions rkyv/src/util/aligned_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,13 +509,8 @@ impl AlignedVec {
/// assert_eq!(slice.len(), 3);
/// ```
#[inline]
pub fn into_boxed_slice(mut self) -> Box<[u8]> {
unsafe {
self.shrink_to_fit();
let slice_ptr = ptr_meta::from_raw_parts_mut(self.as_mut_ptr().cast(), self.len);
core::mem::forget(self);
Box::from_raw(slice_ptr)
}
pub fn into_boxed_slice(self) -> Box<[u8]> {
self.into_vec().into_boxed_slice()
}

/// Converts the vector into `Vec<u8>`.
Expand Down

3 comments on commit a941719

@RalfJung
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't the new implementation violate the doc comment of into_boxed_slice?

This will preserve the alignment guarantees provided by AlignedVec.

@djkoloski
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, my bad. I was rushing to get this out the door. I'll patch that up.

@djkoloski
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0.7.35.

Please sign in to comment.