linksplatform / doublets-rs

The Unlicense
5 stars 2 forks source link

Wrap all exported functions to catch and unwind #2

Open uselessgoddess opened 2 years ago

uselessgoddess commented 2 years ago

All panics must be catch and unwind otherwise it is UB. I recommend use catch_unwind with the following if let:

let result = panic::catch_unwind(|| {
    // ffi function call
});

if let Err(err) = result {
    // if `err` panic in `Drop` we will be sad
    forget(err);
}

You can create macro or function to resolve it In currently implementation:

#[ffi::specialize_for(
    . . .
)]
unsafe fn drop_links<T: LinkType>(this: *mut c_void) {
    let links: &mut WrappedLinks<T> = unnull_or_panic(this);
    drop_in_place(links);
}

We can split to:

unsafe fn drop_links_impl<T: LinkType>(this: *mut c_void) {
    // impl
}

#[ffi::specialize_for(
    . . .
)]
unsafe fn drop_links<T: LinkType>(this: *mut c_void) {
    catch_unwind(/* some */)
}

Or add this behavior to ffi::specialize_for

uselessgoddess commented 2 years ago

Also check out unwind_api RFC – advanced

uselessgoddess commented 2 years ago

In my new PR(#10) I try use log-panics crate