Skip to content

Commit

Permalink
glib: fix undefined behavior in types::register_type
Browse files Browse the repository at this point in the history
ptr::addr_of has been stable for a while, we can remove this UB.
  • Loading branch information
jf2048 authored and sdroege committed Nov 27, 2022
1 parent 8f1b759 commit 27dda71
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions glib/src/subclass/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -968,10 +968,9 @@ pub fn register_type<T: ObjectSubclass>() -> Type {
// Must not be a dangling pointer so let's create some uninitialized memory
let priv_ = std::mem::MaybeUninit::<PrivateStruct<T>>::uninit();
let ptr = priv_.as_ptr();
// FIXME: Technically UB but we'd need std::ptr::raw_const for this
let imp_ptr = &(*ptr).imp as *const _ as *const u8;
let imp_ptr = std::ptr::addr_of!((*ptr).imp) as *const u8;
let ptr = ptr as *const u8;
imp_ptr as isize - ptr as isize
imp_ptr.offset_from(ptr)
};

let iface_types = T::Interfaces::iface_infos();
Expand Down

0 comments on commit 27dda71

Please sign in to comment.