Properly fixed possible realloc with size zero problem.

This commit is contained in:
irungentoo 2013-08-18 10:27:03 -04:00
parent 558a80f1c3
commit 88ea4659e9
2 changed files with 12 additions and 4 deletions

View File

@ -37,11 +37,15 @@ static int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_i
return -1 if realloc fails */
int realloc_friendlist(Messenger *m, uint32_t num)
{
if (num * sizeof(Friend) == 0) return -1;
if (num == 0) {
free(m->friendlist);
m->friendlist = NULL;
return 0;
}
Friend *newfriendlist = realloc(m->friendlist, num * sizeof(Friend));
if (newfriendlist == NULL && num != 0)
if (newfriendlist == NULL)
return -1;
m->friendlist = newfriendlist;

View File

@ -406,11 +406,15 @@ static int getcryptconnection_id(uint8_t *public_key)
return -1 if realloc fails */
int realloc_cryptoconnection(uint32_t num)
{
if (num * sizeof(Crypto_Connection) == 0) return -1;
if (num == 0) {
free(crypto_connections);
crypto_connections = NULL;
return 0;
}
Crypto_Connection *newcrypto_connections = realloc(crypto_connections, num * sizeof(Crypto_Connection));
if (newcrypto_connections == NULL && num != 0)
if (newcrypto_connections == NULL)
return -1;
crypto_connections = newcrypto_connections;