filechooser: use-after-free and selection fixes from GTK 3

Backported from GTK 3:

- no use of a freed chooser or path bar after closing
- a file selected in a hidden dialog stays selected
- too long names and existing files in create-folder mode give an error
- insensitive accept buttons stay unused
- the first row no longer overwrites typed names
- the button follows menu and drops
- private recent items stay hidden
- unknown MIME types are quiet
- icon cache and thin icons

Upstream commits:

- 2c40c2b7c8 (Matthias Clasen)
- 70eb35d569 (Volker Sobek)
- 63bc3542ce (Matthias Clasen)
- 3ebcc5a7d5 (Daniel Boles)
- fcd3321fa7 (Руслан Ижбулатов)
- 0f929fb6e3 (Matthias Clasen)
- 5111f7af99 (Kai Willadsen)
- edcddf6561 (Benjamin Otte)
- 0352d88b8a (Luca Bacci)
- 90ca7fb3c9 (Gaël Bonithon)
- b940122c4a (Timothy Arceri)
- 080f8740f2 (Siegfried-Angel Gevatter Pujals)
- 79c1095512 (Matthias Clasen)
- 32e75adf6a (Federico Mena Quintero)
- 0367f785ef (Matthias Clasen)
- b01f371ce6 (Nelson Benítez León)
- 096b49013a (Matthias Clasen)
This commit is contained in:
Daemonratte 2026-10-18 13:41:38 +02:00
commit a52972c7b7
13 changed files with 130 additions and 30 deletions

View file

