Fix memory leak in runtime interface (#4837)

* Fix memory leak in runtime interface

We used `slice::from_raw_parts` in runtime-interface which did not free
the memory afterwards. This pr changes it to `Vec::from_raw_parts` to
make sure `drop` is called properly and the values are freed.

* Check that `len` is non-zero

* Adds comment
This commit is contained in:
Bastian Köcher
2020-02-06 14:26:41 +01:00
committed by GitHub
parent 3c79e6d03c
commit c7867e5125
4 changed files with 68 additions and 12 deletions
@@ -32,7 +32,7 @@ use sp_wasm_interface::{FunctionContext, Pointer, Result};
use sp_std::{marker::PhantomData, convert::TryFrom};
#[cfg(not(feature = "std"))]
use sp_std::{slice, vec::Vec};
use sp_std::vec::Vec;
/// Derive macro for implementing [`PassBy`] with the [`Codec`] strategy.
///
@@ -255,8 +255,13 @@ impl<T: codec::Codec> PassByImpl<T> for Codec<T> {
let (ptr, len) = unpack_ptr_and_len(arg);
let len = len as usize;
let slice = unsafe { slice::from_raw_parts(ptr as *const u8, len) };
T::decode(&mut &slice[..]).expect("Host to wasm values are encoded correctly; qed")
let encoded = if len == 0 {
Vec::new()
} else {
unsafe { Vec::from_raw_parts(ptr as *mut u8, len, len) }
};
T::decode(&mut &encoded[..]).expect("Host to wasm values are encoded correctly; qed")
}
}