Skip to content

Commit 6145da9

Browse files
committed
glib: Fix segfault in List::retain()
One more level of pointer indirection is needed to get the item from the list.
1 parent 9110313 commit 6145da9

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

glib/src/collections/list.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ impl<T: TransparentPtrType> List<T> {
355355
unsafe {
356356
let mut ptr = head.as_ptr();
357357
while !ptr.is_null() {
358-
let item = &*((*ptr).data as *const ffi::gpointer as *const T);
358+
let item = &*(&(*ptr).data as *const ffi::gpointer as *const T);
359359
let next = (*ptr).next;
360360
if !f(item) {
361361
ptr::drop_in_place(&mut (*ptr).data as *mut ffi::gpointer as *mut T);
@@ -909,6 +909,12 @@ mod test {
909909
let mut list_items = list2.iter().cloned().collect::<Vec<_>>();
910910
list_items.reverse();
911911
assert_eq!(&items[1..], &list_items);
912+
913+
list.reverse();
914+
let mut list3 = list.clone();
915+
list3.retain(|item| item.seconds() >= 14.0);
916+
let list_items = list3.iter().cloned().collect::<Vec<_>>();
917+
assert_eq!(&items[2..], &list_items);
912918
}
913919

914920
#[test]

glib/src/collections/slist.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ impl<T: TransparentPtrType> SList<T> {
350350
unsafe {
351351
let mut ptr = head.as_ptr();
352352
while !ptr.is_null() {
353-
let item = &*((*ptr).data as *const ffi::gpointer as *const T);
353+
let item = &*(&(*ptr).data as *const ffi::gpointer as *const T);
354354
let next = (*ptr).next;
355355
if !f(item) {
356356
ptr::drop_in_place(&mut (*ptr).data as *mut ffi::gpointer as *mut T);
@@ -902,6 +902,12 @@ mod test {
902902
let mut list_items = list2.iter().cloned().collect::<Vec<_>>();
903903
list_items.reverse();
904904
assert_eq!(&items[1..], &list_items);
905+
906+
list.reverse();
907+
let mut list3 = list.clone();
908+
list3.retain(|item| item.seconds() >= 14.0);
909+
let list_items = list3.iter().cloned().collect::<Vec<_>>();
910+
assert_eq!(&items[2..], &list_items);
905911
}
906912

907913
#[test]

0 commit comments

Comments
 (0)