changeset 27430:e400cd35542b

I was seeing some crashes when connecting to Google Talk on my work laptop. When looking into it I noticed that the Windows code was using GSLists and the common code was using GLists. I figured that was a bad thing. I also noticed a small use-after-free issue.
author Etan Reisner <pidgin@unreliablesource.net>
date Sun, 05 Jul 2009 05:04:33 +0000
parents 4fd6b50d03b3
children a71cc338f0fd
files libpurple/dnssrv.c
diffstat 1 files changed, 35 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/dnssrv.c	Sat Jul 04 17:36:07 2009 +0000
+++ b/libpurple/dnssrv.c	Sun Jul 05 05:04:33 2009 +0000
@@ -82,7 +82,7 @@
 	GThread *resolver;
 	char *query;
 	char *error_message;
-	GSList *results;
+	GList *results;
 #else
 	int fd_in, fd_out;
 	pid_t pid;
@@ -134,8 +134,8 @@
  * @param container_ptr The PurpleSrvResponseContainer that was chosen
  *        will be returned here.
  */
-static GList *select_random_response(GList *list,
-		PurpleSrvResponseContainer **container_ptr)
+static GList *
+select_random_response(GList *list, PurpleSrvResponseContainer **container_ptr)
 {
 	GList *cur;
 	size_t runningtotal;
@@ -173,11 +173,11 @@
  * Reorder a GList of PurpleSrvResponses that have the same priority
  * (aka "pref").
  */
-static void srv_reorder(GList *list, int num)
+static void
+srv_reorder(GList *list, int num)
 {
 	int i;
-	GList *cur;
-	GList *container_list = NULL;
+	GList *cur, *container_list = NULL;
 	PurpleSrvResponseContainer *container;
 
 	if (num < 2)
@@ -206,21 +206,22 @@
 }
 
 /**
- * Sorts a GList of PurpleSrvResponse's according to the
+ * Sorts a GList of PurpleSrvResponses according to the
  * algorithm described in RFC 2782.
  *
  * @param response GList of PurpleSrvResponse's
  * @param The original list, resorted
  */
-static GList *purple_srv_sort(GList *list)
+static GList *
+purple_srv_sort(GList *list)
 {
+	int pref, count;
 	GList *cur, *start;
-	int count;
-	int pref;
 
-	if (!list || !list->next)
+	if (!list || !list->next) {
 		/* Nothing to sort */
 		return list;
+	}
 
 	list = g_list_sort(list, responsecompare);
 
@@ -255,11 +256,8 @@
 	PurpleSrvResponse *srvres;
 	PurpleTxtResponse *txtres;
 	queryans answer;
-	int size;
-	int qdcount;
-	int ancount;
-	guchar *end;
-	guchar *cp;
+	int size, qdcount, ancount;
+	guchar *end, *cp;
 	gchar name[256];
 	guint16 type, dlen, pref, weight, port;
 	PurpleSrvInternalQuery query;
@@ -348,8 +346,11 @@
 	while (ret != NULL)
 	{
 		/* TODO: Check return value */
-		if (query.type == T_SRV) write(out, ret->data, sizeof(PurpleSrvResponse));
-		if (query.type == T_TXT) write(out, ret->data, sizeof(PurpleTxtResponse));
+		if (query.type == T_SRV)
+			write(out, ret->data, sizeof(PurpleSrvResponse));
+		if (query.type == T_TXT)
+			write(out, ret->data, sizeof(PurpleTxtResponse));
+
 		g_free(ret->data);
 		ret = g_list_remove(ret, ret->data);
 	}
@@ -410,7 +411,7 @@
 
 					cb(res, size, query_data->extradata);
 				} else if (type == T_TXT) {
-					GSList *responses = NULL;
+					GList *responses = NULL;
 					PurpleTxtResponse *res;
 					PurpleTxtCallback cb = query_data->cb.txt;
 					ssize_t red;
@@ -423,8 +424,8 @@
 									"response: %s\n", g_strerror(errno));
 							size = 0;
 							g_free(res);
-							g_slist_foreach(responses, (GFunc)purple_txt_response_destroy, NULL);
-							g_slist_free(responses);
+							g_list_foreach(responses, (GFunc)purple_txt_response_destroy, NULL);
+							g_list_free(responses);
 							responses = NULL;
 							break;
 						}
@@ -450,24 +451,24 @@
 res_main_thread_cb(gpointer data)
 {
 	PurpleSrvResponse *srvres = NULL;
-	int size = 0;
 	PurpleSrvQueryData *query_data = data;
 	if(query_data->error_message != NULL)
 		purple_debug_error("dnssrv", query_data->error_message);
 	else {
 		if (query_data->type == DNS_TYPE_SRV) {
 			PurpleSrvResponse *srvres_tmp = NULL;
-			GSList *lst = query_data->results;
-
-			size = g_slist_length(lst);
+			GList *lst = query_data->results;
+			int size = g_list_length(lst);
 
 			if(query_data->cb.srv && size > 0)
 				srvres_tmp = srvres = g_new0(PurpleSrvResponse, size);
 			while (lst) {
+				PurpleSrvResponse *lstdata = lst->data;
+				lst = g_list_delete_link(lst, lst);
+
 				if(query_data->cb.srv)
-					memcpy(srvres_tmp++, lst->data, sizeof(PurpleSrvResponse));
-				g_free(lst->data);
-				lst = g_slist_remove(lst, lst->data);
+					memcpy(srvres_tmp++, lstdata, sizeof(PurpleSrvResponse));
+				g_free(lstdata);
 			}
 
 			query_data->results = NULL;
@@ -476,9 +477,9 @@
 
 			if(query_data->cb.srv) query_data->cb.srv(srvres, size, query_data->extradata);
 		} else if (query_data->type == DNS_TYPE_TXT) {
-			GSList *lst = query_data->results;
+			GList *lst = query_data->results;
 
-			purple_debug_info("dnssrv", "found %d TXT entries\n", g_slist_length(lst));
+			purple_debug_info("dnssrv", "found %d TXT entries\n", g_list_length(lst));
 
 			if (query_data->cb.txt) {
 				query_data->results = NULL;
@@ -517,7 +518,7 @@
 	} else {
 		if (type == DNS_TYPE_SRV) {
 			PDNS_RECORD dr_tmp;
-			GSList *lst = NULL;
+			GList *lst = NULL;
 			DNS_SRV_DATA *srv_data;
 			PurpleSrvResponse *srvres;
 
@@ -535,14 +536,14 @@
 				srvres->port = srv_data->wPort;
 				srvres->weight = srv_data->wWeight;
 
-				lst = g_slist_prepend(lst, srvres);
+				lst = g_list_prepend(lst, srvres);
 			}
 
 			MyDnsRecordListFree(dr, DnsFreeRecordList);
 			query_data->results = purple_srv_sort(lst);
 		} else if (type == DNS_TYPE_TXT) {
 			PDNS_RECORD dr_tmp;
-			GSList *lst = NULL;
+			GList *lst = NULL;
 			DNS_TXT_DATA *txt_data;
 			PurpleTxtResponse *txtres;
 
@@ -563,7 +564,7 @@
 					s = g_string_append(s, txt_data->pStringArray[i]);
 				txtres->content = g_string_free(s, FALSE);
 
-				lst = g_slist_append(lst, txtres);
+				lst = g_list_append(lst, txtres);
 			}
 
 			MyDnsRecordListFree(dr, DnsFreeRecordList);