Skip to content

Commit 5366869

Browse files
committed
gio: Fix segfault in ListStore::find_with_equal_func
1 parent 94c3909 commit 5366869

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

gio/src/list_store.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,21 @@ impl ListStore {
150150
(*func)(&a).into_glib()
151151
}
152152

153+
// GIO prior to 2.76 requires a non-NULL item to be passed in so we're constructing a fake item here.
154+
// See https://gitlab.gnome.org/GNOME/glib/-/merge_requests/3284
155+
#[cfg(not(feature = "v2_76"))]
153156
unsafe {
154-
// GIO requires a non-NULL item to be passed in so we're constructing a fake item here.
155-
// See https://gitlab.gnome.org/GNOME/glib/-/merge_requests/3284
157+
let g_class: *mut glib::gobject_ffi::GTypeClass =
158+
glib::gobject_ffi::g_type_class_peek(self.item_type().into_glib()) as *mut _;
159+
160+
// g_class will be `NULL` when no instance of the `item-type` has been created yet.
161+
// See https://github.com/gtk-rs/gtk-rs-core/issues/1767
162+
if g_class.is_null() {
163+
return None;
164+
}
165+
156166
let item = glib::gobject_ffi::GObject {
157-
g_type_instance: glib::gobject_ffi::GTypeInstance {
158-
g_class: glib::gobject_ffi::g_type_class_peek(self.item_type().into_glib())
159-
as *mut _,
160-
},
167+
g_type_instance: glib::gobject_ffi::GTypeInstance { g_class },
161168
ref_count: 1,
162169
qdata: std::ptr::null_mut(),
163170
};
@@ -178,6 +185,26 @@ impl ListStore {
178185

179186
found.then(|| position.assume_init())
180187
}
188+
189+
#[cfg(feature = "v2_76")]
190+
unsafe {
191+
let mut func = equal_func;
192+
let func_obj: &mut (dyn FnMut(&Object) -> bool) = &mut func;
193+
let func_ptr =
194+
&func_obj as *const &mut (dyn FnMut(&Object) -> bool) as glib::ffi::gpointer;
195+
196+
let mut position = std::mem::MaybeUninit::uninit();
197+
198+
let found = bool::from_glib(ffi::g_list_store_find_with_equal_func_full(
199+
self.to_glib_none().0,
200+
std::ptr::null_mut(),
201+
Some(equal_func_trampoline),
202+
func_ptr,
203+
position.as_mut_ptr(),
204+
));
205+
206+
found.then(|| position.assume_init())
207+
}
181208
}
182209
}
183210

0 commit comments

Comments
 (0)