From ee0f05f9a1dbcba4a4ac33cab2fe87ad5541fa02 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 10 Feb 2020 11:31:54 +0100 Subject: [PATCH] feat(runtime-c-api) `wasmer_memory_data_length` returns 0 if `memory` is NULL. --- lib/runtime-c-api/src/memory.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/runtime-c-api/src/memory.rs b/lib/runtime-c-api/src/memory.rs index 74bb82fe8..f8fd581da 100644 --- a/lib/runtime-c-api/src/memory.rs +++ b/lib/runtime-c-api/src/memory.rs @@ -198,10 +198,15 @@ pub extern "C" fn wasmer_memory_data(memory: *const wasmer_memory_t) -> *mut u8 /// ``` #[allow(clippy::cast_ptr_alignment)] #[no_mangle] -pub extern "C" fn wasmer_memory_data_length(mem: *mut wasmer_memory_t) -> u32 { - let memory = mem as *mut Memory; - let Bytes(len) = unsafe { (*memory).size().bytes() }; - len as u32 +pub extern "C" fn wasmer_memory_data_length(memory: *mut wasmer_memory_t) -> u32 { + if memory.is_null() { + return 0; + } + + let memory = unsafe { &*(memory as *const Memory) }; + let Bytes(length) = memory.size().bytes(); + + length as u32 } /// Frees memory for the given Memory