Skip to content

Commit

Permalink
Merge pull request #5156 from wasmerio/c-api-doublefree
Browse files Browse the repository at this point in the history
Resolve double-free error in c-api
  • Loading branch information
syrusakbary authored Oct 18, 2024
2 parents 9b8dcf8 + c978574 commit 8b97cfe
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/api/third_party/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
wamr
2 changes: 1 addition & 1 deletion lib/c-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,4 @@ jit = ["compiler"]
#emscripten = ["wasmer-emscripten"]

[build-dependencies]
cbindgen = { version = "0.24", default-features = false }
cbindgen = { version = "0.27", default-features = false }
21 changes: 17 additions & 4 deletions lib/c-api/src/wasm_c_api/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,24 @@ pub unsafe extern "C" fn wasm_val_copy(
}); otherwise ());
}

impl Drop for wasm_val_t {
fn drop(&mut self) {
let kind: Result<wasm_valkind_enum, _> = self.kind.try_into();
match kind {
Ok(wasm_valkind_enum::WASM_EXTERNREF) | Ok(wasm_valkind_enum::WASM_FUNCREF) => unsafe {
if !self.of.wref.is_null() {
drop(Box::from_raw(self.of.wref));
}
},
_ => {}
}
}
}

#[no_mangle]
pub unsafe extern "C" fn wasm_val_delete(val: Option<Box<wasm_val_t>>) {
if let Some(val) = val {
// TODO: figure out where wasm_val is allocated first...
let _ = val;
pub unsafe extern "C" fn wasm_val_delete(val: *mut wasm_val_t) {
if !val.is_null() {
std::ptr::drop_in_place(val);
}
}

Expand Down

0 comments on commit 8b97cfe

Please sign in to comment.