-
Notifications
You must be signed in to change notification settings - Fork 15
Description
An internal macro (set_callback
) uses a transmute to create several different function types. This is UB and indicative of an underlying problem.
Lines 223 to 233 in 9248d74
macro_rules! set_callback { | |
($output: expr, $input: expr) => ( | |
unsafe { | |
if $input == mem::transmute( 0 as usize ) { | |
$output = None; | |
} else { | |
$output = Some( $input ); | |
} | |
} | |
) | |
} |
According to the documentation of the function type:
Like references, function pointers are, among other things, assumed to not be null, so if you want to pass a function pointer over FFI and be able to accommodate null pointers, make your type Option<fn()> with your required signature.
The comparison might simply never succeed. What was likely intended is to use the types Option<RespectiveFn>
for the input argument, and not only for those stored. This might have been enver observed as a possible (and somewhat likely) likely compilation assigns the null pointer directly to the Option
field, thus changing the discriminant to the intended None
.
As also aluded to in the documentation, the correct type for ffi is also Option<fn(_) -> _
which should be reflected in the exposed C-interface here.
Also unfortunately, the liberal use of unsafe
in the macro hides the fact that the assignment to the static mut ENVIRONMENT_CALLBACK
is the one that is actually unsafe
and strictly speaking would require a form of synchronization, such as using an AtomicPtr
.