Skip to content
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

Fix: cast caused double free (use mem::forget) #105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions crates/cust/src/memory/device/device_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ impl<A: DeviceCopy + Pod> DeviceBuffer<A> {
/// whole number of elements. Such as `3` x [`u16`] -> `1.5` x [`u32`].
/// - If either type is a ZST (but not both).
#[cfg_attr(docsrs, doc(cfg(feature = "bytemuck")))]
pub fn try_cast<B: Pod + DeviceCopy>(self) -> Result<DeviceBuffer<B>, PodCastError> {
pub fn try_cast<B: Pod + DeviceCopy>(mut self) -> Result<DeviceBuffer<B>, PodCastError> {
if align_of::<B>() > align_of::<A>() && (self.buf.as_raw() as usize) % align_of::<B>() != 0
{
Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
Expand All @@ -325,10 +325,12 @@ impl<A: DeviceCopy + Pod> DeviceBuffer<A> {
Err(PodCastError::SizeMismatch)
} else if (size_of::<A>() * self.len) % size_of::<B>() == 0 {
let new_len = (size_of::<A>() * self.len) / size_of::<B>();
Ok(DeviceBuffer {
let ret = Ok(DeviceBuffer {
buf: self.buf.cast(),
len: new_len,
})
});
unsafe{std::mem::forget(self);}
ret
} else {
Err(PodCastError::OutputSliceWouldHaveSlop)
}
Expand Down