diff --git a/glib/src/collections/ptr_slice.rs b/glib/src/collections/ptr_slice.rs index ce5c76cb9a65..32320555dea0 100644 --- a/glib/src/collections/ptr_slice.rs +++ b/glib/src/collections/ptr_slice.rs @@ -658,13 +658,21 @@ impl PtrSlice { /// This is guaranteed to be `NULL`-terminated. #[inline] pub fn into_raw(mut self) -> *mut ::GlibType { + // Make sure to allocate a valid pointer that points to a + // NULL-pointer. if self.len == 0 { - ptr::null_mut() - } else { - self.len = 0; - self.capacity = 0; - self.ptr.as_ptr() + self.reserve(0); + unsafe { + ptr::write( + self.ptr.as_ptr().add(0), + Ptr::from(ptr::null_mut::<::GlibType>()), + ); + } } + + self.len = 0; + self.capacity = 0; + self.ptr.as_ptr() } // rustdoc-stripper-ignore-next diff --git a/glib/src/collections/slice.rs b/glib/src/collections/slice.rs index 6b7ad394cc05..8dc771c9ef94 100644 --- a/glib/src/collections/slice.rs +++ b/glib/src/collections/slice.rs @@ -546,8 +546,6 @@ impl Slice { // rustdoc-stripper-ignore-next /// Returns the underlying pointer. - /// - /// This is guaranteed to be `NULL`-terminated. #[inline] pub fn as_ptr(&self) -> *const T::GlibType { if self.len == 0 { @@ -559,8 +557,6 @@ impl Slice { // rustdoc-stripper-ignore-next /// Returns the underlying pointer. - /// - /// This is guaranteed to be `NULL`-terminated. #[inline] pub fn as_mut_ptr(&mut self) -> *mut T::GlibType { if self.len == 0 { @@ -572,8 +568,6 @@ impl Slice { // rustdoc-stripper-ignore-next /// Consumes the slice and returns the underlying pointer. - /// - /// This is guaranteed to be `NULL`-terminated. #[inline] pub fn into_raw(mut self) -> *mut T::GlibType { if self.len == 0 { diff --git a/glib/src/collections/strv.rs b/glib/src/collections/strv.rs index 6852256908d4..fcc30d0b2e01 100644 --- a/glib/src/collections/strv.rs +++ b/glib/src/collections/strv.rs @@ -646,13 +646,18 @@ impl StrV { /// This is guaranteed to be `NULL`-terminated. #[inline] pub fn into_raw(mut self) -> *mut *mut c_char { + // Make sure to allocate a valid pointer that points to a + // NULL-pointer. if self.len == 0 { - ptr::null_mut() - } else { - self.len = 0; - self.capacity = 0; - self.ptr.as_ptr() + self.reserve(0); + unsafe { + *self.ptr.as_ptr().add(0) = ptr::null_mut(); + } } + + self.len = 0; + self.capacity = 0; + self.ptr.as_ptr() } // rustdoc-stripper-ignore-next