From 4b9d8acc2f3c6506c72ed7a7f8a0ef19f492007f Mon Sep 17 00:00:00 2001 From: Nikita Strygin Date: Thu, 28 Sep 2023 15:31:04 +0300 Subject: [PATCH] [fix] #3928: Fix double free in wasm tests The `log` and `dbg` functions do not take the pointer ownership, but their mock versions used for testing did Signed-off-by: Nikita Strygin --- wasm/src/debug.rs | 8 +++++--- wasm/src/log.rs | 5 +++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/wasm/src/debug.rs b/wasm/src/debug.rs index 0f81f10336d..ed88fa048f1 100644 --- a/wasm/src/debug.rs +++ b/wasm/src/debug.rs @@ -169,15 +169,17 @@ mod tests { use webassembly_test::webassembly_test; - use crate::_decode_from_raw; - fn get_dbg_message() -> &'static str { "dbg_message" } #[no_mangle] pub unsafe extern "C" fn _dbg_mock(ptr: *const u8, len: usize) { - assert_eq!(_decode_from_raw::(ptr, len), get_dbg_message()); + use parity_scale_codec::DecodeAll; + + // can't use _decode_from_raw here, because we must NOT take the ownership + let bytes = core::slice::from_raw_parts(ptr, len); + assert_eq!(String::decode_all(&mut &*bytes).unwrap(), get_dbg_message()); } #[webassembly_test] diff --git a/wasm/src/log.rs b/wasm/src/log.rs index 30cf22d3c58..2adcfede7f2 100644 --- a/wasm/src/log.rs +++ b/wasm/src/log.rs @@ -88,7 +88,6 @@ mod tests { use webassembly_test::webassembly_test; use super::*; - use crate::_decode_from_raw; fn get_log_message() -> &'static str { "log_message" @@ -96,7 +95,9 @@ mod tests { #[no_mangle] pub unsafe extern "C" fn _log_mock(ptr: *const u8, len: usize) { - let (log_level, msg) = _decode_from_raw::<(u8, String)>(ptr, len); + // can't use _decode_from_raw here, because we must NOT take the ownership + let bytes = core::slice::from_raw_parts(ptr, len); + let (log_level, msg) = <(u8, String)>::decode_all(&mut &*bytes).unwrap(); assert_eq!(log_level, 3); assert_eq!(msg, get_log_message()); }