Skip to content

Commit 9db073b

Browse files
DoeringChristianDöring Christian
andauthored
Fix: cast caused double free (use mem::forget) (#105)
Co-authored-by: Döring Christian <christian.doering@ŧum.de>
1 parent 6a6dcda commit 9db073b

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

crates/cust/src/memory/device/device_buffer.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ impl<A: DeviceCopy + Pod> DeviceBuffer<A> {
314314
/// whole number of elements. Such as `3` x [`u16`] -> `1.5` x [`u32`].
315315
/// - If either type is a ZST (but not both).
316316
#[cfg_attr(docsrs, doc(cfg(feature = "bytemuck")))]
317-
pub fn try_cast<B: Pod + DeviceCopy>(self) -> Result<DeviceBuffer<B>, PodCastError> {
317+
pub fn try_cast<B: Pod + DeviceCopy>(mut self) -> Result<DeviceBuffer<B>, PodCastError> {
318318
if align_of::<B>() > align_of::<A>() && (self.buf.as_raw() as usize) % align_of::<B>() != 0
319319
{
320320
Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
@@ -325,10 +325,12 @@ impl<A: DeviceCopy + Pod> DeviceBuffer<A> {
325325
Err(PodCastError::SizeMismatch)
326326
} else if (size_of::<A>() * self.len) % size_of::<B>() == 0 {
327327
let new_len = (size_of::<A>() * self.len) / size_of::<B>();
328-
Ok(DeviceBuffer {
328+
let ret = Ok(DeviceBuffer {
329329
buf: self.buf.cast(),
330330
len: new_len,
331-
})
331+
});
332+
unsafe{std::mem::forget(self);}
333+
ret
332334
} else {
333335
Err(PodCastError::OutputSliceWouldHaveSlop)
334336
}

0 commit comments

Comments
 (0)