Skip to content

Commit b4e16a5

Browse files
committed
glib: Make sure that empty glib::StrV / glib::PtrSlice returns a valid pointer
Instead of returning `NULL` make sure to return a valid pointer that dereferences into a `NULL`-pointer. Fixes #1733
1 parent 37fc03e commit b4e16a5

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

glib/src/collections/ptr_slice.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -658,13 +658,21 @@ impl<T: TransparentPtrType> PtrSlice<T> {
658658
/// This is guaranteed to be `NULL`-terminated.
659659
#[inline]
660660
pub fn into_raw(mut self) -> *mut <T as GlibPtrDefault>::GlibType {
661+
// Make sure to allocate a valid pointer that points to a
662+
// NULL-pointer.
661663
if self.len == 0 {
662-
ptr::null_mut()
663-
} else {
664-
self.len = 0;
665-
self.capacity = 0;
666-
self.ptr.as_ptr()
664+
self.reserve(0);
665+
unsafe {
666+
ptr::write(
667+
self.ptr.as_ptr().add(0),
668+
Ptr::from(ptr::null_mut::<<T as GlibPtrDefault>::GlibType>()),
669+
);
670+
}
667671
}
672+
673+
self.len = 0;
674+
self.capacity = 0;
675+
self.ptr.as_ptr()
668676
}
669677

670678
// rustdoc-stripper-ignore-next

glib/src/collections/strv.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -646,13 +646,18 @@ impl StrV {
646646
/// This is guaranteed to be `NULL`-terminated.
647647
#[inline]
648648
pub fn into_raw(mut self) -> *mut *mut c_char {
649+
// Make sure to allocate a valid pointer that points to a
650+
// NULL-pointer.
649651
if self.len == 0 {
650-
ptr::null_mut()
651-
} else {
652-
self.len = 0;
653-
self.capacity = 0;
654-
self.ptr.as_ptr()
652+
self.reserve(0);
653+
unsafe {
654+
*self.ptr.as_ptr().add(0) = ptr::null_mut();
655+
}
655656
}
657+
658+
self.len = 0;
659+
self.capacity = 0;
660+
self.ptr.as_ptr()
656661
}
657662

658663
// rustdoc-stripper-ignore-next

0 commit comments

Comments
 (0)