Properly fixed signed/unsigned comparisons.

This commit is contained in:
irungentoo 2013-09-02 09:55:37 -04:00
parent 36a3b02f63
commit 5233656561
4 changed files with 44 additions and 38 deletions

View File

@ -544,9 +544,9 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl
encrypt );
if (len == -1)
return -1;
return -1;
if ((uint32_t)len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING)
if ((unsigned int)len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING)
return -1;
data[0] = NET_PACKET_SEND_NODES;
@ -612,10 +612,7 @@ static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint3
packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING, plain );
if (len == -1)
return -1;
if ((uint32_t)len != sizeof(ping_id) + num_nodes * sizeof(Node_format))
if ((unsigned int)len != sizeof(ping_id) + num_nodes * sizeof(Node_format))
return 1;
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
/* If ip is not zero and node is good */
if (client->ret_ip_port.ip.uint32 != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) {
int retval = sendpacket(dht->c->lossless_udp->net->sock, client->ip_port, packet, length);
if (retval != -1 && (uint32_t)retval == length)
int retval = sendpacket(dht->c->lossless_udp->net->sock, client->ip_port, packet, length);
if ((unsigned int)retval == length)
++sent;
}
}
@ -924,7 +922,8 @@ static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint
return 0;
int retval = sendpacket(dht->c->lossless_udp->net->sock, ip_list[rand() % n], packet, length);
if (retval != -1 && (uint32_t)retval == length)
if ((unsigned int)retval == length)
return 1;
return 0;

View File

