Skip to content

Commit b2c44ac

Browse files
authored
Merge pull request #836 from jf2048/imp-offset-addr-of
glib: fix undefined behavior in types::register_type
2 parents 8426bdb + 2b52d2f commit b2c44ac

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

glib/src/subclass/types.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -968,10 +968,9 @@ pub fn register_type<T: ObjectSubclass>() -> Type {
968968
// Must not be a dangling pointer so let's create some uninitialized memory
969969
let priv_ = std::mem::MaybeUninit::<PrivateStruct<T>>::uninit();
970970
let ptr = priv_.as_ptr();
971-
// FIXME: Technically UB but we'd need std::ptr::raw_const for this
972-
let imp_ptr = &(*ptr).imp as *const _ as *const u8;
971+
let imp_ptr = std::ptr::addr_of!((*ptr).imp) as *const u8;
973972
let ptr = ptr as *const u8;
974-
imp_ptr as isize - ptr as isize
973+
imp_ptr.offset_from(ptr)
975974
};
976975

977976
let iface_types = T::Interfaces::iface_infos();

0 commit comments

Comments
 (0)