Added some safeguards against crashes.

This commit is contained in:
Håvard Pettersson 2014-10-18 12:02:14 +02:00
parent e6aea86754
commit 17c166cc41

View File

@ -332,7 +332,7 @@ twc_group_namelist_change_callback(Tox *tox,
group_number, group_number,
false); false);
struct t_gui_nick *nick; struct t_gui_nick *nick = NULL;
char *name = twc_get_peer_name_nt(profile->tox, group_number, peer_number); char *name = twc_get_peer_name_nt(profile->tox, group_number, peer_number);
char *prev_name = NULL; char *prev_name = NULL;
@ -340,31 +340,38 @@ twc_group_namelist_change_callback(Tox *tox,
|| change_type == TOX_CHAT_CHANGE_PEER_NAME) || change_type == TOX_CHAT_CHANGE_PEER_NAME)
{ {
nick = weechat_hashtable_get(chat->nicks, &peer_number); nick = weechat_hashtable_get(chat->nicks, &peer_number);
if (nick)
{
prev_name = strdup(weechat_nicklist_nick_get_string(chat->buffer, prev_name = strdup(weechat_nicklist_nick_get_string(chat->buffer,
nick, "name")); nick, "name"));
weechat_nicklist_remove_nick(chat->buffer, nick); weechat_nicklist_remove_nick(chat->buffer, nick);
weechat_hashtable_remove(chat->nicks, &peer_number); weechat_hashtable_remove(chat->nicks, &peer_number);
} }
}
if (change_type == TOX_CHAT_CHANGE_PEER_ADD if (change_type == TOX_CHAT_CHANGE_PEER_ADD
|| change_type == TOX_CHAT_CHANGE_PEER_NAME) || change_type == TOX_CHAT_CHANGE_PEER_NAME)
{ {
nick = weechat_nicklist_add_nick(chat->buffer, chat->nicklist_group, nick = weechat_nicklist_add_nick(chat->buffer, chat->nicklist_group,
name, NULL, NULL, NULL, 1); name, NULL, NULL, NULL, 1);
if (nick)
weechat_hashtable_set(chat->nicks, &peer_number, nick); weechat_hashtable_set(chat->nicks, &peer_number, nick);
} }
switch (change_type) switch (change_type)
{ {
case TOX_CHAT_CHANGE_PEER_NAME: case TOX_CHAT_CHANGE_PEER_NAME:
if (prev_name && name)
weechat_printf(chat->buffer, "%s%s is now known as %s", weechat_printf(chat->buffer, "%s%s is now known as %s",
weechat_prefix("network"), prev_name, name); weechat_prefix("network"), prev_name, name);
break; break;
case TOX_CHAT_CHANGE_PEER_ADD: case TOX_CHAT_CHANGE_PEER_ADD:
if (name)
weechat_printf(chat->buffer, "%s%s just joined the group chat", weechat_printf(chat->buffer, "%s%s just joined the group chat",
weechat_prefix("join"), name); weechat_prefix("join"), name);
break; break;
case TOX_CHAT_CHANGE_PEER_DEL: case TOX_CHAT_CHANGE_PEER_DEL:
if (prev_name)
weechat_printf(chat->buffer, "%s%s just left the group chat", weechat_printf(chat->buffer, "%s%s just left the group chat",
weechat_prefix("quit"), prev_name); weechat_prefix("quit"), prev_name);
break; break;