changeset 27106:c59918579c3a

Strip unprintable UTF-8 characters from buddy list strings. Fixes #8776. buddy, contact, and chat aliases and group names are all cleaned at assignment. Presumably, buddy names won't include bad characters.
author Paul Aurich <paul@darkrain42.org>
date Wed, 03 Jun 2009 22:12:28 +0000
parents 7c864f388c96
children 6bd7b3e06233
files libpurple/blist.c libpurple/util.c
diffstat 2 files changed, 69 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/blist.c	Wed Jun 03 21:59:58 2009 +0000
+++ b/libpurple/blist.c	Wed Jun 03 22:12:28 2009 +0000
@@ -977,18 +977,26 @@
 	PurpleConversation *conv;
 	PurpleBlistNode *bnode;
 	char *old_alias;
+	char *new_alias = NULL;
 
 	g_return_if_fail(contact != NULL);
 
-	if (!purple_strings_are_different(contact->alias, alias))
+	if ((alias != NULL) && (*alias != '\0'))
+		new_alias = purple_utf8_strip_unprintables(alias);
+
+	if (!purple_strings_are_different(contact->alias, new_alias)) {
+		g_free(new_alias);
 		return;
+	}
 
 	old_alias = contact->alias;
 
-	if ((alias != NULL) && (*alias != '\0'))
-		contact->alias = g_strdup(alias);
-	else
+	if ((new_alias != NULL) && (*new_alias != '\0'))
+		contact->alias = new_alias;
+	else {
 		contact->alias = NULL;
+		g_free(new_alias); /* could be "\0" */
+	}
 
 	purple_blist_schedule_save();
 
