Skip to content

Commit 05d0b0d

Browse files
martinlingbilelmoussaoui
authored andcommitted
Fix segfault in FileChooserExtManual::add_choice()
1 parent 57345c8 commit 05d0b0d

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

gtk4/src/file_chooser.rs

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,45 @@ mod sealed {
1616
pub trait FileChooserExtManual: sealed::Sealed + IsA<FileChooser> + 'static {
1717
#[doc(alias = "gtk_file_chooser_add_choice")]
1818
fn add_choice(&self, id: impl IntoGStr, label: impl IntoGStr, options: &[(&str, &str)]) {
19-
unsafe {
20-
let (options_ids, options_labels) = if options.is_empty() {
21-
(std::ptr::null(), std::ptr::null())
22-
} else {
23-
let stashes_ids = options
24-
.iter()
25-
.map(|o| o.0.to_glib_none())
26-
.collect::<Vec<_>>();
27-
let stashes_labels = options
28-
.iter()
29-
.map(|o| o.1.to_glib_none())
30-
.collect::<Vec<_>>();
31-
(
32-
stashes_ids
33-
.iter()
34-
.map(|o| o.0)
35-
.collect::<Vec<*const libc::c_char>>()
36-
.as_ptr(),
37-
stashes_labels
38-
.iter()
39-
.map(|o| o.0)
40-
.collect::<Vec<*const libc::c_char>>()
41-
.as_ptr(),
42-
)
43-
};
44-
19+
if options.is_empty() {
20+
id.run_with_gstr(|id| {
21+
label.run_with_gstr(|label| unsafe {
22+
ffi::gtk_file_chooser_add_choice(
23+
self.as_ref().to_glib_none().0,
24+
id.as_ptr(),
25+
label.as_ptr(),
26+
mut_override(std::ptr::null()),
27+
mut_override(std::ptr::null()),
28+
);
29+
});
30+
});
31+
} else {
32+
let stashes_ids = options
33+
.iter()
34+
.map(|o| o.0.to_glib_none())
35+
.collect::<Vec<_>>();
36+
let stashes_labels = options
37+
.iter()
38+
.map(|o| o.1.to_glib_none())
39+
.collect::<Vec<_>>();
40+
let options_ids = stashes_ids
41+
.iter()
42+
.map(|o| o.0)
43+
.chain(std::iter::once(std::ptr::null()))
44+
.collect::<Vec<*const libc::c_char>>();
45+
let options_labels = stashes_labels
46+
.iter()
47+
.map(|o| o.0)
48+
.chain(std::iter::once(std::ptr::null()))
49+
.collect::<Vec<*const libc::c_char>>();
4550
id.run_with_gstr(|id| {
46-
label.run_with_gstr(|label| {
51+
label.run_with_gstr(|label| unsafe {
4752
ffi::gtk_file_chooser_add_choice(
4853
self.as_ref().to_glib_none().0,
4954
id.as_ptr(),
5055
label.as_ptr(),
51-
mut_override(options_ids),
52-
mut_override(options_labels),
56+
mut_override(options_ids.as_ptr()),
57+
mut_override(options_labels.as_ptr()),
5358
);
5459
});
5560
});

0 commit comments

Comments
 (0)