From 0a5b349818a2acb1a338616e6b6a9affd8cf6a51 Mon Sep 17 00:00:00 2001 From: Martin Ling Date: Fri, 6 Sep 2024 19:22:55 +0100 Subject: [PATCH] Fix segfault in FileChooserExtManual::add_choice() --- gtk4/src/file_chooser.rs | 63 ++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/gtk4/src/file_chooser.rs b/gtk4/src/file_chooser.rs index deaa2af927a6..8bdb2b43c310 100644 --- a/gtk4/src/file_chooser.rs +++ b/gtk4/src/file_chooser.rs @@ -16,40 +16,45 @@ mod sealed { pub trait FileChooserExtManual: sealed::Sealed + IsA + 'static { #[doc(alias = "gtk_file_chooser_add_choice")] fn add_choice(&self, id: impl IntoGStr, label: impl IntoGStr, options: &[(&str, &str)]) { - unsafe { - let (options_ids, options_labels) = if options.is_empty() { - (std::ptr::null(), std::ptr::null()) - } else { - let stashes_ids = options - .iter() - .map(|o| o.0.to_glib_none()) - .collect::>(); - let stashes_labels = options - .iter() - .map(|o| o.1.to_glib_none()) - .collect::>(); - ( - stashes_ids - .iter() - .map(|o| o.0) - .collect::>() - .as_ptr(), - stashes_labels - .iter() - .map(|o| o.0) - .collect::>() - .as_ptr(), - ) - }; - + if options.is_empty() { + id.run_with_gstr(|id| { + label.run_with_gstr(|label| unsafe { + ffi::gtk_file_chooser_add_choice( + self.as_ref().to_glib_none().0, + id.as_ptr(), + label.as_ptr(), + mut_override(std::ptr::null()), + mut_override(std::ptr::null()), + ); + }); + }); + } else { + let stashes_ids = options + .iter() + .map(|o| o.0.to_glib_none()) + .collect::>(); + let stashes_labels = options + .iter() + .map(|o| o.1.to_glib_none()) + .collect::>(); + let options_ids = stashes_ids + .iter() + .map(|o| o.0) + .chain(std::iter::once(std::ptr::null())) + .collect::>(); + let options_labels = stashes_labels + .iter() + .map(|o| o.0) + .chain(std::iter::once(std::ptr::null())) + .collect::>(); id.run_with_gstr(|id| { - label.run_with_gstr(|label| { + label.run_with_gstr(|label| unsafe { ffi::gtk_file_chooser_add_choice( self.as_ref().to_glib_none().0, id.as_ptr(), label.as_ptr(), - mut_override(options_ids), - mut_override(options_labels), + mut_override(options_ids.as_ptr()), + mut_override(options_labels.as_ptr()), ); }); });