Fix UB in impl VertexBufferCollection for Vec<Subbuffer<T>> (#2577)

This commit is contained in:
marc0246 2024-10-16 19:50:54 +02:00 committed by GitHub
parent 1da590f6a1
commit d1a9314c28
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,5 +1,5 @@
use crate::buffer::Subbuffer; use crate::buffer::Subbuffer;
use std::mem; use std::mem::{self, ManuallyDrop};
/// A collection of vertex buffers. /// A collection of vertex buffers.
pub trait VertexBuffersCollection { pub trait VertexBuffersCollection {
@ -32,8 +32,13 @@ impl<T: ?Sized> VertexBuffersCollection for Vec<Subbuffer<T>> {
mem::align_of::<Subbuffer<[u8]>>(), mem::align_of::<Subbuffer<[u8]>>(),
); );
let mut this = ManuallyDrop::new(self);
let cap = this.capacity();
let len = this.len();
let ptr = this.as_mut_ptr();
// SAFETY: All `Subbuffer`s share the same layout. // SAFETY: All `Subbuffer`s share the same layout.
unsafe { mem::transmute::<Vec<Subbuffer<T>>, Vec<Subbuffer<[u8]>>>(self) } unsafe { Vec::from_raw_parts(ptr.cast(), len, cap) }
} }
} }