Skip to content

Commit

Permalink
Use explicit pointer casting
Browse files Browse the repository at this point in the history
This commit replaces calls to `pointer::cast` with explicit casting via
`as *mut SomeType`.

This reduces the likelihood of our codegen generating unsound code that
casts a pointer to the wrong type.
  • Loading branch information
chinedufn committed Apr 29, 2024
1 parent 9d02d8f commit b4ba1a7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,11 @@ impl BridgeableType for OpaqueForeignType {
}
}
HostLang::Swift => {
let ty = &self.ty;

quote! {
if let Some(val) = #expression {
val.0.cast()
val.0 as *mut super::#ty
} else {
std::ptr::null_mut()
}
Expand Down Expand Up @@ -517,7 +519,7 @@ impl BridgeableType for OpaqueForeignType {
if val.is_null() {
None
} else {
Some(#ty(val.cast()))
Some(#ty(val as *mut std::ffi::c_void))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ mod extern_rust_fn_return_option_opaque_swift_type {
#[export_name = "__swift_bridge__$some_function"]
pub extern "C" fn __swift_bridge__some_function() -> *mut super::SomeSwiftType {
if let Some(val) = super::some_function() {
val.0.cast()
val.0 as *mut super::SomeSwiftType
} else {
std::ptr::null_mut()
}
Expand Down Expand Up @@ -929,7 +929,7 @@ mod extern_rust_fn_with_option_opaque_swift_type_arg {
if val.is_null() {
None
} else {
Some(SomeSwiftType(val.cast()))
Some(SomeSwiftType(val as *mut std::ffi::c_void))
}
})
}
Expand Down

0 comments on commit b4ba1a7

Please sign in to comment.