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