Skip to content

Commit bf69b54

Browse files
committed
Fix memory leak when closing TCP connection.
Thanks to: TokTok#1216
1 parent fda74a8 commit bf69b54

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

toxcore/TCP_client.c

+1-13
Original file line numberDiff line numberDiff line change
@@ -346,18 +346,6 @@ static _Bool add_priority(TCP_Client_Connection *con, const uint8_t *packet, uin
346346
return 1;
347347
}
348348

349-
static void wipe_priority_list(TCP_Client_Connection *con)
350-
{
351-
TCP_Priority_List *p = con->priority_queue_start;
352-
353-
while (p) {
354-
TCP_Priority_List *pp = p;
355-
p = p->next;
356-
free(pp);
357-
}
358-
359-
}
360-
361349
/* return 1 on success.
362350
* return 0 if could not send packet.
363351
* return -1 on failure (connection must be killed).
@@ -960,7 +948,7 @@ void kill_TCP_connection(TCP_Client_Connection *TCP_connection)
960948
if (TCP_connection == NULL)
961949
return;
962950

963-
wipe_priority_list(TCP_connection);
951+
wipe_priority_list(TCP_connection->priority_queue_start);
964952
kill_sock(TCP_connection->sock);
965953
sodium_memzero(TCP_connection, sizeof(TCP_Client_Connection));
966954
free(TCP_connection);

toxcore/TCP_server.c

+11
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ static int del_accepted(TCP_Server *TCP_server, int index)
169169
if (!bs_list_remove(&TCP_server->accepted_key_list, TCP_server->accepted_connection_array[index].public_key, index))
170170
return -1;
171171

172+
wipe_priority_list(TCP_server->accepted_connection_array[index].priority_queue_start);
172173
sodium_memzero(&TCP_server->accepted_connection_array[index], sizeof(TCP_Secure_Connection));
173174
--TCP_server->num_accepted_connections;
174175

@@ -315,6 +316,15 @@ static int send_pending_data_nonpriority(TCP_Secure_Connection *con)
315316

316317
}
317318

319+
void wipe_priority_list(TCP_Priority_List *p)
320+
{
321+
while (p) {
322+
TCP_Priority_List *pp = p;
323+
p = p->next;
324+
free(pp);
325+
}
326+
}
327+
318328
/* return 0 if pending data was sent completely
319329
* return -1 if it wasn't
320330
*/
@@ -446,6 +456,7 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const
446456
*/
447457
static void kill_TCP_connection(TCP_Secure_Connection *con)
448458
{
459+
wipe_priority_list(con->priority_queue_start);
449460
kill_sock(con->sock);
450461
sodium_memzero(con, sizeof(TCP_Secure_Connection));
451462
}

toxcore/TCP_server.h

+2
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ typedef struct {
146146
TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uint16_t *ports, const uint8_t *secret_key,
147147
Onion *onion);
148148

149+
void wipe_priority_list(TCP_Priority_List *p);
150+
149151
/* Run the TCP_server
150152
*/
151153
void do_TCP_server(TCP_Server *TCP_server);

0 commit comments

Comments
 (0)