Skip to content

Commit

Permalink
[fix] #3928: Fix double free in wasm tests
Browse files Browse the repository at this point in the history
The `log` and `dbg` functions do not take the pointer ownership, but their mock versions used for testing did

Signed-off-by: Nikita Strygin <[email protected]>
  • Loading branch information
DCNick3 committed Sep 29, 2023
1 parent aacdbab commit 4b9d8ac
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
8 changes: 5 additions & 3 deletions wasm/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>(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]
Expand Down
5 changes: 3 additions & 2 deletions wasm/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,16 @@ mod tests {
use webassembly_test::webassembly_test;

use super::*;
use crate::_decode_from_raw;

fn get_log_message() -> &'static str {
"log_message"
}

#[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());
}
Expand Down

0 comments on commit 4b9d8ac

Please sign in to comment.