From 080ff7fa8aa1a3e031a5fe940f36afb5debe9ea7 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Tue, 5 Jul 2022 22:08:38 +0300 Subject: [PATCH 25/25] gtk: Fix NULL dereference in diplodlg with no available cities See osdn #44833 Signed-off-by: Marko Lindqvist --- client/gui-gtk-2.0/diplodlg.c | 56 ++++++++++++++++++---------------- client/gui-gtk-3.0/diplodlg.c | 56 ++++++++++++++++++---------------- client/gui-gtk-3.22/diplodlg.c | 54 +++++++++++++++++--------------- 3 files changed, 89 insertions(+), 77 deletions(-) diff --git a/client/gui-gtk-2.0/diplodlg.c b/client/gui-gtk-2.0/diplodlg.c index 8a3ca69da1..751fc9a8eb 100644 --- a/client/gui-gtk-2.0/diplodlg.c +++ b/client/gui-gtk-2.0/diplodlg.c @@ -341,43 +341,48 @@ static void popup_add_menu(GtkMenuShell *parent, gpointer data) /**************************************************************** Creates a sorted list of plr0's cities, excluding the capital and - any cities not visible to plr1. This means that you can only trade - cities visible to requesting player. + any cities not visible to plr1. This means that you can only trade + cities visible to requesting player. - Kris Bubendorfer *****************************************************************/ if (game.info.trading_city) { - int i = 0, j = 0, n = city_list_size(pgiver->cities); - struct city **city_list_ptrs; + int i = 0; + int n = city_list_size(pgiver->cities); + + menu = gtk_menu_new(); if (n > 0) { + struct city **city_list_ptrs; + city_list_ptrs = fc_malloc(sizeof(struct city *) * n); - } else { - city_list_ptrs = NULL; - } - city_list_iterate(pgiver->cities, pcity) { - if (!is_capital(pcity)) { - city_list_ptrs[i] = pcity; - i++; - } - } city_list_iterate_end; + city_list_iterate(pgiver->cities, pcity) { + if (!is_capital(pcity)) { + city_list_ptrs[i] = pcity; + i++; + } + } city_list_iterate_end; - qsort(city_list_ptrs, i, sizeof(struct city *), city_name_compare); - - menu = gtk_menu_new(); + if (i > 0) { /* Cities other than capitals */ + int j; - for (j = 0; j < i; j++) { - item = gtk_menu_item_new_with_label(city_name_get(city_list_ptrs[j])); + qsort(city_list_ptrs, i, sizeof(struct city *), city_name_compare); - gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); - g_signal_connect(item, "activate", - G_CALLBACK(diplomacy_dialog_city_callback), - GINT_TO_POINTER((player_number(pgiver) << 24) | - (player_number(pother) << 16) | - city_list_ptrs[j]->id)); + for (j = 0; j < i; j++) { + item = gtk_menu_item_new_with_label(city_name_get(city_list_ptrs[j])); + + gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); + g_signal_connect(item, "activate", + G_CALLBACK(diplomacy_dialog_city_callback), + GINT_TO_POINTER((player_number(pgiver) << 24) | + (player_number(pother) << 16) | + city_list_ptrs[j]->id)); + } + } + + free(city_list_ptrs); } - free(city_list_ptrs); item = gtk_menu_item_new_with_mnemonic(_("_Cities")); gtk_widget_set_sensitive(item, (i > 0)); @@ -386,7 +391,6 @@ static void popup_add_menu(GtkMenuShell *parent, gpointer data) gtk_widget_show_all(item); } - /* Give shared vision. */ item = gtk_menu_item_new_with_mnemonic(_("_Give shared vision")); g_object_set_data(G_OBJECT(item), "plr", pgiver); diff --git a/client/gui-gtk-3.0/diplodlg.c b/client/gui-gtk-3.0/diplodlg.c index 137a78e8cf..c6d331c3dd 100644 --- a/client/gui-gtk-3.0/diplodlg.c +++ b/client/gui-gtk-3.0/diplodlg.c @@ -341,43 +341,48 @@ static void popup_add_menu(GtkMenuShell *parent, gpointer data) /**************************************************************** Creates a sorted list of plr0's cities, excluding the capital and - any cities not visible to plr1. This means that you can only trade - cities visible to requesting player. + any cities not visible to plr1. This means that you can only trade + cities visible to requesting player. - Kris Bubendorfer *****************************************************************/ if (game.info.trading_city) { - int i = 0, j = 0, n = city_list_size(pgiver->cities); - struct city **city_list_ptrs; + int i = 0; + int n = city_list_size(pgiver->cities); + + menu = gtk_menu_new(); if (n > 0) { + struct city **city_list_ptrs; + city_list_ptrs = fc_malloc(sizeof(struct city *) * n); - } else { - city_list_ptrs = NULL; - } - city_list_iterate(pgiver->cities, pcity) { - if (!is_capital(pcity)) { - city_list_ptrs[i] = pcity; - i++; - } - } city_list_iterate_end; + city_list_iterate(pgiver->cities, pcity) { + if (!is_capital(pcity)) { + city_list_ptrs[i] = pcity; + i++; + } + } city_list_iterate_end; - qsort(city_list_ptrs, i, sizeof(struct city *), city_name_compare); - - menu = gtk_menu_new(); + if (i > 0) { /* Cities other than capitals */ + int j; - for (j = 0; j < i; j++) { - item = gtk_menu_item_new_with_label(city_name_get(city_list_ptrs[j])); + qsort(city_list_ptrs, i, sizeof(struct city *), city_name_compare); - gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); - g_signal_connect(item, "activate", - G_CALLBACK(diplomacy_dialog_city_callback), - GINT_TO_POINTER((player_number(pgiver) << 24) | - (player_number(pother) << 16) | - city_list_ptrs[j]->id)); + for (j = 0; j < i; j++) { + item = gtk_menu_item_new_with_label(city_name_get(city_list_ptrs[j])); + + gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); + g_signal_connect(item, "activate", + G_CALLBACK(diplomacy_dialog_city_callback), + GINT_TO_POINTER((player_number(pgiver) << 24) | + (player_number(pother) << 16) | + city_list_ptrs[j]->id)); + } + } + + free(city_list_ptrs); } - free(city_list_ptrs); item = gtk_menu_item_new_with_mnemonic(_("_Cities")); gtk_widget_set_sensitive(item, (i > 0)); @@ -386,7 +391,6 @@ static void popup_add_menu(GtkMenuShell *parent, gpointer data) gtk_widget_show_all(item); } - /* Give shared vision. */ item = gtk_menu_item_new_with_mnemonic(_("_Give shared vision")); g_object_set_data(G_OBJECT(item), "plr", pgiver); diff --git a/client/gui-gtk-3.22/diplodlg.c b/client/gui-gtk-3.22/diplodlg.c index 3c87764c69..19999ee468 100644 --- a/client/gui-gtk-3.22/diplodlg.c +++ b/client/gui-gtk-3.22/diplodlg.c @@ -341,43 +341,48 @@ static void popup_add_menu(GtkMenuShell *parent, gpointer data) /**************************************************************** Creates a sorted list of plr0's cities, excluding the capital and - any cities not visible to plr1. This means that you can only trade - cities visible to requesting player. + any cities not visible to plr1. This means that you can only trade + cities visible to requesting player. - Kris Bubendorfer *****************************************************************/ if (game.info.trading_city) { - int i = 0, j = 0, n = city_list_size(pgiver->cities); - struct city **city_list_ptrs; + int i = 0; + int n = city_list_size(pgiver->cities); + + menu = gtk_menu_new(); if (n > 0) { + struct city **city_list_ptrs; + city_list_ptrs = fc_malloc(sizeof(struct city *) * n); - } else { - city_list_ptrs = NULL; - } - city_list_iterate(pgiver->cities, pcity) { - if (!is_capital(pcity)) { - city_list_ptrs[i] = pcity; - i++; - } - } city_list_iterate_end; + city_list_iterate(pgiver->cities, pcity) { + if (!is_capital(pcity)) { + city_list_ptrs[i] = pcity; + i++; + } + } city_list_iterate_end; - qsort(city_list_ptrs, i, sizeof(struct city *), city_name_compare); + if (i > 0) { /* Cities other than capitals */ + int j; - menu = gtk_menu_new(); + qsort(city_list_ptrs, i, sizeof(struct city *), city_name_compare); - for (j = 0; j < i; j++) { - item = gtk_menu_item_new_with_label(city_name_get(city_list_ptrs[j])); + for (j = 0; j < i; j++) { + item = gtk_menu_item_new_with_label(city_name_get(city_list_ptrs[j])); - gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); - g_signal_connect(item, "activate", - G_CALLBACK(diplomacy_dialog_city_callback), - GINT_TO_POINTER((player_number(pgiver) << 24) | - (player_number(pother) << 16) | - city_list_ptrs[j]->id)); + gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); + g_signal_connect(item, "activate", + G_CALLBACK(diplomacy_dialog_city_callback), + GINT_TO_POINTER((player_number(pgiver) << 24) | + (player_number(pother) << 16) | + city_list_ptrs[j]->id)); + } + } + + free(city_list_ptrs); } - free(city_list_ptrs); item = gtk_menu_item_new_with_mnemonic(_("_Cities")); gtk_widget_set_sensitive(item, (i > 0)); @@ -386,7 +391,6 @@ static void popup_add_menu(GtkMenuShell *parent, gpointer data) gtk_widget_show_all(item); } - /* Give shared vision. */ item = gtk_menu_item_new_with_mnemonic(_("_Give shared vision")); g_object_set_data(G_OBJECT(item), "plr", pgiver); -- 2.35.1