From dad9089a05bb9f71c3d68980430d4cb29eadae49 Mon Sep 17 00:00:00 2001 From: andy5995 Date: Sun, 18 Feb 2024 17:36:54 -0600 Subject: [PATCH] Fix crash when pinning or unpinning 'untitled' doc Related to https://github.com/geany/geany/pull/3770 --- pinner.c | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/pinner.c b/pinner.c index 1558805..e9f5042 100644 --- a/pinner.c +++ b/pinner.c @@ -64,7 +64,7 @@ destroy_widget(gpointer pdata) gtk_widget_destroy(widget); } -void +static void clear_pinned_documents(void) { if (doc_to_widget_map != NULL) { @@ -167,28 +167,27 @@ static void pin_activate_cb(GtkMenuItem* menuitem, gpointer pdata) { (void)menuitem; + (void)pdata; - gchar* ptr_file_name = NULL; - if (pdata == NULL) { - GeanyDocument* doc = document_get_current(); - if (doc == NULL) - return; - else - ptr_file_name = doc->file_name; - } else { - ptr_file_name = pdata; - } + GeanyDocument* doc = document_get_current(); + if (!(doc && doc->is_valid)) + return; + + // See https://github.com/geany/geany/pull/3770 for more info on + // Why this check is necessary even after checking if doc == NULL + if (doc->file_name == NULL) + return; - if (is_duplicate(ptr_file_name)) + if (is_duplicate(doc->file_name)) return; /* This must be freed when nodes are removed from the list */ - gchar* tmp_file_name = g_strdup(ptr_file_name); + gchar* tmp_file_name = g_strdup(doc->file_name); GtkWidget* event_box = gtk_event_box_new(); g_hash_table_insert(doc_to_widget_map, tmp_file_name, event_box); - GtkWidget* label = gtk_label_new(ptr_file_name); + GtkWidget* label = gtk_label_new(doc->file_name); // Enable ellipsizing at the start of the filename gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_START); gtk_label_set_max_width_chars(GTK_LABEL(label), 30); @@ -216,14 +215,22 @@ pin_activate_cb(GtkMenuItem* menuitem, gpointer pdata) static void unpin_activate_cb(GtkMenuItem* menuitem, gpointer pdata) { - (void)pdata; (void)menuitem; GeanyDocument* doc = document_get_current(); - if (doc == NULL) - return; + gchar* ptr_file_name = pdata; + + if (!pdata) { + if (!(doc && doc->is_valid)) + return; + + if (doc->file_name == NULL) + return; + else + ptr_file_name = doc->file_name; + } - gboolean removed = g_hash_table_remove(doc_to_widget_map, doc->file_name); + gboolean removed = g_hash_table_remove(doc_to_widget_map, ptr_file_name); // If removed if (!removed) { // Handle the case where the document was not found in the map