summaryrefslogtreecommitdiff
path: root/toxcore/Messenger.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/Messenger.c')
-rw-r--r--toxcore/Messenger.c31
1 files changed, 17 insertions, 14 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 70623611..33c37b06 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -28,6 +28,9 @@
28static void set_friend_status(Messenger *m, int friendnumber, uint8_t status); 28static void set_friend_status(Messenger *m, int friendnumber, uint8_t status);
29static int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length); 29static int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length);
30 30
31// friend_is_valid determines if the friendnumber passed is valid in the Messenger object
32static uint8_t friend_is_valid(int friendnumber, Messenger *m) { return friendnumber < 0 || (uint32_t)friendnumber >= m->numfriends; }
33
31/* return 1 if we are online. 34/* return 1 if we are online.
32 * return 0 if we are offline. 35 * return 0 if we are offline.
33 * static uint8_t online; 36 * static uint8_t online;
@@ -76,7 +79,7 @@ int getfriend_id(Messenger *m, uint8_t *client_id)
76 */ 79 */
77int getclient_id(Messenger *m, int friend_id, uint8_t *client_id) 80int getclient_id(Messenger *m, int friend_id, uint8_t *client_id)
78{ 81{
79 if (friend_id >= m->numfriends || friend_id < 0) 82 if (friend_is_valid(friend_id,m))
80 return -1; 83 return -1;
81 84
82 if (m->friendlist[friend_id].status > 0) { 85 if (m->friendlist[friend_id].status > 0) {
@@ -246,7 +249,7 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id)
246 */ 249 */
247int m_delfriend(Messenger *m, int friendnumber) 250int m_delfriend(Messenger *m, int friendnumber)
248{ 251{
249 if (friendnumber >= m->numfriends || friendnumber < 0) 252 if (friend_is_valid(friendnumber,m))
250 return -1; 253 return -1;
251 254
252 DHT_delfriend(m->dht, m->friendlist[friendnumber].client_id); 255 DHT_delfriend(m->dht, m->friendlist[friendnumber].client_id);
@@ -276,7 +279,7 @@ int m_delfriend(Messenger *m, int friendnumber)
276 */ 279 */
277int m_friendstatus(Messenger *m, int friendnumber) 280int m_friendstatus(Messenger *m, int friendnumber)
278{ 281{
279 if (friendnumber < 0 || friendnumber >= m->numfriends) 282 if (friend_is_valid(friendnumber,m))
280 return NOFRIEND; 283 return NOFRIEND;
281 284
282 return m->friendlist[friendnumber].status; 285 return m->friendlist[friendnumber].status;
@@ -288,7 +291,7 @@ int m_friendstatus(Messenger *m, int friendnumber)
288 */ 291 */
289uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length) 292uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length)
290{ 293{
291 if (friendnumber < 0 || friendnumber >= m->numfriends) 294 if (friend_is_valid(friendnumber,m))
292 return 0; 295 return 0;
293 296
294 uint32_t msgid = ++m->friendlist[friendnumber].message_id; 297 uint32_t msgid = ++m->friendlist[friendnumber].message_id;
@@ -341,7 +344,7 @@ static int m_sendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t le
341 */ 344 */
342static int setfriendname(Messenger *m, int friendnumber, uint8_t *name) 345static int setfriendname(Messenger *m, int friendnumber, uint8_t *name)
343{ 346{
344 if (friendnumber >= m->numfriends || friendnumber < 0) 347 if (friend_is_valid(friendnumber,m))
345 return -1; 348 return -1;
346 349
347 memcpy(m->friendlist[friendnumber].name, name, MAX_NAME_LENGTH); 350 memcpy(m->friendlist[friendnumber].name, name, MAX_NAME_LENGTH);
@@ -395,7 +398,7 @@ uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen)
395 */ 398 */
396int getname(Messenger *m, int friendnumber, uint8_t *name) 399int getname(Messenger *m, int friendnumber, uint8_t *name)
397{ 400{
398 if (friendnumber >= m->numfriends || friendnumber < 0) 401 if (friend_is_valid(friendnumber,m))
399 return -1; 402 return -1;
400 403
401 memcpy(name, m->friendlist[friendnumber].name, MAX_NAME_LENGTH); 404 memcpy(name, m->friendlist[friendnumber].name, MAX_NAME_LENGTH);
@@ -438,7 +441,7 @@ int m_set_userstatus(Messenger *m, USERSTATUS status)
438 */ 441 */
439int m_get_statusmessage_size(Messenger *m, int friendnumber) 442int m_get_statusmessage_size(Messenger *m, int friendnumber)
440{ 443{
441 if (friendnumber >= m->numfriends || friendnumber < 0) 444 if (friend_is_valid(friendnumber,m))
442 return -1; 445 return -1;
443 446
444 return m->friendlist[friendnumber].statusmessage_length; 447 return m->friendlist[friendnumber].statusmessage_length;
@@ -449,7 +452,7 @@ int m_get_statusmessage_size(Messenger *m, int friendnumber)
449 */ 452 */
450int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen) 453int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen)
451{ 454{
452 if (friendnumber >= m->numfriends || friendnumber < 0) 455 if (friend_is_valid(friendnumber,m))
453 return -1; 456 return -1;
454 457
455 memset(buf, 0, maxlen); 458 memset(buf, 0, maxlen);
@@ -466,7 +469,7 @@ int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen)
466 469
467USERSTATUS m_get_userstatus(Messenger *m, int friendnumber) 470USERSTATUS m_get_userstatus(Messenger *m, int friendnumber)
468{ 471{
469 if (friendnumber >= m->numfriends || friendnumber < 0) 472 if (friend_is_valid(friendnumber,m))
470 return USERSTATUS_INVALID; 473 return USERSTATUS_INVALID;
471 474
472 USERSTATUS status = m->friendlist[friendnumber].userstatus; 475 USERSTATUS status = m->friendlist[friendnumber].userstatus;
@@ -502,7 +505,7 @@ static int send_ping(Messenger *m, int friendnumber)
502 505
503static int set_friend_statusmessage(Messenger *m, int friendnumber, uint8_t *status, uint16_t length) 506static int set_friend_statusmessage(Messenger *m, int friendnumber, uint8_t *status, uint16_t length)
504{ 507{
505 if (friendnumber >= m->numfriends || friendnumber < 0) 508 if (friend_is_valid(friendnumber,m))
506 return -1; 509 return -1;
507 510
508 uint8_t *newstatus = calloc(length, 1); 511 uint8_t *newstatus = calloc(length, 1);
@@ -524,7 +527,7 @@ void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno)
524 if (yesno != 0 || yesno != 1) 527 if (yesno != 0 || yesno != 1)
525 return; 528 return;
526 529
527 if (friendnumber >= m->numfriends || friendnumber < 0) 530 if (friend_is_valid(friendnumber,m))
528 return; 531 return;
529 532
530 m->friendlist[friendnumber].receives_read_receipts = yesno; 533 m->friendlist[friendnumber].receives_read_receipts = yesno;
@@ -606,7 +609,7 @@ void set_friend_status(Messenger *m, int friendnumber, uint8_t status)
606 609
607int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length) 610int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length)
608{ 611{
609 if (friendnumber < 0 || friendnumber >= m->numfriends) 612 if (friend_is_valid(friendnumber,m))
610 return 0; 613 return 0;
611 614
612 if (length >= MAX_DATA_SIZE || m->friendlist[friendnumber].status != FRIEND_ONLINE) 615 if (length >= MAX_DATA_SIZE || m->friendlist[friendnumber].status != FRIEND_ONLINE)
@@ -853,7 +856,7 @@ void doFriends(Messenger *m)
853 case PACKET_ID_RECEIPT: { 856 case PACKET_ID_RECEIPT: {
854 uint32_t msgid; 857 uint32_t msgid;
855 858
856 if (data_length < sizeof(msgid)) 859 if (data_length < 0 || (uint32_t)data_length < sizeof(msgid))
857 break; 860 break;
858 861
859 memcpy(&msgid, data, sizeof(msgid)); 862 memcpy(&msgid, data, sizeof(msgid));
@@ -959,7 +962,7 @@ void Messenger_save(Messenger *m, uint8_t *data)
959/* Load the messenger from data of size length. */ 962/* Load the messenger from data of size length. */
960int Messenger_load(Messenger *m, uint8_t *data, uint32_t length) 963int Messenger_load(Messenger *m, uint8_t *data, uint32_t length)
961{ 964{
962 if (length == ~0) 965 if (length == ~((uint32_t)0))
963 return -1; 966 return -1;
964 967
965 if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3) 968 if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3)