From 80ef61b967d33e398b0637ebfb5a0dae06ddd379 Mon Sep 17 00:00:00 2001
From: Dan Winship <danw@gnome.org>
Date: Thu, 15 Mar 2012 14:25:46 +0000
Subject: Fix some leaks

g_object_get() and gtk_tree_model_get() copy/ref the values they
return, so make sure to deal with that everywhere.

https://bugzilla.gnome.org/show_bug.cgi?id=679723
---
diff --git a/src/connection-editor/nm-connection-list.c b/src/connection-editor/nm-connection-list.c
index 455334d..09e84ea 100644
--- a/src/connection-editor/nm-connection-list.c
+++ b/src/connection-editor/nm-connection-list.c
@@ -127,6 +127,12 @@ get_active_connection (GtkTreeView *treeview)
 	g_list_foreach (selected_rows, (GFunc) gtk_tree_path_free, NULL);
 	g_list_free (selected_rows);
 
+	/* gtk_tree_model_get() will have reffed connection, but we don't
+	 * need that since we know the model will continue to hold a ref.
+	 */
+	if (connection)
+		g_object_unref (connection);
+
 	return connection;
 }
 
@@ -198,9 +204,9 @@ get_iter_for_connection (GtkTreeModel *model,
 		if (candidate && (candidate == connection)) {
 			*iter = temp_iter;
 			found = TRUE;
-			break;
 		}
-	} while (gtk_tree_model_iter_next (model, &temp_iter));
+		g_object_unref (candidate);
+	} while (!found && gtk_tree_model_iter_next (model, &temp_iter));
 
 	return found;
 }
diff --git a/src/connection-editor/page-wireless-security.c b/src/connection-editor/page-wireless-security.c
index 346a52a..09a6744 100644
--- a/src/connection-editor/page-wireless-security.c
+++ b/src/connection-editor/page-wireless-security.c
@@ -504,6 +504,8 @@ validate (CEPage *page, NMConnection *connection, GError **error)
 				valid = FALSE;
 			}
 		}
