From b41e918ec3b59e56bf840d1bf7461bb054663db0 Mon Sep 17 00:00:00 2001 From: Yury Delendik Date: Wed, 25 Sep 2019 17:01:00 -0500 Subject: [PATCH] [wasmtime-api] Fixes wasm_exporttype_type leak --- wasmtime-api/src/wasm.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/wasmtime-api/src/wasm.rs b/wasmtime-api/src/wasm.rs index 9f56eaefeb..3c86d615a3 100644 --- a/wasmtime-api/src/wasm.rs +++ b/wasmtime-api/src/wasm.rs @@ -259,6 +259,7 @@ declare_vec!(wasm_importtype_vec_t, *mut wasm_importtype_t); pub struct wasm_exporttype_t { ty: ExportType, name_cache: Option, + type_cache: Option, } declare_vec!(wasm_exporttype_vec_t, *mut wasm_exporttype_t); @@ -711,6 +712,7 @@ pub unsafe extern "C" fn wasm_module_new( .map(|e| wasm_exporttype_t { ty: e.clone(), name_cache: None, + type_cache: None, }) .collect::>(); let module = Box::new(wasm_module_t { @@ -903,11 +905,14 @@ pub unsafe extern "C" fn wasm_exporttype_name(et: *const wasm_exporttype_t) -> * pub unsafe extern "C" fn wasm_exporttype_type( et: *const wasm_exporttype_t, ) -> *const wasm_externtype_t { - let ty = Box::new(wasm_externtype_t { - ty: (*et).ty.r#type().clone(), - cache: wasm_externtype_t_type_cache::Empty, - }); - Box::into_raw(ty) + if (*et).type_cache.is_none() { + let et = (et as *mut wasm_exporttype_t).as_mut().unwrap(); + et.type_cache = Some(wasm_externtype_t { + ty: (*et).ty.r#type().clone(), + cache: wasm_externtype_t_type_cache::Empty, + }); + } + (*et).type_cache.as_ref().unwrap() } #[no_mangle]