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:
@@ -29,7 +29,6 @@ macro_rules! declare_vecs {
|
|||||||
))*
|
))*
|
||||||
) => {$(
|
) => {$(
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct $name {
|
pub struct $name {
|
||||||
size: usize,
|
size: usize,
|
||||||
data: *mut $elem_ty,
|
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 {
|
impl From<Vec<$elem_ty>> for $name {
|
||||||
fn from(vec: Vec<$elem_ty>) -> Self {
|
fn from(vec: Vec<$elem_ty>) -> Self {
|
||||||
let mut vec = vec.into_boxed_slice();
|
let mut vec = vec.into_boxed_slice();
|
||||||
|
|||||||
Reference in New Issue
Block a user