summaryrefslogtreecommitdiff
path: root/toxcore/Messenger.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/Messenger.c')
-rw-r--r--toxcore/Messenger.c35
1 files changed, 19 insertions, 16 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 194a633e..1bcc1f51 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -204,8 +204,7 @@ static int32_t init_new_friend(Messenger *m, const uint8_t *real_pk, uint8_t sta
204 m->friendlist[i].friendcon_id = friendcon_id; 204 m->friendlist[i].friendcon_id = friendcon_id;
205 m->friendlist[i].friendrequest_lastsent = 0; 205 m->friendlist[i].friendrequest_lastsent = 0;
206 id_copy(m->friendlist[i].real_pk, real_pk); 206 id_copy(m->friendlist[i].real_pk, real_pk);
207 m->friendlist[i].statusmessage = calloc(1, 1); 207 m->friendlist[i].statusmessage_length = 0;
208 m->friendlist[i].statusmessage_length = 1;
209 m->friendlist[i].userstatus = USERSTATUS_NONE; 208 m->friendlist[i].userstatus = USERSTATUS_NONE;
210 m->friendlist[i].avatar_info_sent = 0; 209 m->friendlist[i].avatar_info_sent = 0;
211 m->friendlist[i].avatar_recv_data = NULL; 210 m->friendlist[i].avatar_recv_data = NULL;
@@ -334,7 +333,6 @@ int m_delfriend(Messenger *m, int32_t friendnumber)
334 if (m->friendlist[friendnumber].status == FRIEND_ONLINE) 333 if (m->friendlist[friendnumber].status == FRIEND_ONLINE)
335 remove_online_friend(m, friendnumber); 334 remove_online_friend(m, friendnumber);
336 335
337 free(m->friendlist[friendnumber].statusmessage);
338 free(m->friendlist[friendnumber].avatar_recv_data); 336 free(m->friendlist[friendnumber].avatar_recv_data);
339 remove_request_received(&(m->fr), m->friendlist[friendnumber].real_pk); 337 remove_request_received(&(m->fr), m->friendlist[friendnumber].real_pk);
340 friend_connection_callbacks(m->fr_c, m->friendlist[friendnumber].friendcon_id, MESSENGER_CALLBACK_INDEX, 0, 0, 0, 0, 0); 338 friend_connection_callbacks(m->fr_c, m->friendlist[friendnumber].friendcon_id, MESSENGER_CALLBACK_INDEX, 0, 0, 0, 0, 0);
@@ -451,7 +449,7 @@ uint32_t m_sendaction_withid(const Messenger *m, int32_t friendnumber, uint32_t
451 */ 449 */
452static int m_sendname(const Messenger *m, int32_t friendnumber, const uint8_t *name, uint16_t length) 450static int m_sendname(const Messenger *m, int32_t friendnumber, const uint8_t *name, uint16_t length)
453{ 451{
454 if (length > MAX_NAME_LENGTH || length == 0) 452 if (length > MAX_NAME_LENGTH)
455 return 0; 453 return 0;
456 454
457 return write_cryptpacket_id(m, friendnumber, PACKET_ID_NICKNAME, name, length, 0); 455 return write_cryptpacket_id(m, friendnumber, PACKET_ID_NICKNAME, name, length, 0);
@@ -485,13 +483,15 @@ int setfriendname(Messenger *m, int32_t friendnumber, const uint8_t *name, uint1
485 */ 483 */
486int setname(Messenger *m, const uint8_t *name, uint16_t length) 484int setname(Messenger *m, const uint8_t *name, uint16_t length)
487{ 485{
488 if (length > MAX_NAME_LENGTH || length == 0) 486 if (length > MAX_NAME_LENGTH)
489 return -1; 487 return -1;
490 488
491 if (m->name_length == length && memcmp(name, m->name, length) == 0) 489 if (m->name_length == length && (length == 0 || memcmp(name, m->name, length) == 0))
492 return 0; 490 return 0;
493 491
494 memcpy(m->name, name, length); 492 if (length)
493 memcpy(m->name, name, length);
494
495 m->name_length = length; 495 m->name_length = length;
496 uint32_t i; 496 uint32_t i;
497 497
@@ -550,10 +550,12 @@ int m_set_statusmessage(Messenger *m, const uint8_t *status, uint16_t length)
550 if (length > MAX_STATUSMESSAGE_LENGTH) 550 if (length > MAX_STATUSMESSAGE_LENGTH)
551 return -1; 551 return -1;
552 552
553 if (m->statusmessage_length == length && memcmp(m->statusmessage, status, length) == 0) 553 if (m->statusmessage_length == length && (length == 0 || memcmp(m->statusmessage, status, length) == 0))
554 return 0; 554 return 0;
555 555
556 memcpy(m->statusmessage, status, length); 556 if (length)
557 memcpy(m->statusmessage, status, length);
558
557 m->statusmessage_length = length; 559 m->statusmessage_length = length;
558 560
559 uint32_t i; 561 uint32_t i;
@@ -859,10 +861,12 @@ static int set_friend_statusmessage(const Messenger *m, int32_t friendnumber, co
859 if (friend_not_valid(m, friendnumber)) 861 if (friend_not_valid(m, friendnumber))
860 return -1; 862 return -1;
861 863
862 uint8_t *newstatus = calloc(length + 1, 1); 864 if (length > MAX_STATUSMESSAGE_LENGTH)
863 memcpy(newstatus, status, length); 865 return -1;
864 free(m->friendlist[friendnumber].statusmessage); 866
865 m->friendlist[friendnumber].statusmessage = newstatus; 867 if (length)
868 memcpy(m->friendlist[friendnumber].statusmessage, status, length);
869
866 m->friendlist[friendnumber].statusmessage_length = length; 870 m->friendlist[friendnumber].statusmessage_length = length;
867 return 0; 871 return 0;
868} 872}
@@ -1632,7 +1636,6 @@ void kill_messenger(Messenger *m)
1632 kill_networking(m->net); 1636 kill_networking(m->net);
1633 1637
1634 for (i = 0; i < m->numfriends; ++i) { 1638 for (i = 0; i < m->numfriends; ++i) {
1635 free(m->friendlist[i].statusmessage);
1636 free(m->friendlist[i].avatar_recv_data); 1639 free(m->friendlist[i].avatar_recv_data);
1637 } 1640 }
1638 1641
@@ -1990,7 +1993,7 @@ static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len)
1990 } 1993 }
1991 1994
1992 case PACKET_ID_NICKNAME: { 1995 case PACKET_ID_NICKNAME: {
1993 if (data_length > MAX_NAME_LENGTH || data_length == 0) 1996 if (data_length > MAX_NAME_LENGTH)
1994 break; 1997 break;
1995 1998
1996 /* Make sure the NULL terminator is present. */ 1999 /* Make sure the NULL terminator is present. */
@@ -2009,7 +2012,7 @@ static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len)
2009 } 2012 }
2010 2013
2011 case PACKET_ID_STATUSMESSAGE: { 2014 case PACKET_ID_STATUSMESSAGE: {
2012 if (data_length == 0 || data_length > MAX_STATUSMESSAGE_LENGTH) 2015 if (data_length > MAX_STATUSMESSAGE_LENGTH)
2013 break; 2016 break;
2014 2017
2015 /* Make sure the NULL terminator is present. */ 2018 /* Make sure the NULL terminator is present. */