Skip to content

Commit e9a2ab3

Browse files
authored
Merge pull request #1734 from sdroege/strv-into-raw-empty
glib: Make sure that empty `glib::StrV` / `glib::PtrSlice` returns a valid pointer
2 parents e7f5c5d + b4e16a5 commit e9a2ab3

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
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/slice.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -546,8 +546,6 @@ impl<T: TransparentType> Slice<T> {
546546

547547
// rustdoc-stripper-ignore-next
548548
/// Returns the underlying pointer.
549-
///
550-
/// This is guaranteed to be `NULL`-terminated.
551549
#[inline]
552550
pub fn as_ptr(&self) -> *const T::GlibType {
553551
if self.len == 0 {
@@ -559,8 +557,6 @@ impl<T: TransparentType> Slice<T> {
559557

560558
// rustdoc-stripper-ignore-next
561559
/// Returns the underlying pointer.
562-
///
563-
/// This is guaranteed to be `NULL`-terminated.
564560
#[inline]
565561
pub fn as_mut_ptr(&mut self) -> *mut T::GlibType {
566562
if self.len == 0 {
@@ -572,8 +568,6 @@ impl<T: TransparentType> Slice<T> {
572568

573569
// rustdoc-stripper-ignore-next
574570
/// Consumes the slice and returns the underlying pointer.
575-
///
576-
/// This is guaranteed to be `NULL`-terminated.
577571
#[inline]
578572
pub fn into_raw(mut self) -> *mut T::GlibType {
579573
if self.len == 0 {

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)