@@ -1014,18 +1022,26 @@
 {
 	PurpleBlistUiOps *ops = purple_blist_get_ui_ops();
 	char *old_alias;
+	char *new_alias = NULL;
 
 	g_return_if_fail(chat != NULL);
 
-	if (!purple_strings_are_different(chat->alias, alias))
+	if ((alias != NULL) && (*alias != '\0'))
+		new_alias = purple_utf8_strip_unprintables(alias);
+
+	if (!purple_strings_are_different(chat->alias, new_alias)) {
+		g_free(new_alias);
 		return;
+	}
 
 	old_alias = chat->alias;
 
-	if ((alias != NULL) && (*alias != '\0'))
-		chat->alias = g_strdup(alias);
-	else
+	if ((new_alias != NULL) && (*new_alias != '\0'))
+		chat->alias = new_alias;
+	else {
 		chat->alias = NULL;
+		g_free(new_alias); /* could be "\0" */
+	}
 
 	purple_blist_schedule_save();
 
@@ -1042,18 +1058,26 @@
 	PurpleBlistUiOps *ops = purple_blist_get_ui_ops();
 	PurpleConversation *conv;
 	char *old_alias;
+	char *new_alias = NULL;
 
 	g_return_if_fail(buddy != NULL);
 
-	if (!purple_strings_are_different(buddy->alias, alias))
+	if ((alias != NULL) && (*alias != '\0'))
+		new_alias = purple_utf8_strip_unprintables(alias);
+
+	if (!purple_strings_are_different(buddy->alias, new_alias)) {
+		g_free(new_alias);
 		return;
+	}
 
 	old_alias = buddy->alias;
 
-	if ((alias != NULL) && (*alias != '\0'))
+	if ((new_alias != NULL) && (*new_alias != '\0'))
 		buddy->alias = g_strdup(alias);
-	else
+	else {
 		buddy->alias = NULL;
+		g_free(new_alias); /* could be "\0" */
+	}
 
 	purple_blist_schedule_save();
 
@@ -1075,18 +1099,26 @@
 	PurpleBlistUiOps *ops = purple_blist_get_ui_ops();
 	PurpleConversation *conv;
 	char *old_alias;
+	char *new_alias = NULL;
 
 	g_return_if_fail(buddy != NULL);
 
-	if (!purple_strings_are_different(buddy->server_alias, alias))
+	if ((alias != NULL) && (*alias != '\0') && g_utf8_validate(alias, -1, NULL))
+		new_alias = purple_utf8_strip_unprintables(alias);
+
+	if (!purple_strings_are_different(buddy->server_alias, new_alias)) {
+		g_free(new_alias);
 		return;
+	}
 
 	old_alias = buddy->server_alias;
 
-	if ((alias != NULL) && (*alias != '\0') && g_utf8_validate(alias, -1, NULL))
-		buddy->server_alias = g_strdup(alias);
-	else
+	if ((new_alias != NULL) && (*new_alias != '\0'))
+		buddy->server_alias = new_alias;
+	else {
 		buddy->server_alias = NULL;
+		g_free(new_alias); /* could be "\0"; */
+	}
 
 	purple_blist_schedule_save();
 
@@ -1106,19 +1138,24 @@
 /*
  * TODO: If merging, prompt the user if they want to merge.
  */
-void purple_blist_rename_group(PurpleGroup *source, const char *new_name)
+void purple_blist_rename_group(PurpleGroup *source, const char *name)
 {
 	PurpleBlistUiOps *ops = purple_blist_get_ui_ops();
 	PurpleGroup *dest;
 	gchar *old_name;
+	gchar *new_name;
 	GList *moved_buddies = NULL;
 	GSList *accts;
 
 	g_return_if_fail(source != NULL);
-	g_return_if_fail(new_name != NULL);
-
-	if (*new_name == '\0' || purple_strequal(new_name, source->name))
+	g_return_if_fail(name != NULL);
+
+	new_name = purple_utf8_strip_unprintables(name);
+
+	if (*new_name == '\0' || purple_strequal(new_name, source->name)) {
+		g_free(new_name);
 		return;
+	}
 
 	dest = purple_find_group(new_name);
 	if (dest != NULL && purple_utf8_strcasecmp(source->name, dest->name) != 0) {
@@ -1160,6 +1197,7 @@
 		old_name = g_strdup(source->name);
 		purple_blist_remove_group(source);
 		source = dest;
+		g_free(new_name);
 	} else {
 		/* A simple rename */
 		PurpleBlistNode *cnode, *bnode;
@@ -1172,7 +1210,7 @@
 		}
 
 		old_name = source->name;
-		source->name = g_strdup(new_name);
+		source->name = new_name;
 	}
 
 	/* Save our changes */
@@ -1184,7 +1222,7 @@
 
 	/* Notify all PRPLs */
 	/* TODO: Is this condition needed?  Seems like it would always be TRUE */
-	if(old_name && purple_strequal(source->name, old_name)) {
+	if(old_name && !purple_strequal(source->name, old_name)) {
 		for (accts = purple_group_get_accounts(source); accts; accts = g_slist_remove(accts, accts->data)) {
 			PurpleAccount *account = accts->data;
 			PurpleConnection *gc = NULL;
@@ -1246,7 +1284,7 @@
 	chat = g_new0(PurpleChat, 1);
 	chat->account = account;
 	if ((alias != NULL) && (*alias != '\0'))
-		chat->alias = g_strdup(alias);
+		chat->alias = purple_utf8_strip_unprintables(alias);
 	chat->components = components;
 	purple_blist_node_initialize_settings((PurpleBlistNode *)chat);
 	((PurpleBlistNode *)chat)->type = PURPLE_BLIST_CHAT_NODE;
@@ -1273,13 +1311,13 @@
 	PurpleBlistUiOps *ops = purple_blist_get_ui_ops();
 	PurpleBuddy *buddy;
 
-	g_return_val_if_fail(account != NULL, FALSE);
-	g_return_val_if_fail(name != NULL, FALSE);
+	g_return_val_if_fail(account != NULL, NULL);
+	g_return_val_if_fail(name != NULL, NULL);
 
 	buddy = g_new0(PurpleBuddy, 1);
 	buddy->account  = account;
-	buddy->name     = g_strdup(name);
-	buddy->alias    = g_strdup(alias);
+	buddy->name     = purple_utf8_strip_unprintables(name);
+	buddy->alias    = purple_utf8_strip_unprintables(alias);
 	buddy->presence = purple_presence_new_for_buddy(buddy);
 	((PurpleBlistNode *)buddy)->type = PURPLE_BLIST_BUDDY_NODE;
 
@@ -1705,7 +1743,7 @@
 		return group;
 
 	group = g_new0(PurpleGroup, 1);
-	group->name = g_strdup(name);
+	group->name = purple_utf8_strip_unprintables(name);
 	group->totalsize = 0;
 	group->currentsize = 0;
 	group->online = 0;
--- a/libpurple/util.c	Wed Jun 03 21:59:58 2009 +0000
+++ b/libpurple/util.c	Wed Jun 03 22:12:28 2009 +0000
@@ -4429,7 +4429,10 @@
 {
 	gchar *workstr, *iter;
 
-	g_return_val_if_fail(str != NULL, NULL);
+	if (str == NULL)
+		/* Act like g_strdup */
+		return NULL;
+
 	g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
 
 	workstr = iter = g_new(gchar, strlen(str) + 1);