Skip to content

Commit

Permalink
Fix segfault in FileChooserExtManual::add_choice()
Browse files Browse the repository at this point in the history
  • Loading branch information
martinling authored and bilelmoussaoui committed Sep 26, 2024
1 parent 57345c8 commit 05d0b0d
Showing 1 changed file with 34 additions and 29 deletions.
63 changes: 34 additions & 29 deletions gtk4/src/file_chooser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,45 @@ mod sealed {
pub trait FileChooserExtManual: sealed::Sealed + IsA<FileChooser> + '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::<Vec<_>>();
let stashes_labels = options
.iter()
.map(|o| o.1.to_glib_none())
.collect::<Vec<_>>();
(
stashes_ids
.iter()
.map(|o| o.0)
.collect::<Vec<*const libc::c_char>>()
.as_ptr(),
stashes_labels
.iter()
.map(|o| o.0)
.collect::<Vec<*const libc::c_char>>()
.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::<Vec<_>>();
let stashes_labels = options
.iter()
.map(|o| o.1.to_glib_none())
.collect::<Vec<_>>();
let options_ids = stashes_ids
.iter()
.map(|o| o.0)
.chain(std::iter::once(std::ptr::null()))
.collect::<Vec<*const libc::c_char>>();
let options_labels = stashes_labels
.iter()
.map(|o| o.0)
.chain(std::iter::once(std::ptr::null()))
.collect::<Vec<*const libc::c_char>>();
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()),
);
});
});
Expand Down

0 comments on commit 05d0b0d

Please sign in to comment.