Skip to content

Commit

Permalink
Fix memory leak when closing TCP connection.
Browse files Browse the repository at this point in the history
Thanks to: TokTok#1216
  • Loading branch information
irungentoo committed Oct 3, 2018
1 parent fda74a8 commit bf69b54
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
14 changes: 1 addition & 13 deletions toxcore/TCP_client.c
Expand Up @@ -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).
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions toxcore/TCP_server.c
Expand Up @@ -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;

Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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));
}
Expand Down
2 changes: 2 additions & 0 deletions toxcore/TCP_server.h
Expand Up @@ -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.

Copy link
@kpp

kpp Oct 3, 2018

Contributor

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

This comment has been minimized.

Copy link
@iphydf

iphydf Apr 5, 2020

Contributor

We do, though. TCP_client also uses it.


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

0 comments on commit bf69b54

Please sign in to comment.