diff --git a/CHANGELOG.md b/CHANGELOG.md index 5717edba5aa..1babe0f2c40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ ### Fixed +- [#1855](https://github.com/wasmerio/wasmer/pull/1855) Fix memory leak when using `wat2wasm` in the C API, the function now takes its output parameter by pointer rather than returning an allocated `wasm_byte_vec_t`. - [#1841](https://github.com/wasmerio/wasmer/pull/1841) We will now panic when attempting to use a native function with a captured env as a host function. Previously this would silently do the wrong thing. See [#1840](https://github.com/wasmerio/wasmer/pull/1840) for info about Wasmer's support of closures as host functions. - [#1764](https://github.com/wasmerio/wasmer/pull/1764) Fix bug in WASI `path_rename` allowing renamed files to be 1 directory below a preopened directory. diff --git a/lib/c-api/src/wasm_c_api/macros.rs b/lib/c-api/src/wasm_c_api/macros.rs index ffd95b701e9..c425d985da7 100644 --- a/lib/c-api/src/wasm_c_api/macros.rs +++ b/lib/c-api/src/wasm_c_api/macros.rs @@ -102,12 +102,13 @@ macro_rules! wasm_declare_vec { #[no_mangle] - pub unsafe extern "C" fn [](ptr: *mut []) { - 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 [](ptr: Option<&mut []>) { + 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; + } } } } diff --git a/lib/c-api/src/wasm_c_api/wat.rs b/lib/c-api/src/wasm_c_api/wat.rs index 185b61e91c0..fdfcc7e45cd 100644 --- a/lib/c-api/src/wasm_c_api/wat.rs +++ b/lib/c-api/src/wasm_c_api/wat.rs @@ -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> { - 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)] diff --git a/lib/c-api/wasmer_wasm.h b/lib/c-api/wasmer_wasm.h index 3e2916c21bb..62a2fd4f858 100644 --- a/lib/c-api/wasmer_wasm.h +++ b/lib/c-api/wasmer_wasm.h @@ -239,6 +239,6 @@ int wasmer_last_error_message(char *buffer, int length); * * In case of failure, `wat2wasm` returns `NULL`. */ -wasm_byte_vec_t *wat2wasm(const wasm_byte_vec_t *wat); +void wat2wasm(const wasm_byte_vec_t *wat, wasm_byte_vec_t *out); #endif /* WASMER_WASM_H */