From 8aa08225cd5575bb6a590bdf11dcfd0ceb772203 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 17 Dec 2020 14:55:29 +0100 Subject: [PATCH] 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>`, otherwise it will crash. --- lib/c-api/src/wasm_c_api/macros.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/c-api/src/wasm_c_api/macros.rs b/lib/c-api/src/wasm_c_api/macros.rs index 07a99b01545..8132f5cc5c2 100644 --- a/lib/c-api/src/wasm_c_api/macros.rs +++ b/lib/c-api/src/wasm_c_api/macros.rs @@ -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 []> = Vec::from_raw_parts(vec.data, vec.size, vec.size); - let _data: Vec]>> = ::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]>> = ::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... - } }; }