@ -1162,7 +1162,7 @@ dnd_select_folder_get_info_cb (GCancellable *cancellable,
data->selected =
(((data->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER && is_folder) ||
(data->action == GTK_FILE_CHOOSER_ACTION_OPEN && !is_folder)) &&
gtk_file_chooser_select_file (GTK_FILE_CHOOSER (data->button->priv->dialog),
gtk_file_chooser_select_file (GTK_FILE_CHOOSER (data->button),
data->file, NULL));
}
else
@ -2565,7 +2565,12 @@ update_label_and_image (GtkFileChooserButton *button)
}
}
if (g_file_is_native (file))
/* a remote file that is not a bookmark is asked for its name too,
* instead of showing "(None)" as if nothing was chosen */
if (!g_file_is_native (file))
label_text = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
if (g_file_is_native (file) || label_text == NULL)
{
priv->update_button_cancellable =
_gtk_file_system_get_info (priv->fs, file,
@ -2577,7 +2582,6 @@ update_label_and_image (GtkFileChooserButton *button)
{
GdkPixbuf *pixbuf;
label_text = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)),
"text-x-generic",
priv->icon_size, 0, NULL);
@ -2738,11 +2742,12 @@ static void
combo_box_changed_cb (GtkComboBox *combo_box,
gpointer user_data)
{
GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
GtkTreeIter iter;
gboolean file_was_set = FALSE;
if (gtk_combo_box_get_active_iter (combo_box, &iter))
{
GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
GtkFileChooserButtonPrivate *priv = button->priv;
gchar type;
gpointer data;
@ -2762,7 +2767,10 @@ combo_box_changed_cb (GtkComboBox *combo_box,
case ROW_TYPE_BOOKMARK:
case ROW_TYPE_CURRENT_FOLDER:
if (data)
gtk_file_chooser_button_select_file (GTK_FILE_CHOOSER (button), data, NULL);
{
gtk_file_chooser_button_select_file (GTK_FILE_CHOOSER (button), data, NULL);
file_was_set = TRUE;
}
break;
case ROW_TYPE_VOLUME:
{
@ -2772,6 +2780,7 @@ combo_box_changed_cb (GtkComboBox *combo_box,
if (base_file)
{
gtk_file_chooser_button_select_file (GTK_FILE_CHOOSER (button), base_file, NULL);
file_was_set = TRUE;
g_object_unref (base_file);
}
}
@ -2783,6 +2792,10 @@ combo_box_changed_cb (GtkComboBox *combo_box,
break;
}
}
/* the user chose a file or folder in the menu of the button */
if (file_was_set)
g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
}
/* Calback for the "notify::popup-shown" signal on the combo box.

View file

@ -1078,15 +1078,14 @@ error_creating_folder_dialog (GtkFileChooserDefault *impl,
* the same name is already there.
*/
static void
error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
GFile *file,
GError *error)
error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl)
{
error_dialog (impl,
_("The folder could not be created, as a file with the same "
"name already exists. Try using a different name for the "
"folder, or rename the file first."),
file, error);
/* there is no GError here: the file was found */
error_message (impl,
_("The folder could not be created, as a file with the same "
"name already exists. Try using a different name for the "
"folder, or rename the file first."),
NULL);
}
static void
@ -1104,6 +1103,14 @@ error_with_file_under_nonfolder (GtkFileChooserDefault *impl,
parent_file, error);
}
static void
error_filename_too_long_dialog (GtkFileChooserDefault *impl)
{
error_message (impl,
_("Cannot create file as the filename is too long"),
_("Try using a shorter name."));
}
/* Shows an error about not being able to select a folder because a file with
* the same name is already there.
*/
@ -9406,7 +9413,13 @@ browse_files_select_first_row (GtkFileChooserDefault *impl)
/* If the list is empty, do nothing. */
if (gtk_tree_model_get_iter (tree_model, &dummy_iter, path))
current_view_set_cursor (impl, path);
{
/* Setting the cursor also selects the row in the tree view; the
* location entry is only filled from selections by the user. */
impl->auto_selecting_first_row = TRUE;
current_view_set_cursor (impl, path);
impl->auto_selecting_first_row = FALSE;
}
gtk_tree_path_free (path);
}
@ -10599,7 +10612,7 @@ update_chooser_entry (GtkFileChooserDefault *impl)
else
change_entry = TRUE; /* ... unless we are in SELECT_FOLDER mode */
if (change_entry)
if (change_entry && !impl->auto_selecting_first_row)
{
gtk_entry_set_text (GTK_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
@ -10720,6 +10733,7 @@ update_current_folder_mount_enclosing_volume_cb (GCancellable *cancellabl
change_folder_and_display_error (impl, data->file, data->clear_entry);
out:
g_object_unref (data->impl);
g_object_unref (data->file);
g_free (data);
@ -10886,6 +10900,7 @@ update_current_folder_get_info_cb (GCancellable *cancellable,
g_signal_emit_by_name (impl, "selection-changed", 0);
out:
g_object_unref (data->impl);
g_object_unref (data->file);
g_free (data);
@ -10925,7 +10940,7 @@ gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
/* Test validity of path here. */
data = g_new0 (struct UpdateCurrentFolderData, 1);
data->impl = impl;
data->impl = g_object_ref (impl);
data->file = g_object_ref (file);
data->keep_trail = keep_trail;
data->clear_entry = clear_entry;
@ -11008,6 +11023,10 @@ gtk_file_chooser_default_select_file (GtkFileChooser *chooser,
files.data = (gpointer) file;
files.next = NULL;
/* Prevent the file chooser from loading a different folder when
* it is mapped (again: a dialog that is reused) */
impl->reload_state = RELOAD_HAS_FOLDER;
result = show_and_select_files (impl, &files);
g_object_unref (parent_file);
return result;
@ -12148,8 +12167,7 @@ file_exists_get_info_cb (GCancellable *cancellable,
/* Oops, the user typed the name of an existing path which is not
* a folder
*/
error_creating_folder_over_existing_file_dialog (data->impl, data->file,
g_error_copy (error));
error_creating_folder_over_existing_file_dialog (data->impl);
}
else
{
@ -12177,6 +12195,9 @@ file_exists_get_info_cb (GCancellable *cancellable,
{
if (is_folder)
change_folder_and_display_error (data->impl, data->file, TRUE);
else if (!file_exists &&
g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FILENAME_TOO_LONG))
error_filename_too_long_dialog (data->impl);
else
needs_parent_check = TRUE;
}
@ -13006,6 +13027,22 @@ get_recent_files_limit (GtkWidget *widget)
return limit;
}
/* Whether @info was registered as private by other applications only;
* GtkRecentChooser leaves out private items as well */
static gboolean
recent_item_is_private (GtkRecentInfo *info)
{
const gchar *app_name;
if (!gtk_recent_info_get_private_hint (info))
return FALSE;
app_name = g_get_application_name ();
return app_name == NULL ||
!gtk_recent_info_has_application (info, app_name);
}
/* Populates the file system model with the GtkRecentInfo* items in the provided list; frees the items */
static void
populate_model_with_recent_items (GtkFileChooserDefault *impl, GList *items)
@ -13023,6 +13060,9 @@ populate_model_with_recent_items (GtkFileChooserDefault *impl, GList *items)
GtkRecentInfo *info = l->data;
GFile *file;
if (recent_item_is_private (info))
continue;
file = g_file_new_for_uri (gtk_recent_info_get_uri (info));
_gtk_file_system_model_add_and_query_file (impl->recent_model,
file,
@ -13179,6 +13219,19 @@ set_current_filter (GtkFileChooserDefault *impl,
if (impl->recent_model)
_gtk_file_system_model_set_filter (impl->recent_model, filter);
/* Folders are sensitive by the filter when choosing a folder;
* the cached sensitivity is from the old one */
if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
{
if (impl->browse_files_model)
_gtk_file_system_model_clear_cache (impl->browse_files_model, MODEL_COL_IS_SENSITIVE);
if (impl->search_model)
_gtk_file_system_model_clear_cache (impl->search_model, MODEL_COL_IS_SENSITIVE);
if (impl->recent_model)
_gtk_file_system_model_clear_cache (impl->recent_model, MODEL_COL_IS_SENSITIVE);
}
g_object_notify (G_OBJECT (impl), "filter");
}
}

View file

@ -141,7 +141,8 @@ file_chooser_widget_file_activated (GtkFileChooser *chooser,
widget = GTK_WIDGET (l->data);
response_id = gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);
if (is_stock_accept_response_id (response_id))
if (gtk_widget_is_sensitive (widget) &&
is_stock_accept_response_id (response_id))
{
gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */
break;
@ -238,7 +239,8 @@ file_chooser_widget_response_requested (GtkWidget *widget,
widget = GTK_WIDGET (l->data);
response_id = gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);
if (is_stock_accept_response_id (response_id))
if (gtk_widget_is_sensitive (widget) &&
is_stock_accept_response_id (response_id))
{
gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */
break;

View file

@ -378,6 +378,7 @@ struct _GtkFileChooserDefault
guint show_preview_panel : 1;
guint drag_several : 1; /* a drag of all selected files */
guint create_folders : 1;
guint auto_selecting_first_row : 1;
#if 0
guint shortcuts_drag_outside : 1;

View file

@ -385,7 +385,11 @@ gtk_file_filter_filter (GtkFileFilter *filter,
filter_content_type = g_content_type_from_mime_type (filter_info->mime_type);
rule_content_type = g_content_type_from_mime_type (rule->u.mime_type);
match = g_content_type_is_a (filter_content_type, rule_content_type);
/* there may be no content type for a MIME type,
* e.g. one that is unknown on Windows */
match = filter_content_type != NULL &&
rule_content_type != NULL &&
g_content_type_is_a (filter_content_type, rule_content_type);
g_free (filter_content_type);
g_free (rule_content_type);

View file

@ -549,6 +549,9 @@ _gtk_icon_cache_get_icon (GtkIconCache *cache,
offset = find_image_offset (cache, icon_name, directory_index);
if (!offset)
return NULL;
image_data_offset = GET_UINT32 (cache->buffer, offset + 4);
if (!image_data_offset)

View file

@ -3505,9 +3505,10 @@ icon_info_ensure_scale_and_pixbuf (GtkIconInfo *icon_info,
icon_info->pixbuf = source_pixbuf;
else
{
/* never 0 pixels, even for a very long and thin image */
icon_info->pixbuf = icon_scale_pixbuf (source_pixbuf,
0.5 + image_width * icon_info->scale,
0.5 + image_height * icon_info->scale,
MAX (1, 0.5 + image_width * icon_info->scale),
MAX (1, 0.5 + image_height * icon_info->scale),
icon_info->scale);
_gtk_icon_pixbuf_mark_shared (icon_info->pixbuf);
g_object_unref (source_pixbuf);
@ -4029,8 +4030,8 @@ gtk_icon_theme_lookup_by_gicon (GtkIconTheme *icon_theme,
scale = (gdouble) size / (gdouble) max;
scaled = icon_scale_pixbuf (pixbuf,
0.5 + width * scale,
0.5 + height * scale,
MAX (1, 0.5 + width * scale),
MAX (1, 0.5 + height * scale),
scale);
info = gtk_icon_info_new_for_pixbuf (icon_theme, scaled);

View file

@ -1651,7 +1651,11 @@ gtk_path_bar_get_info_callback (GCancellable *cancellable,
gboolean info_is_hidden = FALSE;
gboolean info_is_backup = FALSE;
if (cancellable != file_info->path_bar->get_info_cancellable)
/* A cancelled lookup is no longer the path bar's (it was replaced or
* the path bar was disposed); the path bar may be gone, so do not
* look at it. */
if (cancelled ||
cancellable != file_info->path_bar->get_info_cancellable)
{
gtk_path_bar_set_file_finish (file_info, FALSE);
g_object_unref (cancellable);

View file

@ -368,7 +368,7 @@ get_is_recent_filtered (GtkRecentFilter *filter,
filter_info.contains |= GTK_RECENT_FILTER_DISPLAY_NAME;
}
else
filter_info.uri = NULL;
filter_info.display_name = NULL;
if (needed & GTK_RECENT_FILTER_APPLICATION)
{

View file

@ -1187,7 +1187,19 @@ gtk_recent_manager_add_item_query_info (GObject *source_object,
g_object_unref (file_info);
}
else
recent_data.mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME); /* FIXME: maybe we should make up the MIME type from the filename's extension */
{
/* the file is not there (yet) or cannot be asked: guess the type
* from the name instead of calling every such item a binary file */
gchar *basename = g_file_get_basename (file);
gchar *content_type = g_content_type_guess (basename, NULL, 0, NULL);
recent_data.mime_type = content_type ? g_content_type_get_mime_type (content_type) : NULL;
if (!recent_data.mime_type)
recent_data.mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
g_free (content_type);
g_free (basename);
}
recent_data.app_name = g_strdup (g_get_application_name ());
recent_data.app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);

View file

@ -86,6 +86,7 @@ G_DEFINE_TYPE_WITH_PRIVATE (GtkSearchEngineQuartz, _gtk_search_engine_quartz, GT
}
_gtk_search_engine_hits_added (engine, hits);
g_list_foreach (hits, (GFunc) g_free, NULL);
g_list_free (hits);
submitted_hits = [ns_query resultCount];

View file

@ -317,15 +317,19 @@ query_callback (GObject *object,
v = g_variant_iter_next_value (&iter);
strv = g_variant_get_strv (v, NULL);
result[i] = (gchar*)strv[0];
result[i] = g_strdup (strv[0]);
g_free (strv);
g_variant_unref (v);
}
/* We iterate result by result, not n at a time. */
hits = string_list_to_gslist (result);
_gtk_search_engine_hits_added (GTK_SEARCH_ENGINE (tracker), hits);
_gtk_search_engine_finished (GTK_SEARCH_ENGINE (tracker));
g_list_foreach (hits, (GFunc) g_free, NULL);
g_list_free (hits);
for (i = 0; i < n; i++)
g_free (result[i]);
g_free (result);
g_variant_unref (reply);
g_variant_unref (r);

View file

@ -1549,7 +1549,9 @@ build_cache (const gchar *path)
opentmp:
if ((fd = g_open (tmp_cache_path, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC | _O_BINARY, mode)) == -1)
{
if (force_update && retry_count == 0)
/* a temporary file left by a run that did not finish would
* otherwise keep the cache from ever being updated again */
if (retry_count == 0)
{
retry_count++;
g_remove (tmp_cache_path);