+
+		wireless_security_unref (sec);
 	} else {
 		/* No security, unencrypted */
 		g_object_set (s_wireless, NM_SETTING_WIRELESS_SEC, NULL, NULL);
@@ -519,9 +521,14 @@ static GtkWidget *
 nag_user (CEPage *page)
 {
 	WirelessSecurity *sec;
+	GtkWidget *nag = NULL;
 
 	sec = wireless_security_combo_get_active (CE_PAGE_WIRELESS_SECURITY (page));
-	return sec ? wireless_security_nag_user (sec) : NULL;
+	if (sec) {
+		nag = wireless_security_nag_user (sec);
+		wireless_security_unref (sec);
+	}
+	return nag;
 }
 
 static void
diff --git a/src/connection-editor/page-wireless.c b/src/connection-editor/page-wireless.c
index 886f175..3b1b57e 100644
--- a/src/connection-editor/page-wireless.c
+++ b/src/connection-editor/page-wireless.c
@@ -285,9 +285,9 @@ populate_ui (CEPageWireless *self)
 {
 	CEPageWirelessPrivate *priv = CE_PAGE_WIRELESS_GET_PRIVATE (self);
 	NMSettingWireless *setting = priv->setting;
-	const GByteArray *ssid = NULL;
-	const char *mode = NULL;
-	const char *band = NULL;
+	GByteArray *ssid = NULL;
+	char *mode = NULL;
+	char *band = NULL;
 	int band_idx = 0;
 	int rate_def;
 	int tx_power_def;
@@ -330,6 +330,7 @@ populate_ui (CEPageWireless *self)
 	gtk_entry_set_text (priv->ssid, utf8_ssid);
 	g_signal_connect_swapped (priv->ssid, "changed", G_CALLBACK (ce_page_changed), self);
 	g_free (utf8_ssid);
+	g_byte_array_unref (ssid);
 
 	/* Default to Infrastructure */
 	gtk_combo_box_set_active (priv->mode, 0);
@@ -337,6 +338,7 @@ populate_ui (CEPageWireless *self)
 		gtk_combo_box_set_active (priv->mode, 1);
 	mode_combo_changed_cb (priv->mode, self);
 	g_signal_connect (priv->mode, "changed", G_CALLBACK (mode_combo_changed_cb), self);
+	g_free (mode);
 
 	g_signal_connect (priv->channel, "output",
 	                  G_CALLBACK (channel_spin_output_cb),
@@ -354,6 +356,7 @@ populate_ui (CEPageWireless *self)
 			band_idx = 2;
 			gtk_widget_set_sensitive (GTK_WIDGET (priv->channel), TRUE);
 		}
+		g_free (band);
 	}
 
 	gtk_combo_box_set_active (priv->band, band_idx);
diff --git a/src/connection-editor/vpn-helpers.c b/src/connection-editor/vpn-helpers.c
index 12547fe..55ef432 100644
--- a/src/connection-editor/vpn-helpers.c
+++ b/src/connection-editor/vpn-helpers.c
@@ -427,17 +427,22 @@ sort_plugins (gconstpointer a, gconstpointer b)
 {
 	NMVpnPluginUiInterface *aa = NM_VPN_PLUGIN_UI_INTERFACE (a);
 	NMVpnPluginUiInterface *bb = NM_VPN_PLUGIN_UI_INTERFACE (b);
-	const char *aa_desc = NULL, *bb_desc = NULL;
+	char *aa_desc = NULL, *bb_desc = NULL;
+	gint ret;
 
 	g_object_get (aa, NM_VPN_PLUGIN_UI_INTERFACE_NAME, &aa_desc, NULL);
 	g_object_get (bb, NM_VPN_PLUGIN_UI_INTERFACE_NAME, &bb_desc, NULL);
 
 	if (!aa_desc)
-		return -1;
-	if (!bb_desc)
-		return 1;
-
-	return strcmp (aa_desc, bb_desc);
+		ret = -1;
+	else if (!bb_desc)
+		ret = 1;
+	else
+		ret = strcmp (aa_desc, bb_desc);
+
+	g_free (aa_desc);
+	g_free (bb_desc);
+	return ret;
 }
 
 #define COL_PLUGIN_DESC 0
@@ -450,7 +455,7 @@ combo_changed_cb (GtkComboBox *combo, gpointer user_data)
 	GtkTreeModel *model;
 	GtkTreeIter iter;
 	NMVpnPluginUiInterface *plugin = NULL;
-	const char *desc = NULL;
+	char *desc = NULL;
 	char *tmp;
 
 	if (!gtk_combo_box_get_active_iter (combo, &iter))
@@ -465,12 +470,14 @@ combo_changed_cb (GtkComboBox *combo, gpointer user_data)
 		goto error;
 
 	g_object_get (G_OBJECT (plugin), NM_VPN_PLUGIN_UI_INTERFACE_DESC, &desc, NULL);
+	g_object_unref (plugin);
 	if (!desc)
 		goto error;
 
 	tmp = g_strdup_printf ("<i>%s</i>", desc);
 	gtk_label_set_markup (label, tmp);
 	g_free (tmp);
+	g_free (desc);
 	return;
 
 error:
@@ -515,13 +522,14 @@ vpn_ask_connection_type (GtkWindow *parent)
 	plugin_list = g_slist_sort (plugin_list, sort_plugins);
 	for (iter = plugin_list; iter; iter = g_slist_next (iter)) {
 		NMVpnPluginUiInterface *plugin = NM_VPN_PLUGIN_UI_INTERFACE (iter->data);
-		const char *desc;
+		char *desc;
 
 		gtk_list_store_append (GTK_LIST_STORE (model), &tree_iter);
 		g_object_get (plugin, NM_VPN_PLUGIN_UI_INTERFACE_NAME, &desc, NULL);
 		gtk_list_store_set (GTK_LIST_STORE (model), &tree_iter,
 		                    COL_PLUGIN_DESC, desc,
 		                    COL_PLUGIN_OBJ, plugin, -1);
+		g_free (desc);
 	}
 
 	combo = GTK_WIDGET (gtk_builder_get_object (builder, "vpn_type_combo"));
@@ -540,15 +548,17 @@ vpn_ask_connection_type (GtkWindow *parent)
 		NMVpnPluginUiInterface *plugin = NULL;
 
 		gtk_tree_model_get (model, &tree_iter, COL_PLUGIN_OBJ, &plugin, -1);
-		if (plugin)
+		if (plugin) {
 			g_object_get (G_OBJECT (plugin), NM_VPN_PLUGIN_UI_INTERFACE_SERVICE, &service_type, NULL);
+			g_object_unref (plugin);
+		}
 	}
 
 out:
 	gtk_widget_destroy (dialog);
 	g_object_unref (builder);
 	if (service_type)
-		return g_strdup (service_type);
+		return service_type;
 	return NULL;
 }
 
diff --git a/src/libnm-gtk/nm-mobile-wizard.c b/src/libnm-gtk/nm-mobile-wizard.c
index 2b91fa9..0966c99 100644
--- a/src/libnm-gtk/nm-mobile-wizard.c
+++ b/src/libnm-gtk/nm-mobile-wizard.c
@@ -171,6 +171,8 @@ assistant_closed (GtkButton *button, gpointer user_data)
 
 	(*(self->callback)) (self, FALSE, wiz_method, self->user_data);
 
