rusty-cheddar icon indicating copy to clipboard operation
rusty-cheddar copied to clipboard

Dealing with NULL-able function pointers

Open jgallagher opened this issue 9 years ago • 4 comments

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.

jgallagher avatar Jan 21 '16 16:01 jgallagher

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.

Sean1708 avatar Jan 21 '16 22:01 Sean1708

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

jgallagher avatar Jan 22 '16 03:01 jgallagher

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.

kinetiknz avatar Mar 31 '17 03:03 kinetiknz

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?

rillian avatar Mar 31 '17 03:03 rillian