From 8dcdbfb4779be708faa7712ce7d77aca2e45711f Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Tue, 11 Dec 2018 13:17:21 +0000 Subject: [PATCH] endpoint->hello information memory leak when coap_free_endpoint() called Information set up in the pseudo session held in endpoint->hello is not freed off when coap_free_endpoint() is called. A solution is to split coap_session_free() into two parts with coap_session_free() calling a new function coap_session_mfree(). Then coap_mfree_endpoint() can call coap_session_mfree() to free off the endpoint specific hello information. Makefile.am Add coap_mfree_endpoint and coap_session_mfree to the CTAGS_IGNORE list. libcoap-2.{map|sym} Remove coap_mfree_endpoint as it is not a public function include/coap2/coap_session.h Add in new coap_session_mfree() function definition src/coap_io.c Call coap_session_mfree() from within coap_mfree_endpoint() src/coap_session.c Move some of the functionality out of coap_session_free() into a new function coap_session_mfree() and call coap_session_mfree(). --- Makefile.am | 2 ++ include/coap2/coap_session.h | 1 + libcoap-2.map | 1 - libcoap-2.sym | 1 - src/coap_io.c | 2 ++ src/coap_session.c | 29 ++++++++++++++++------------- 6 files changed, 21 insertions(+), 15 deletions(-) diff --git a/Makefile.am b/Makefile.am index 7142f7d8..5b4d2fb1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -157,8 +157,10 @@ coap_dtls_send \ coap_dtls_session_update_mtu \ coap_dtls_startup \ coap_endpoint_new_dtls_session \ +coap_mfree_endpoint \ coap_packet_extract_pbuf \ coap_pdu_from_pbuf \ +coap_session_mfree \ coap_socket_accept_tcp \ coap_socket_bind_tcp \ coap_socket_bind_udp \ diff --git a/include/coap2/coap_session.h b/include/coap2/coap_session.h index a02e1916..ef152315 100644 --- a/include/coap2/coap_session.h +++ b/include/coap2/coap_session.h @@ -368,6 +368,7 @@ coap_session_t *coap_session_get_by_peer(struct coap_context_t *ctx, const struct coap_address_t *remote_addr, int ifindex); void coap_session_free(coap_session_t *session); +void coap_session_mfree(coap_session_t *session); /** * @defgroup cc Rate Control diff --git a/libcoap-2.map b/libcoap-2.map index a7baaf0c..584d9149 100644 --- a/libcoap-2.map +++ b/libcoap-2.map @@ -78,7 +78,6 @@ global: coap_malloc_endpoint; coap_malloc_type; coap_memory_init; - coap_mfree_endpoint; coap_network_read; coap_network_send; coap_new_client_session; diff --git a/libcoap-2.sym b/libcoap-2.sym index 05bf66a1..b52ce217 100644 --- a/libcoap-2.sym +++ b/libcoap-2.sym @@ -76,7 +76,6 @@ coap_log_impl coap_malloc_endpoint coap_malloc_type coap_memory_init -coap_mfree_endpoint coap_network_read coap_network_send coap_new_client_session diff --git a/src/coap_io.c b/src/coap_io.c index 9a6b7896..37ecbf0a 100644 --- a/src/coap_io.c +++ b/src/coap_io.c @@ -94,6 +94,7 @@ struct coap_endpoint_t * void coap_mfree_endpoint(struct coap_endpoint_t *ep) { ep_initialized = 0; + coap_session_mfree(&ep->hello); } int @@ -183,6 +184,7 @@ struct coap_endpoint_t * void coap_mfree_endpoint(struct coap_endpoint_t *ep) { + coap_session_mfree(&ep->hello); coap_free_type(COAP_ENDPOINT, ep); } diff --git a/src/coap_session.c b/src/coap_session.c index 89244196..ce5a762c 100644 --- a/src/coap_session.c +++ b/src/coap_session.c @@ -144,14 +144,9 @@ coap_make_session(coap_proto_t proto, coap_session_type_t type, return session; } -void coap_session_free(coap_session_t *session) { +void coap_session_mfree(coap_session_t *session) { coap_queue_t *q, *tmp; - if (!session) - return; - assert(session->ref == 0); - if (session->ref) - return; if (session->partial_pdu) coap_delete_pdu(session->partial_pdu); if (session->proto == COAP_PROTO_DTLS) @@ -160,13 +155,6 @@ void coap_session_free(coap_session_t *session) { coap_tls_free_session(session); if (session->sock.flags != COAP_SOCKET_EMPTY) coap_socket_close(&session->sock); - if (session->endpoint) { - if (session->endpoint->sessions) - LL_DELETE(session->endpoint->sessions, session); - } else if (session->context) { - if (session->context->sessions) - LL_DELETE(session->context->sessions, session); - } if (session->psk_identity) coap_free(session->psk_identity); if (session->psk_key) @@ -177,7 +165,22 @@ void coap_session_free(coap_session_t *session) { session->context->nack_handler(session->context, session, q->pdu, session->proto == COAP_PROTO_DTLS ? COAP_NACK_TLS_FAILED : COAP_NACK_NOT_DELIVERABLE, q->id); coap_delete_node(q); } +} +void coap_session_free(coap_session_t *session) { + if (!session) + return; + assert(session->ref == 0); + if (session->ref) + return; + if (session->endpoint) { + if (session->endpoint->sessions) + LL_DELETE(session->endpoint->sessions, session); + } else if (session->context) { + if (session->context->sessions) + LL_DELETE(session->context->sessions, session); + } + coap_session_mfree(session); coap_log(LOG_DEBUG, "***%s: session closed\n", coap_session_str(session)); coap_free_type(COAP_SESSION, session);