Fix memory leak when closing TCP connection.

Thanks to: https://github.com/TokTok/c-toxcore/pull/1216
This commit is contained in:
irungentoo 2018-10-03 08:44:02 -04:00
parent fda74a8454
commit bf69b54f64
3 changed files with 14 additions and 13 deletions

View File

@ -346,18 +346,6 @@ static _Bool add_priority(TCP_Client_Connection *con, const uint8_t *packet, uin
return 1;
}
static void wipe_priority_list(TCP_Client_Connection *con)
{
TCP_Priority_List *p = con->priority_queue_start;
while (p) {
TCP_Priority_List *pp = p;
p = p->next;
free(pp);
}
}
/* return 1 on success.
* return 0 if could not send packet.
* return -1 on failure (connection must be killed).
@ -960,7 +948,7 @@ void kill_TCP_connection(TCP_Client_Connection *TCP_connection)
if (TCP_connection == NULL)
return;
wipe_priority_list(TCP_connection);
wipe_priority_list(TCP_connection->priority_queue_start);
kill_sock(TCP_connection->sock);
sodium_memzero(TCP_connection, sizeof(TCP_Client_Connection));
free(TCP_connection);

View File

@ -169,6 +169,7 @@ static int del_accepted(TCP_Server *TCP_server, int index)
if (!bs_list_remove(&TCP_server->accepted_key_list, TCP_server->accepted_connection_array[index].public_key, index))
return -1;
wipe_priority_list(TCP_server->accepted_connection_array[index].priority_queue_start);
sodium_memzero(&TCP_server->accepted_connection_array[index], sizeof(TCP_Secure_Connection));
--TCP_server->num_accepted_connections;
@ -315,6 +316,15 @@ static int send_pending_data_nonpriority(TCP_Secure_Connection *con)
}
void wipe_priority_list(TCP_Priority_List *p)
{
while (p) {
TCP_Priority_List *pp = p;
p = p->next;
free(pp);
}
}
/* return 0 if pending data was sent completely
* return -1 if it wasn't
*/
@ -446,6 +456,7 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const
*/
static void kill_TCP_connection(TCP_Secure_Connection *con)
{
wipe_priority_list(con->priority_queue_start);
kill_sock(con->sock);
sodium_memzero(con, sizeof(TCP_Secure_Connection));
}

View File

@ -146,6 +146,8 @@ typedef struct {
TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uint16_t *ports, const uint8_t *secret_key,
Onion *onion);
void wipe_priority_list(TCP_Priority_List *p);
/* Run the TCP_server
*/
void do_TCP_server(TCP_Server *TCP_server);