pidgin: 5cba5165: Strip unprintable UTF-8 characters from ...

darkrain42 at pidgin.im darkrain42 at pidgin.im
Wed Jun 3 18:15:41 EDT 2009


-----------------------------------------------------------------
Revision: 5cba5165f95e4a83afda18c95e09abe279e6da99
Ancestor: ac9f2ae79aa8a0ef65201b4cbedf9ca0a7e3a9e6
Author: darkrain42 at pidgin.im
Date: 2009-06-03T22:12:28
Branch: im.pidgin.pidgin
URL: http://d.pidgin.im/viewmtn/revision/info/5cba5165f95e4a83afda18c95e09abe279e6da99

Modified files:
        libpurple/blist.c libpurple/util.c

ChangeLog: 

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.

-------------- next part --------------
============================================================
--- libpurple/blist.c	43e0b66e0f974311c31a3fbd2b361dbf123c8948
+++ libpurple/blist.c	880a4f22c97e117941e6d36767fd82cf322c0c91
@@ -977,18 +977,26 @@ void purple_blist_alias_contact(PurpleCo
 	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 @@ void purple_blist_alias_chat(PurpleChat 
 {
 	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 @@ void purple_blist_alias_buddy(PurpleBudd
 	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 @@ void purple_blist_server_alias_buddy(Pur
 	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 @@ void purple_blist_server_alias_buddy(Pur
 /*
  * 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);
+	g_return_if_fail(name != NULL);
 
-	if (*new_name == '\0' || purple_strequal(new_name, source->name))
+	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 @@ void purple_blist_rename_group(PurpleGro
 		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 @@ void purple_blist_rename_group(PurpleGro
 		}
 
 		old_name = source->name;
-		source->name = g_strdup(new_name);
+		source->name = new_name;
 	}
 
 	/* Save our changes */
@@ -1184,7 +1222,7 @@ void purple_blist_rename_group(PurpleGro
 
 	/* 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 @@ PurpleChat *purple_chat_new(PurpleAccoun
 	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 @@ PurpleBuddy *purple_buddy_new(PurpleAcco
 	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 @@ PurpleGroup *purple_group_new(const char
 		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;
============================================================
--- libpurple/util.c	6f0104b30052597717ce66df8bdd300bcbca215a
+++ libpurple/util.c	7ba662405fad431fbf16bff7628a6d9635cb64d7
@@ -4429,7 +4429,10 @@ purple_utf8_strip_unprintables(const gch
 {
 	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);


More information about the Commits mailing list