Skip to content

Commit

Permalink
feat(c-api) wasm_$name_vec_delete checks the vec is initialized.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Hywan committed Dec 17, 2020
1 parent 940dea7 commit 8aa0822
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/c-api/src/wasm_c_api/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -299,7 +306,6 @@ macro_rules! wasm_declare_ref_base {
}

// TODO: finish this...

}
};
}
Expand Down

0 comments on commit 8aa0822

Please sign in to comment.