feat(c-api) wasm_$name_vec_delete checks the vec is initialized.

In case of a boxed vector, `wasm_$name_vec_delete` now checks that the
vec is correctly initialized (by checking the first item only) because
transmuting `Vec<*mut T>` to `Vec<Box<T>>`, otherwise it will crash.
This commit is contained in:
Ivan Enderlin
2020-12-17 14:55:29 +01:00
parent 940dea72e7
commit 8aa08225cd

View File

@@ -274,7 +274,14 @@ Read the documentation of [`wasm_" $name "_t`] to see more concrete examples."]
let vec = &mut *ptr;
if !vec.data.is_null() {
let data: Vec<*mut [<wasm_ $name _t>]> = Vec::from_raw_parts(vec.data, vec.size, vec.size);
let _data: Vec<Box<[<wasm_ $name _t>]>> = ::std::mem::transmute(data);
// If the vector has been initialized (we check
// only the first item), we can transmute items to
// `Box`es.
if vec.size > 0 && !data[0].is_null() {
let _data: Vec<Box<[<wasm_ $name _t>]>> = ::std::mem::transmute(data);
}
vec.data = ::std::ptr::null_mut();
vec.size = 0;
}
@@ -299,7 +306,6 @@ macro_rules! wasm_declare_ref_base {
}
// TODO: finish this...
}
};
}