From c4bede86f6196d02f356eff49d3edb1a68f9b9b0 Mon Sep 17 00:00:00 2001 From: Tomas Bzatek Date: Wed, 22 Jul 2020 17:44:56 +0200 Subject: panel-run-dialog: Fix leaking tree model strings The gtk_tree_model_get() duplicates memory or adds a reference and any data need to be freed explicitly. --- mate-panel/panel-run-dialog.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/mate-panel/panel-run-dialog.c b/mate-panel/panel-run-dialog.c index f6fa95b8..fcd482ee 100644 --- a/mate-panel/panel-run-dialog.c +++ b/mate-panel/panel-run-dialog.c @@ -172,8 +172,8 @@ _panel_run_save_recent_programs_list (PanelRunDialog *dialog, model = gtk_combo_box_get_model (GTK_COMBO_BOX (entry)); /* reasonable upper bound for zero-terminated array with new command */ - gchar const *items[gtk_tree_model_iter_n_children (model, NULL) + 2]; - guint items_added = 0; + gchar *items[gtk_tree_model_iter_n_children (model, NULL) + 2]; + guint items_added = 0; items[0] = lastcommand; @@ -181,8 +181,10 @@ _panel_run_save_recent_programs_list (PanelRunDialog *dialog, char *command; do { gtk_tree_model_get (model, &iter, 0, &command, -1); - if (g_strcmp0 (command, lastcommand) == 0) + if (g_strcmp0 (command, lastcommand) == 0) { + g_free (command); continue; + } items[items_added + 1] = command; items_added++; } while (gtk_tree_model_iter_next (model, &iter)); @@ -192,13 +194,23 @@ _panel_run_save_recent_programs_list (PanelRunDialog *dialog, guint pos; for (pos = 0; pos < items_added / 2; pos++) { - gchar const * tmp = items[pos + 1]; + gchar *tmp = items[pos + 1]; items[pos + 1] = items[items_added - pos]; items[items_added - pos] = tmp; } } - items[history_max_size < items_added + 1 ? history_max_size : items_added + 1] = NULL; - g_settings_set_strv (dialog->settings, PANEL_RUN_HISTORY_KEY, items); + + if (history_max_size < items_added + 1) { + g_free (items[history_max_size]); + items[history_max_size] = NULL; + } else { + items[items_added + 1] = NULL; + } + g_settings_set_strv (dialog->settings, PANEL_RUN_HISTORY_KEY, (const gchar **)items); + + /* free the items */ + for (; items_added; items_added--) + g_free (items[items_added]); } } -- cgit v1.2.1