+	if (provider)
+		nmn_mobile_provider_unref (provider);
 	g_free (wiz_method->provider_name);
 	g_free (wiz_method->plan_name);
 	g_free (wiz_method->username);
@@ -292,17 +294,20 @@ confirm_prepare (NMAMobileWizard *self)
 
 	/* Provider */
 	str = g_string_new (NULL);
-	if (provider)
+	if (provider) {
 		g_string_append (str, provider->name);
-	else {
+		nmn_mobile_provider_unref (provider);
+	} else {
 		const char *unlisted_provider;
 
 		unlisted_provider = gtk_entry_get_text (GTK_ENTRY (self->provider_unlisted_entry));
 		g_string_append (str, unlisted_provider);
 	}
 
-	if (country)
+	if (country) {
 		g_string_append_printf (str, ", %s", country);
+		g_free (country);
+	}
 	gtk_label_set_text (GTK_LABEL (self->confirm_provider), str->str);
 	g_string_free (str, TRUE);
 
@@ -576,6 +581,7 @@ plan_prepare (NMAMobileWizard *self)
 			                    -1);
 			count++;
 		}
+		nmn_mobile_provider_unref (provider);
 
 		/* Draw the separator */
 		if (count)
diff --git a/src/libnm-gtk/nm-wireless-dialog.c b/src/libnm-gtk/nm-wireless-dialog.c
index f17c776..885a1c6 100644
--- a/src/libnm-gtk/nm-wireless-dialog.c
+++ b/src/libnm-gtk/nm-wireless-dialog.c
@@ -112,25 +112,6 @@ nma_wireless_dialog_get_nag_ignored (NMAWirelessDialog *self)
 }
 
 static void
-model_free (GtkTreeModel *model, guint col)
-{
-	GtkTreeIter	iter;
-
-	if (!model)
-		return;
-
-	if (gtk_tree_model_get_iter_first (model, &iter)) {
-		do {
-			char *str;
-
-			gtk_tree_model_get (model, &iter, col, &str, -1);
-			g_free (str);
-		} while (gtk_tree_model_iter_next (model, &iter));
-	}
-	g_object_unref (model);
-}
-
-static void
 size_group_clear (GtkSizeGroup *group)
 {
 	GSList *iter;
@@ -427,7 +408,7 @@ connection_combo_init (NMAWirelessDialog *self, NMConnection *connection)
 	g_return_val_if_fail (priv->connection == NULL, FALSE);
 
 	/* Clear any old model */
-	model_free (priv->connection_model, C_NAME_COLUMN);
+	g_object_unref (priv->connection_model);
 
 	/* New model */
 	store = gtk_list_store_new (4, G_TYPE_STRING, G_TYPE_OBJECT, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN);
@@ -1358,7 +1339,7 @@ GtkWidget *
 nma_wireless_dialog_nag_user (NMAWirelessDialog *self)
 {
 	NMAWirelessDialogPrivate *priv;
-	GtkWidget *combo;
+	GtkWidget *combo, *nag;
 	GtkTreeModel *model;
 	GtkTreeIter iter;
 	WirelessSecurity *sec = NULL;
@@ -1378,8 +1359,11 @@ nma_wireless_dialog_nag_user (NMAWirelessDialog *self)
 	}
 
 	gtk_tree_model_get (model, &iter, S_SEC_COLUMN, &sec, -1);
-	if (sec)
-		return wireless_security_nag_user (sec);
+	if (sec) {
+		nag = wireless_security_nag_user (sec);
+		wireless_security_unref (sec);
+		return nag;
+	}
 
 	return NULL;
 }
@@ -1417,8 +1401,8 @@ dispose (GObject *object)
 	g_object_unref (priv->settings);
 	g_object_unref (priv->builder);
 
-	model_free (priv->device_model, D_NAME_COLUMN);
-	model_free (priv->connection_model, C_NAME_COLUMN);
+	g_object_unref (priv->device_model);
+	g_object_unref (priv->connection_model);
 
 	if (priv->group)
 		g_object_unref (priv->group);
diff --git a/src/wireless-security/wireless-security.c b/src/wireless-security/wireless-security.c
index 00cc637..db51920 100644
--- a/src/wireless-security/wireless-security.c
+++ b/src/wireless-security/wireless-security.c
@@ -544,6 +544,8 @@ ws_802_1x_nag_user (WirelessSecurity *sec,
 	gtk_tree_model_get (model, &iter, AUTH_METHOD_COLUMN, &eap, -1);
 	g_return_val_if_fail (eap != NULL, NULL);
 
-	return eap_method_nag_user (eap);
+	widget = eap_method_nag_user (eap);
+	eap_method_unref (eap);
+	return widget;
 }
 
--
cgit v0.9.0.2
