summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-09-02 09:55:37 -0400
committerirungentoo <irungentoo@gmail.com>2013-09-02 09:55:37 -0400
commit52336565612207fdb2d2068989da936b5b83ceeb (patch)
treec1c3c9ceb9c09d6d4a42d13aba294e53453d5b30 /toxcore
parent36a3b02f63aedeeb09105832ba75252bc06b8598 (diff)
Properly fixed signed/unsigned comparisons.
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/DHT.c17
-rw-r--r--toxcore/Lossless_UDP.c14
-rw-r--r--toxcore/Messenger.c35
-rw-r--r--toxcore/net_crypto.c16
4 files changed, 44 insertions, 38 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 9d5be369..4ed437bb 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -544,9 +544,9 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl
544 encrypt ); 544 encrypt );
545 545
546 if (len == -1) 546 if (len == -1)
547 return -1; 547 return -1;
548 548
549 if ((uint32_t)len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING) 549 if ((unsigned int)len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING)
550 return -1; 550 return -1;
551 551
552 data[0] = NET_PACKET_SEND_NODES; 552 data[0] = NET_PACKET_SEND_NODES;
@@ -612,10 +612,7 @@ static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint3
612 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, 612 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
613 sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING, plain ); 613 sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING, plain );
614 614
615 if (len == -1) 615 if ((unsigned int)len != sizeof(ping_id) + num_nodes * sizeof(Node_format))
616 return -1;
617
618 if ((uint32_t)len != sizeof(ping_id) + num_nodes * sizeof(Node_format))
619 return 1; 616 return 1;
620 617
621 memcpy(&ping_id, plain, sizeof(ping_id)); 618 memcpy(&ping_id, plain, sizeof(ping_id));
@@ -883,8 +880,9 @@ int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t lengt
883 880
884 /* If ip is not zero and node is good */ 881 /* If ip is not zero and node is good */
885 if (client->ret_ip_port.ip.uint32 != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) { 882 if (client->ret_ip_port.ip.uint32 != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) {
886 int retval = sendpacket(dht->c->lossless_udp->net->sock, client->ip_port, packet, length); 883 int retval = sendpacket(dht->c->lossless_udp->net->sock, client->ip_port, packet, length);
887 if (retval != -1 && (uint32_t)retval == length) 884
885 if ((unsigned int)retval == length)
888 ++sent; 886 ++sent;
889 } 887 }
890 } 888 }
@@ -924,7 +922,8 @@ static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint
924 return 0; 922 return 0;
925 923
926 int retval = sendpacket(dht->c->lossless_udp->net->sock, ip_list[rand() % n], packet, length); 924 int retval = sendpacket(dht->c->lossless_udp->net->sock, ip_list[rand() % n], packet, length);
927 if (retval != -1 && (uint32_t)retval == length) 925
926 if ((unsigned int)retval == length)
928 return 1; 927 return 1;
929 928
930 return 0; 929 return 0;
diff --git a/toxcore/Lossless_UDP.c b/toxcore/Lossless_UDP.c
index f7b9d4b6..28a50ed9 100644
--- a/toxcore/Lossless_UDP.c
+++ b/toxcore/Lossless_UDP.c
@@ -226,7 +226,7 @@ static void free_connections(Lossless_UDP *ludp)
226 */ 226 */
227int kill_connection(Lossless_UDP *ludp, int connection_id) 227int kill_connection(Lossless_UDP *ludp, int connection_id)
228{ 228{
229 if (connection_id >= 0 && (uint32_t)connection_id < ludp->connections.len) { 229 if ((unsigned int)connection_id < ludp->connections.len) {
230 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); 230 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
231 231
232 if (connection->status > 0) { 232 if (connection->status > 0) {
@@ -248,7 +248,7 @@ int kill_connection(Lossless_UDP *ludp, int connection_id)
248 */ 248 */
249int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds) 249int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds)
250{ 250{
251 if (connection_id >= 0 && (uint32_t)connection_id < ludp->connections.len) { 251 if ((unsigned int)connection_id < ludp->connections.len) {
252 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); 252 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
253 253
254 if (connection->status > 0) { 254 if (connection->status > 0) {
@@ -270,7 +270,7 @@ int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds)
270 */ 270 */
271int is_connected(Lossless_UDP *ludp, int connection_id) 271int is_connected(Lossless_UDP *ludp, int connection_id)
272{ 272{
273 if (connection_id >= 0 && (uint32_t)connection_id < ludp->connections.len) 273 if ((unsigned int)connection_id < ludp->connections.len)
274 return tox_array_get(&ludp->connections, connection_id, Connection).status; 274 return tox_array_get(&ludp->connections, connection_id, Connection).status;
275 275
276 return 0; 276 return 0;
@@ -279,7 +279,7 @@ int is_connected(Lossless_UDP *ludp, int connection_id)
279/* return the ip_port of the corresponding connection. */ 279/* return the ip_port of the corresponding connection. */
280IP_Port connection_ip(Lossless_UDP *ludp, int connection_id) 280IP_Port connection_ip(Lossless_UDP *ludp, int connection_id)
281{ 281{
282 if (connection_id >= 0 && (uint32_t)connection_id < ludp->connections.len) 282 if ((unsigned int)connection_id < ludp->connections.len)
283 return tox_array_get(&ludp->connections, connection_id, Connection).ip_port; 283 return tox_array_get(&ludp->connections, connection_id, Connection).ip_port;
284 284
285 IP_Port zero = {{{{0}}, 0, 0}}; 285 IP_Port zero = {{{{0}}, 0, 0}};
@@ -289,7 +289,7 @@ IP_Port connection_ip(Lossless_UDP *ludp, int connection_id)
289/* returns the number of packets in the queue waiting to be successfully sent. */ 289/* returns the number of packets in the queue waiting to be successfully sent. */
290uint32_t sendqueue(Lossless_UDP *ludp, int connection_id) 290uint32_t sendqueue(Lossless_UDP *ludp, int connection_id)
291{ 291{
292 if (connection_id < 0 || (uint32_t)connection_id >= ludp->connections.len) 292 if ((unsigned int)connection_id >= ludp->connections.len)
293 return 0; 293 return 0;
294 294
295 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); 295 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
@@ -299,7 +299,7 @@ uint32_t sendqueue(Lossless_UDP *ludp, int connection_id)
299/* returns the number of packets in the queue waiting to be successfully read with read_packet(...). */ 299/* returns the number of packets in the queue waiting to be successfully read with read_packet(...). */
300uint32_t recvqueue(Lossless_UDP *ludp, int connection_id) 300uint32_t recvqueue(Lossless_UDP *ludp, int connection_id)
301{ 301{
302 if (connection_id < 0 || (uint32_t)connection_id >= ludp->connections.len) 302 if ((unsigned int)connection_id >= ludp->connections.len)
303 return 0; 303 return 0;
304 304
305 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); 305 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
@@ -311,7 +311,7 @@ uint32_t recvqueue(Lossless_UDP *ludp, int connection_id)
311 */ 311 */
312char id_packet(Lossless_UDP *ludp, int connection_id) 312char id_packet(Lossless_UDP *ludp, int connection_id)
313{ 313{
314 if (connection_id < 0 || (uint32_t)connection_id >= ludp->connections.len || recvqueue(ludp, connection_id) == 0) 314 if (recvqueue(ludp, connection_id) == 0)
315 return -1; 315 return -1;
316 316
317 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); 317 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 33c37b06..2bbeed41 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -28,8 +28,11 @@
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 31// friend_not_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; } 32static uint8_t friend_not_valid(Messenger *m, int friendnumber)
33{
34 return (unsigned int)friendnumber >= m->numfriends;
35}
33 36
34/* return 1 if we are online. 37/* return 1 if we are online.
35 * return 0 if we are offline. 38 * return 0 if we are offline.
@@ -79,7 +82,7 @@ int getfriend_id(Messenger *m, uint8_t *client_id)
79 */ 82 */
80int getclient_id(Messenger *m, int friend_id, uint8_t *client_id) 83int getclient_id(Messenger *m, int friend_id, uint8_t *client_id)
81{ 84{
82 if (friend_is_valid(friend_id,m)) 85 if (friend_not_valid(m, friend_id))
83 return -1; 86 return -1;
84 87
85 if (m->friendlist[friend_id].status > 0) { 88 if (m->friendlist[friend_id].status > 0) {
@@ -249,7 +252,7 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id)
249 */ 252 */
250int m_delfriend(Messenger *m, int friendnumber) 253int m_delfriend(Messenger *m, int friendnumber)
251{ 254{
252 if (friend_is_valid(friendnumber,m)) 255 if (friend_not_valid(m, friendnumber))
253 return -1; 256 return -1;
254 257
255 DHT_delfriend(m->dht, m->friendlist[friendnumber].client_id); 258 DHT_delfriend(m->dht, m->friendlist[friendnumber].client_id);
@@ -279,7 +282,7 @@ int m_delfriend(Messenger *m, int friendnumber)
279 */ 282 */
280int m_friendstatus(Messenger *m, int friendnumber) 283int m_friendstatus(Messenger *m, int friendnumber)
281{ 284{
282 if (friend_is_valid(friendnumber,m)) 285 if (friend_not_valid(m, friendnumber))
283 return NOFRIEND; 286 return NOFRIEND;
284 287
285 return m->friendlist[friendnumber].status; 288 return m->friendlist[friendnumber].status;
@@ -291,7 +294,7 @@ int m_friendstatus(Messenger *m, int friendnumber)
291 */ 294 */
292uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length) 295uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length)
293{ 296{
294 if (friend_is_valid(friendnumber,m)) 297 if (friend_not_valid(m, friendnumber))
295 return 0; 298 return 0;
296 299
297 uint32_t msgid = ++m->friendlist[friendnumber].message_id; 300 uint32_t msgid = ++m->friendlist[friendnumber].message_id;
@@ -344,7 +347,7 @@ static int m_sendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t le
344 */ 347 */
345static int setfriendname(Messenger *m, int friendnumber, uint8_t *name) 348static int setfriendname(Messenger *m, int friendnumber, uint8_t *name)
346{ 349{
347 if (friend_is_valid(friendnumber,m)) 350 if (friend_not_valid(m, friendnumber))
348 return -1; 351 return -1;
349 352
350 memcpy(m->friendlist[friendnumber].name, name, MAX_NAME_LENGTH); 353 memcpy(m->friendlist[friendnumber].name, name, MAX_NAME_LENGTH);
@@ -398,7 +401,7 @@ uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen)
398 */ 401 */
399int getname(Messenger *m, int friendnumber, uint8_t *name) 402int getname(Messenger *m, int friendnumber, uint8_t *name)
400{ 403{
401 if (friend_is_valid(friendnumber,m)) 404 if (friend_not_valid(m, friendnumber))
402 return -1; 405 return -1;
403 406
404 memcpy(name, m->friendlist[friendnumber].name, MAX_NAME_LENGTH); 407 memcpy(name, m->friendlist[friendnumber].name, MAX_NAME_LENGTH);
@@ -441,7 +444,7 @@ int m_set_userstatus(Messenger *m, USERSTATUS status)
441 */ 444 */
442int m_get_statusmessage_size(Messenger *m, int friendnumber) 445int m_get_statusmessage_size(Messenger *m, int friendnumber)
443{ 446{
444 if (friend_is_valid(friendnumber,m)) 447 if (friend_not_valid(m, friendnumber))
445 return -1; 448 return -1;
446 449
447 return m->friendlist[friendnumber].statusmessage_length; 450 return m->friendlist[friendnumber].statusmessage_length;
@@ -452,7 +455,7 @@ int m_get_statusmessage_size(Messenger *m, int friendnumber)
452 */ 455 */
453int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen) 456int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen)
454{ 457{
455 if (friend_is_valid(friendnumber,m)) 458 if (friend_not_valid(m, friendnumber))
456 return -1; 459 return -1;
457 460
458 memset(buf, 0, maxlen); 461 memset(buf, 0, maxlen);
@@ -469,7 +472,7 @@ int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen)
469 472
470USERSTATUS m_get_userstatus(Messenger *m, int friendnumber) 473USERSTATUS m_get_userstatus(Messenger *m, int friendnumber)
471{ 474{
472 if (friend_is_valid(friendnumber,m)) 475 if (friend_not_valid(m, friendnumber))
473 return USERSTATUS_INVALID; 476 return USERSTATUS_INVALID;
474 477
475 USERSTATUS status = m->friendlist[friendnumber].userstatus; 478 USERSTATUS status = m->friendlist[friendnumber].userstatus;
@@ -505,7 +508,7 @@ static int send_ping(Messenger *m, int friendnumber)
505 508
506static int set_friend_statusmessage(Messenger *m, int friendnumber, uint8_t *status, uint16_t length) 509static int set_friend_statusmessage(Messenger *m, int friendnumber, uint8_t *status, uint16_t length)
507{ 510{
508 if (friend_is_valid(friendnumber,m)) 511 if (friend_not_valid(m, friendnumber))
509 return -1; 512 return -1;
510 513
511 uint8_t *newstatus = calloc(length, 1); 514 uint8_t *newstatus = calloc(length, 1);
@@ -527,7 +530,7 @@ void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno)
527 if (yesno != 0 || yesno != 1) 530 if (yesno != 0 || yesno != 1)
528 return; 531 return;
529 532
530 if (friend_is_valid(friendnumber,m)) 533 if (friend_not_valid(m, friendnumber))
531 return; 534 return;
532 535
533 m->friendlist[friendnumber].receives_read_receipts = yesno; 536 m->friendlist[friendnumber].receives_read_receipts = yesno;
@@ -609,7 +612,7 @@ void set_friend_status(Messenger *m, int friendnumber, uint8_t status)
609 612
610int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length) 613int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length)
611{ 614{
612 if (friend_is_valid(friendnumber,m)) 615 if (friend_not_valid(m, friendnumber))
613 return 0; 616 return 0;
614 617
615 if (length >= MAX_DATA_SIZE || m->friendlist[friendnumber].status != FRIEND_ONLINE) 618 if (length >= MAX_DATA_SIZE || m->friendlist[friendnumber].status != FRIEND_ONLINE)
@@ -780,7 +783,7 @@ void doFriends(Messenger *m)
780 len = read_cryptpacket(m->net_crypto, m->friendlist[i].crypt_connection_id, temp); 783 len = read_cryptpacket(m->net_crypto, m->friendlist[i].crypt_connection_id, temp);
781 uint8_t packet_id = temp[0]; 784 uint8_t packet_id = temp[0];
782 uint8_t *data = temp + 1; 785 uint8_t *data = temp + 1;
783 int data_length = len - 1; 786 uint32_t data_length = len - 1;
784 787
785 if (len > 0) { 788 if (len > 0) {
786 switch (packet_id) { 789 switch (packet_id) {
@@ -856,7 +859,7 @@ void doFriends(Messenger *m)
856 case PACKET_ID_RECEIPT: { 859 case PACKET_ID_RECEIPT: {
857 uint32_t msgid; 860 uint32_t msgid;
858 861
859 if (data_length < 0 || (uint32_t)data_length < sizeof(msgid)) 862 if (data_length < sizeof(msgid))
860 break; 863 break;
861 864
862 memcpy(&msgid, data, sizeof(msgid)); 865 memcpy(&msgid, data, sizeof(msgid));
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index f55f40a1..b168fc76 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -32,7 +32,10 @@
32#define CONN_ESTABLISHED 3 32#define CONN_ESTABLISHED 3
33#define CONN_TIMED_OUT 4 33#define CONN_TIMED_OUT 4
34 34
35static uint8_t crypt_id_valid(int crypt_connection_id, Net_Crypto *c) { return crypt_connection_id < 0 || (uint32_t)crypt_connection_id >= c->crypto_connections_length; } 35static uint8_t crypt_connection_id_not_valid(Net_Crypto *c, int crypt_connection_id)
36{
37 return (uint32_t)crypt_connection_id >= c->crypto_connections_length;
38}
36 39
37/* Use this instead of memcmp; not vulnerable to timing attacks. */ 40/* Use this instead of memcmp; not vulnerable to timing attacks. */
38uint8_t crypto_iszero(uint8_t *mem, uint32_t length) 41uint8_t crypto_iszero(uint8_t *mem, uint32_t length)
@@ -152,7 +155,7 @@ void random_nonce(uint8_t *nonce)
152 */ 155 */
153int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data) 156int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data)
154{ 157{
155 if (crypt_id_valid(crypt_connection_id,c)) 158 if (crypt_connection_id_not_valid(c, crypt_connection_id))
156 return 0; 159 return 0;
157 160
158 if (c->crypto_connections[crypt_connection_id].status != CONN_ESTABLISHED) 161 if (c->crypto_connections[crypt_connection_id].status != CONN_ESTABLISHED)
@@ -184,7 +187,7 @@ int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data)
184 */ 187 */
185int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length) 188int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length)
186{ 189{
187 if (crypt_id_valid(crypt_connection_id,c)) 190 if (crypt_connection_id_not_valid(c, crypt_connection_id))
188 return 0; 191 return 0;
189 192
190 if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1) 193 if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1)
@@ -307,7 +310,8 @@ static int cryptopacket_handle(void *object, IP_Port source, uint8_t *packet, ui
307 310
308 } else { /* If request is not for us, try routing it. */ 311 } else { /* If request is not for us, try routing it. */
309 int retval = route_packet(dht, packet + 1, packet, length); 312 int retval = route_packet(dht, packet + 1, packet, length);
310 if (retval < 0 || (uint32_t)retval == length) 313
314 if ((unsigned int)retval == length)
311 return 0; 315 return 0;
312 } 316 }
313 } 317 }
@@ -508,7 +512,7 @@ int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, ui
508 */ 512 */
509int crypto_kill(Net_Crypto *c, int crypt_connection_id) 513int crypto_kill(Net_Crypto *c, int crypt_connection_id)
510{ 514{
511 if (crypt_id_valid(crypt_connection_id,c)) 515 if (crypt_connection_id_not_valid(c, crypt_connection_id))
512 return 1; 516 return 1;
513 517
514 if (c->crypto_connections[crypt_connection_id].status != CONN_NO_CONNECTION) { 518 if (c->crypto_connections[crypt_connection_id].status != CONN_NO_CONNECTION) {
@@ -599,7 +603,7 @@ int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key,
599 */ 603 */
600int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id) 604int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id)
601{ 605{
602 if (crypt_connection_id >= 0 && (uint32_t)crypt_connection_id < c->crypto_connections_length) 606 if ((unsigned int)crypt_connection_id < c->crypto_connections_length)
603 return c->crypto_connections[crypt_connection_id].status; 607 return c->crypto_connections[crypt_connection_id].status;
604 608
605 return CONN_NO_CONNECTION; 609 return CONN_NO_CONNECTION;