Sean1708 / rusty-cheddar

A Rust crate for automatically generating C header files from Rust source file.
http://sean1708.github.io/rusty-cheddar/
191 stars 25 forks source link

Dealing with NULL-able function pointers #30

Open jgallagher opened 8 years ago

jgallagher commented 8 years ago

If I declare the following struct in Rust:

#[repr(C)]
pub struct foo {
    bar: extern "C" fn(blah: *mut libc::c_void)
}

rusty-cheddar (correctly) generates this in its header:

typedef struct foo {
    void (*bar)(void *blah);
} foo;

C code is able to set bar to NULL, but on the Rust side, there's no way to check for that. It appears the expected way of handling this (see here or here) is to change the type on the Rust side to an Option:

#[repr(C)]
pub struct foo {
    bar: Option<extern "C" fn(blah: *mut libc::c_void)>
}

but rusty-cheddar does not handle this appropriately.

Sean1708 commented 8 years ago

Yep this seems like a good course of action, thanks. It's gonna take a bit of work so I can't promise I'll have a fix out as quickly as last time though.

jgallagher commented 8 years ago

No worries. I love that this project exists; thanks for making it! I figured this one would be a bit more work. :)

kinetiknz commented 7 years ago

FWIW, I've added support for this in https://github.com/kinetiknz/rusty-cheddar/commit/bb819e501c9031ce62e409029c4ea5b2c736b633.

I assume @Sean1708 won't want to merge it because rusty-cheddar is being replaced with rusty-binder, but we (https://github.com/mozilla/mp4parse-rust) need this to fix a UB bug (https://github.com/rust-lang/rust/issues/40913), so I went ahead and hacked something up.

rillian commented 7 years ago

We could still benefit from deploying this fix while rusty-binder is under development. @Sean1708, if you're not interested in merging, would you be willing to add one of us to https://crates.io/crates/rusty-cheddar so we can make releases in the meantime?