From c98cc237b3d342b8eca0c9355c1ac4c011da663a Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Fri, 19 Jun 2020 16:42:42 +0200 Subject: [PATCH] net.c: fix memory leak for query in handle_request The storage for query was not released when coap_add_token() has failed. As the query is used only if successful, the storage allocation is moved downwards to improve handling of error cases. This fixes CID 1483550. --- src/net.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/net.c b/src/net.c index 035ae836..458e4895 100644 --- a/src/net.c +++ b/src/net.c @@ -2242,8 +2242,6 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu h = resource->handler[pdu->code - 1]; if (h) { - coap_string_t *query = coap_get_query(pdu); - int owns_query = 1; coap_log(LOG_DEBUG, "call custom handler for resource '%*.*s'\n", (int)resource->uri_path->length, (int)resource->uri_path->length, resource->uri_path->s); @@ -2259,6 +2257,8 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu coap_opt_iterator_t opt_iter; coap_opt_t *observe = NULL; int observe_action = COAP_OBSERVE_CANCEL; + coap_string_t *query = coap_get_query(pdu); + int owns_query = 1; /* check for Observe option RFC7641 and RFC8132 */ if (resource->observable &&