Fix wasm_val_copy for null funcref/externref (#2041)

This commit fixes `Clone for wasm_val_t` to avoid attempting to chase a
null pointer. It also fixes the implementation for `FuncRef` values by
cloning their internal `wasm_ref_t` as well.
This commit is contained in:
Alex Crichton
2020-07-17 14:46:02 -05:00
committed by GitHub
parent 3aeab23bf1
commit fbc05faa49

View File

@@ -37,18 +37,19 @@ impl Drop for wasm_val_t {
impl Clone for wasm_val_t { impl Clone for wasm_val_t {
fn clone(&self) -> Self { fn clone(&self) -> Self {
match into_valtype(self.kind) { let mut ret = wasm_val_t {
ValType::ExternRef => wasm_val_t { kind: self.kind,
kind: self.kind, of: self.of,
of: wasm_val_union { };
ref_: unsafe { Box::into_raw(Box::new((*self.of.ref_).clone())) }, unsafe {
}, match into_valtype(self.kind) {
}, ValType::ExternRef | ValType::FuncRef if !self.of.ref_.is_null() => {
_ => wasm_val_t { ret.of.ref_ = Box::into_raw(Box::new((*self.of.ref_).clone()));
kind: self.kind, }
of: self.of, _ => {}
}, }
} }
return ret;
} }
} }
@@ -139,18 +140,7 @@ impl wasm_val_t {
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn wasm_val_copy(out: &mut MaybeUninit<wasm_val_t>, source: &wasm_val_t) { pub unsafe extern "C" fn wasm_val_copy(out: &mut MaybeUninit<wasm_val_t>, source: &wasm_val_t) {
crate::initialize( crate::initialize(out, source.clone());
out,
match into_valtype(source.kind) {
ValType::I32
| ValType::I64
| ValType::F32
| ValType::F64
| ValType::ExternRef
| ValType::FuncRef => source.clone(),
_ => unimplemented!("wasm_val_copy arg"),
},
);
} }
#[no_mangle] #[no_mangle]