Permalink
Browse files

Fix memory leak when closing TCP connection.

Thanks to: TokTok#1216
  • Loading branch information...
irungentoo committed Oct 3, 2018
1 parent fda74a8 commit bf69b54f64003d160d759068f4816b2d9b2e1e21
Showing with 14 additions and 13 deletions.
  1. +1 −13 toxcore/TCP_client.c
  2. +11 −0 toxcore/TCP_server.c
  3. +2 −0 toxcore/TCP_server.h
View
@@ -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
@@ -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
@@ -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);

This comment has been minimized.

Show comment
Hide comment
@kpp

kpp Oct 3, 2018

Contributor

No need to put it in .h since you don't want to export this function.

@kpp

kpp Oct 3, 2018

Contributor

No need to put it in .h since you don't want to export this function.

/* Run the TCP_server
*/
void do_TCP_server(TCP_Server *TCP_server);

0 comments on commit bf69b54

Please sign in to comment.