Fix CFRunLoop types to avoid UB

A number of callback functions associated with the CFRunLoop types are
allowed to be NULL to evoke default behavior.

Prior to this commit the definition of those structs prevented passing
in null which meant resorting to transmute or other tricks to
effectively set null, but with recent compiler versions the compiler
emits `ud2` instructions and triggers a fault at runtime.

The correct resolution for this is to define these fields as `Option`al
callbacks and that is what this commit does.

I've used this approach successfully here in another project:
Refs: https://github.com/wez/wezterm/commit/398f333c32fb1ff4ccd9cc001a66ae22522eb60c

I've bumped up the version for the crate as part of this commit
because it effectively changes the API around these structs.
This commit is contained in:
Wez Furlong
2019-11-11 19:57:48 -08:00
parent 80a9bf00e4
commit cf3659fc6f
3 changed files with 21 additions and 21 deletions
+1 -1
View File
@@ -3,7 +3,7 @@ name = "core-foundation-sys"
description = "Bindings to Core Foundation for macOS"
homepage = "https://github.com/servo/core-foundation-rs"
repository = "https://github.com/servo/core-foundation-rs"
version = "0.6.3"
version = "0.7.0"
authors = ["The Servo Project Developers"]
license = "MIT / Apache-2.0"
build = "build.rs"
+19 -19
View File
@@ -50,13 +50,13 @@ pub const kCFRunLoopAllActivities: CFOptionFlags = 0x0FFFFFFF;
pub struct CFRunLoopSourceContext {
pub version: CFIndex,
pub info: *mut c_void,
pub retain: extern "C" fn (info: *const c_void) -> *const c_void,
pub release: extern "C" fn (info: *const c_void),
pub copyDescription: extern "C" fn (info: *const c_void) -> CFStringRef,
pub equal: extern "C" fn (info1: *const c_void, info2: *const c_void) -> Boolean,
pub hash: extern "C" fn (info: *const c_void) -> CFHashCode,
pub schedule: extern "C" fn (info: *const c_void, rl: CFRunLoopRef, mode: CFStringRef),
pub cancel: extern "C" fn (info: *const c_void, rl: CFRunLoopRef, mode: CFStringRef),
pub retain: Option<extern "C" fn (info: *const c_void) -> *const c_void>,
pub release: Option<extern "C" fn (info: *const c_void)>,
pub copyDescription: Option<extern "C" fn (info: *const c_void) -> CFStringRef>,
pub equal: Option<extern "C" fn (info1: *const c_void, info2: *const c_void) -> Boolean>,
pub hash: Option<extern "C" fn (info: *const c_void) -> CFHashCode>,
pub schedule: Option<extern "C" fn (info: *const c_void, rl: CFRunLoopRef, mode: CFStringRef)>,
pub cancel: Option<extern "C" fn (info: *const c_void, rl: CFRunLoopRef, mode: CFStringRef)>,
pub perform: extern "C" fn (info: *const c_void),
}
@@ -64,11 +64,11 @@ pub struct CFRunLoopSourceContext {
pub struct CFRunLoopSourceContext1 {
pub version: CFIndex,
pub info: *mut c_void,
pub retain: extern "C" fn (info: *const c_void) -> *const c_void,
pub release: extern "C" fn (info: *const c_void),
pub copyDescription: extern "C" fn (info: *const c_void) -> CFStringRef,
pub equal: extern "C" fn (info1: *const c_void, info2: *const c_void) -> Boolean,
pub hash: extern "C" fn (info: *const c_void) -> CFHashCode,
pub retain: Option<extern "C" fn (info: *const c_void) -> *const c_void>,
pub release: Option<extern "C" fn (info: *const c_void)>,
pub copyDescription: Option<extern "C" fn (info: *const c_void) -> CFStringRef>,
pub equal: Option<extern "C" fn (info1: *const c_void, info2: *const c_void) -> Boolean>,
pub hash: Option<extern "C" fn (info: *const c_void) -> CFHashCode>,
// note that the following two fields are platform dependent in the C header, the ones here are for macOS
pub getPort: extern "C" fn (info: *mut c_void) -> mach_port_t,
pub perform: extern "C" fn (msg: *mut c_void, size: CFIndex, allocator: CFAllocatorRef, info: *mut c_void) -> *mut c_void,
@@ -78,9 +78,9 @@ pub struct CFRunLoopSourceContext1 {
pub struct CFRunLoopObserverContext {
pub version: CFIndex,
pub info: *mut c_void,
pub retain: extern "C" fn (info: *const c_void) -> *const c_void,
pub release: extern "C" fn (info: *const c_void),
pub copyDescription: extern "C" fn (info: *const c_void) -> CFStringRef,
pub retain: Option<extern "C" fn (info: *const c_void) -> *const c_void>,
pub release: Option<extern "C" fn (info: *const c_void)>,
pub copyDescription: Option<extern "C" fn (info: *const c_void) -> CFStringRef>,
}
pub type CFRunLoopObserverCallBack = extern "C" fn (observer: CFRunLoopObserverRef, activity: CFRunLoopActivity, info: *mut c_void);
@@ -89,15 +89,15 @@ pub type CFRunLoopObserverCallBack = extern "C" fn (observer: CFRunLoopObserverR
pub struct CFRunLoopTimerContext {
pub version: CFIndex,
pub info: *mut c_void,
pub retain: extern "C" fn (info: *const c_void) -> *const c_void,
pub release: extern "C" fn (info: *const c_void),
pub copyDescription: extern "C" fn (info: *const c_void) -> CFStringRef,
pub retain: Option<extern "C" fn (info: *const c_void) -> *const c_void>,
pub release: Option<extern "C" fn (info: *const c_void)>,
pub copyDescription: Option<extern "C" fn (info: *const c_void) -> CFStringRef>,
}
pub type CFRunLoopTimerCallBack = extern "C" fn (timer: CFRunLoopTimerRef, info: *mut c_void);
#[repr(C)]
pub struct __CFRunLoopTimer;
pub struct __CFRunLoopTimer(c_void);
pub type CFRunLoopTimerRef = *mut __CFRunLoopTimer;
+1 -1
View File
@@ -11,7 +11,7 @@ keywords = ["macos", "framework", "objc"]
[dependencies.core-foundation-sys]
path = "../core-foundation-sys"
version = "0.6.1"
version = "0.7.0"
[dependencies]
libc = "0.2"