Add a proper implementation of Clone for C API vector types

The previous implementation used a shallow copy, which is incorrect and
could lead to a use-after-free.
This commit is contained in:
Amanieu d'Antras
2021-12-02 11:51:02 +00:00
parent 1db2f38420
commit 6d61c1578f

View File

@@ -29,7 +29,6 @@ macro_rules! declare_vecs {
))*
) => {$(
#[repr(C)]
#[derive(Clone)]
pub struct $name {
size: usize,
data: *mut $elem_ty,
@@ -80,6 +79,12 @@ macro_rules! declare_vecs {
}
}
impl Clone for $name {
fn clone(&self) -> Self {
self.as_slice().to_vec().into()
}
}
impl From<Vec<$elem_ty>> for $name {
fn from(vec: Vec<$elem_ty>) -> Self {
let mut vec = vec.into_boxed_slice();