@ -226,7 +226,7 @@ static void free_connections(Lossless_UDP *ludp)
*/
int kill_connection(Lossless_UDP *ludp, int connection_id)
{
if (connection_id >= 0 && (uint32_t)connection_id < ludp->connections.len) {
if ((unsigned int)connection_id < ludp->connections.len) {
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
if (connection->status > 0) {
@ -248,7 +248,7 @@ int kill_connection(Lossless_UDP *ludp, int connection_id)
*/
int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds)
{
if (connection_id >= 0 && (uint32_t)connection_id < ludp->connections.len) {
if ((unsigned int)connection_id < ludp->connections.len) {
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
if (connection->status > 0) {
@ -270,7 +270,7 @@ int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds)
*/
int is_connected(Lossless_UDP *ludp, int connection_id)
{
if (connection_id >= 0 && (uint32_t)connection_id < ludp->connections.len)
if ((unsigned int)connection_id < ludp->connections.len)
return tox_array_get(&ludp->connections, connection_id, Connection).status;
return 0;
@ -279,7 +279,7 @@ int is_connected(Lossless_UDP *ludp, int connection_id)
/* return the ip_port of the corresponding connection. */
IP_Port connection_ip(Lossless_UDP *ludp, int connection_id)
{
if (connection_id >= 0 && (uint32_t)connection_id < ludp->connections.len)
if ((unsigned int)connection_id < ludp->connections.len)
return tox_array_get(&ludp->connections, connection_id, Connection).ip_port;
IP_Port zero = {{{{0}}, 0, 0}};
@ -289,7 +289,7 @@ IP_Port connection_ip(Lossless_UDP *ludp, int connection_id)
/* returns the number of packets in the queue waiting to be successfully sent. */
uint32_t sendqueue(Lossless_UDP *ludp, int connection_id)
{
if (connection_id < 0 || (uint32_t)connection_id >= ludp->connections.len)
if ((unsigned int)connection_id >= ludp->connections.len)
return 0;
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
@ -299,7 +299,7 @@ uint32_t sendqueue(Lossless_UDP *ludp, int connection_id)
/* returns the number of packets in the queue waiting to be successfully read with read_packet(...). */
uint32_t recvqueue(Lossless_UDP *ludp, int connection_id)
{
if (connection_id < 0 || (uint32_t)connection_id >= ludp->connections.len)
if ((unsigned int)connection_id >= ludp->connections.len)
return 0;
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
@ -311,7 +311,7 @@ uint32_t recvqueue(Lossless_UDP *ludp, int connection_id)
*/
char id_packet(Lossless_UDP *ludp, int connection_id)
{
if (connection_id < 0 || (uint32_t)connection_id >= ludp->connections.len || recvqueue(ludp, connection_id) == 0)
if (recvqueue(ludp, connection_id) == 0)
return -1;
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);

View File

@ -28,8 +28,11 @@
static void set_friend_status(Messenger *m, int friendnumber, uint8_t status);
static int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length);
// friend_is_valid determines if the friendnumber passed is valid in the Messenger object
static uint8_t friend_is_valid(int friendnumber, Messenger *m) { return friendnumber < 0 || (uint32_t)friendnumber >= m->numfriends; }
// friend_not_valid determines if the friendnumber passed is valid in the Messenger object
static uint8_t friend_not_valid(Messenger *m, int friendnumber)
{
return (unsigned int)friendnumber >= m->numfriends;
}
/* return 1 if we are online.
* return 0 if we are offline.
@ -79,7 +82,7 @@ int getfriend_id(Messenger *m, uint8_t *client_id)
*/
int getclient_id(Messenger *m, int friend_id, uint8_t *client_id)
{
if (friend_is_valid(friend_id,m))
if (friend_not_valid(m, friend_id))
return -1;
if (m->friendlist[friend_id].status > 0) {
@ -249,7 +252,7 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id)
*/
int m_delfriend(Messenger *m, int friendnumber)
{
if (friend_is_valid(friendnumber,m))
if (friend_not_valid(m, friendnumber))
return -1;
DHT_delfriend(m->dht, m->friendlist[friendnumber].client_id);
@ -279,7 +282,7 @@ int m_delfriend(Messenger *m, int friendnumber)
*/
int m_friendstatus(Messenger *m, int friendnumber)
{
if (friend_is_valid(friendnumber,m))
if (friend_not_valid(m, friendnumber))
return NOFRIEND;
return m->friendlist[friendnumber].status;
@ -291,7 +294,7 @@ int m_friendstatus(Messenger *m, int friendnumber)
*/
uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length)
{
if (friend_is_valid(friendnumber,m))
if (friend_not_valid(m, friendnumber))
return 0;
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
*/
static int setfriendname(Messenger *m, int friendnumber, uint8_t *name)
{
if (friend_is_valid(friendnumber,m))
if (friend_not_valid(m, friendnumber))
return -1;
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)
*/
int getname(Messenger *m, int friendnumber, uint8_t *name)
{
if (friend_is_valid(friendnumber,m))
if (friend_not_valid(m, friendnumber))
return -1;
memcpy(name, m->friendlist[friendnumber].name, MAX_NAME_LENGTH);
@ -441,7 +444,7 @@ int m_set_userstatus(Messenger *m, USERSTATUS status)
*/
int m_get_statusmessage_size(Messenger *m, int friendnumber)
{
if (friend_is_valid(friendnumber,m))
if (friend_not_valid(m, friendnumber))
return -1;
return m->friendlist[friendnumber].statusmessage_length;
@ -452,7 +455,7 @@ int m_get_statusmessage_size(Messenger *m, int friendnumber)
*/
int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen)
{
if (friend_is_valid(friendnumber,m))
if (friend_not_valid(m, friendnumber))
return -1;
memset(buf, 0, maxlen);
@ -469,7 +472,7 @@ int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen)
USERSTATUS m_get_userstatus(Messenger *m, int friendnumber)
{
if (friend_is_valid(friendnumber,m))
if (friend_not_valid(m, friendnumber))
return USERSTATUS_INVALID;
USERSTATUS status = m->friendlist[friendnumber].userstatus;
@ -505,7 +508,7 @@ static int send_ping(Messenger *m, int friendnumber)
static int set_friend_statusmessage(Messenger *m, int friendnumber, uint8_t *status, uint16_t length)
{
if (friend_is_valid(friendnumber,m))
if (friend_not_valid(m, friendnumber))
return -1;
uint8_t *newstatus = calloc(length, 1);
@ -527,7 +530,7 @@ void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno)
if (yesno != 0 || yesno != 1)
return;
if (friend_is_valid(friendnumber,m))
if (friend_not_valid(m, friendnumber))
return;
m->friendlist[friendnumber].receives_read_receipts = yesno;
@ -609,7 +612,7 @@ void set_friend_status(Messenger *m, int friendnumber, uint8_t status)
int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length)
{
if (friend_is_valid(friendnumber,m))
if (friend_not_valid(m, friendnumber))
return 0;
if (length >= MAX_DATA_SIZE || m->friendlist[friendnumber].status != FRIEND_ONLINE)
@ -780,7 +783,7 @@ void doFriends(Messenger *m)
len = read_cryptpacket(m->net_crypto, m->friendlist[i].crypt_connection_id, temp);
uint8_t packet_id = temp[0];
uint8_t *data = temp + 1;
int data_length = len - 1;
uint32_t data_length = len - 1;
if (len > 0) {
switch (packet_id) {
@ -856,7 +859,7 @@ void doFriends(Messenger *m)
case PACKET_ID_RECEIPT: {
uint32_t msgid;
if (data_length < 0 || (uint32_t)data_length < sizeof(msgid))
if (data_length < sizeof(msgid))
break;
memcpy(&msgid, data, sizeof(msgid));

View File

@ -32,7 +32,10 @@
#define CONN_ESTABLISHED 3
#define CONN_TIMED_OUT 4
static 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; }
static uint8_t crypt_connection_id_not_valid(Net_Crypto *c, int crypt_connection_id)
{
return (uint32_t)crypt_connection_id >= c->crypto_connections_length;
}
/* Use this instead of memcmp; not vulnerable to timing attacks. */
uint8_t crypto_iszero(uint8_t *mem, uint32_t length)
@ -152,7 +155,7 @@ void random_nonce(uint8_t *nonce)
*/
int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data)
{
if (crypt_id_valid(crypt_connection_id,c))
if (crypt_connection_id_not_valid(c, crypt_connection_id))
return 0;
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)
*/
int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length)
{
if (crypt_id_valid(crypt_connection_id,c))
if (crypt_connection_id_not_valid(c, crypt_connection_id))
return 0;
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
} else { /* If request is not for us, try routing it. */
int retval = route_packet(dht, packet + 1, packet, length);
if (retval < 0 || (uint32_t)retval == length)
if ((unsigned int)retval == length)
return 0;
}
}
@ -508,7 +512,7 @@ int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, ui
*/
int crypto_kill(Net_Crypto *c, int crypt_connection_id)
{
if (crypt_id_valid(crypt_connection_id,c))
if (crypt_connection_id_not_valid(c, crypt_connection_id))
return 1;
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,
*/
int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id)
{
if (crypt_connection_id >= 0 && (uint32_t)crypt_connection_id < c->crypto_connections_length)
if ((unsigned int)crypt_connection_id < c->crypto_connections_length)
return c->crypto_connections[crypt_connection_id].status;
return CONN_NO_CONNECTION;