Skip to content

Commit

Permalink
Fix memory leak in wat2wasm function
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark McCaskey committed Dec 1, 2020
1 parent 7a45d7d commit 42af38d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
13 changes: 7 additions & 6 deletions lib/c-api/src/wasm_c_api/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,13 @@ macro_rules! wasm_declare_vec {


#[no_mangle]
pub unsafe extern "C" fn [<wasm_ $name _vec_delete>](ptr: *mut [<wasm_ $name _vec_t>]) {
let vec = &mut *ptr;
if !vec.data.is_null() {
Vec::from_raw_parts(vec.data, vec.size, vec.size);
vec.data = ::std::ptr::null_mut();
vec.size = 0;
pub unsafe extern "C" fn [<wasm_ $name _vec_delete>](ptr: Option<&mut [<wasm_ $name _vec_t>]>) {
if let Some(vec) = ptr {
if !vec.data.is_null() {
Vec::from_raw_parts(vec.data, vec.size, vec.size);
vec.data = ::std::ptr::null_mut();
vec.size = 0;
}
}
}
}
Expand Down
17 changes: 13 additions & 4 deletions lib/c-api/src/wasm_c_api/wat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@ use super::types::wasm_byte_vec_t;
/// In case of failure, `wat2wasm` returns `NULL`.
#[cfg(feature = "wat")]
#[no_mangle]
pub unsafe extern "C" fn wat2wasm(wat: &wasm_byte_vec_t) -> Option<Box<wasm_byte_vec_t>> {
let wat: &[u8] = wat.into_slice()?;
let result: wasm_byte_vec_t = c_try!(wasmer::wat2wasm(wat)).into_owned().into();
pub unsafe extern "C" fn wat2wasm(wat: &wasm_byte_vec_t, out: &mut wasm_byte_vec_t) {
let wat: &[u8] = match wat.into_slice() {
Some(v) => v,
_ => return,
};
let result: wasm_byte_vec_t = match wasmer::wat2wasm(wat) {
Ok(val) => val.into_owned().into(),
Err(err) => {
crate::error::update_last_error(err);
return;
}
};

Some(Box::new(result))
*out = result;
}

#[cfg(test)]
Expand Down

0 comments on commit 42af38d

Please sign in to comment.