From 1a459241515b0dd1257093bb1847cb22824700b8 Mon Sep 17 00:00:00 2001 From: John Jones Date: Tue, 8 Aug 2017 20:40:06 -0500 Subject: [PATCH] Fixing memory leak --- conn/session.c | 37 +++++++++++++++++++++++++++++++++++++ peer/peerstore.c | 5 ++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/conn/session.c b/conn/session.c index f9b93a8..3434672 100644 --- a/conn/session.c +++ b/conn/session.c @@ -1,6 +1,7 @@ #include #include #include "multiaddr/multiaddr.h" +#include "libp2p/crypto/ephemeral.h" #include "libp2p/conn/session.h" #include "libp2p/net/stream.h" @@ -47,6 +48,42 @@ int libp2p_session_context_free(struct SessionContext* context) { context->default_stream = NULL; context->insecure_stream = NULL; context->secure_stream = NULL; + if (context->chosen_cipher != NULL) { + free(context->chosen_cipher); + context->chosen_cipher = NULL; + } + if (context->chosen_curve != NULL) { + free(context->chosen_curve); + context->chosen_curve = NULL; + } + if (context->chosen_hash != NULL) { + free(context->chosen_hash); + context->chosen_hash = NULL; + } + if (context->shared_key != NULL) { + free(context->shared_key); + context->shared_key = NULL; + } + if (context->remote_peer_id != NULL) { + free(context->remote_peer_id); + context->remote_peer_id = NULL; + } + if (context->remote_ephemeral_public_key != NULL) { + free(context->remote_ephemeral_public_key); + context->remote_ephemeral_public_key = NULL; + } + if (context->local_stretched_key != NULL) { + libp2p_crypto_ephemeral_stretched_key_free(context->local_stretched_key); + context->local_stretched_key = NULL; + } + if (context->remote_stretched_key != NULL) { + libp2p_crypto_ephemeral_stretched_key_free(context->remote_stretched_key); + context->remote_stretched_key = NULL; + } + if (context->ephemeral_private_key != NULL) { + libp2p_crypto_ephemeral_key_free(context->ephemeral_private_key); + context->ephemeral_private_key = NULL; + } free(context); } return 1; diff --git a/peer/peerstore.c b/peer/peerstore.c index 66a58af..9c94072 100644 --- a/peer/peerstore.c +++ b/peer/peerstore.c @@ -69,13 +69,16 @@ int libp2p_peerstore_free(struct Peerstore* in) { if (in != NULL) { struct Libp2pLinkedList* current = in->head_entry; struct Libp2pLinkedList* next = NULL; + // first empty out the peer entries while (current != NULL) { next = current->next; libp2p_peer_entry_free((struct PeerEntry*)current->item); current->item = NULL; - libp2p_utils_linked_list_free(current); current = next; } + // now free the linked list entries + libp2p_utils_linked_list_free(in->head_entry); + // and finally the peerstore itself free(in); } return 1;