From 784c2ddc5f397b4bf4ff5ce54d53579931b89b1d Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Fri, 19 Jun 2020 16:49:13 +0200 Subject: [PATCH] coap-server.c: fix memory leak in hnd_unkown_put() Reverse the sequence of sanity checks to avoid allocating a new object when not necessary. This fixes CID 1483549. --- examples/coap-server.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/examples/coap-server.c b/examples/coap-server.c index 58687f6e..6f1cabd6 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -647,15 +647,16 @@ hnd_unknown_put(coap_context_t *ctx, coap_resource_t *r; coap_string_t *uri_path; - /* get the uri_path - will will get used by coap_resource_init() */ - uri_path = coap_get_uri_path(request); - if (!uri_path) { - response->code = COAP_RESPONSE_CODE(404); + /* check if creating a new resource is allowed */ + if (dynamic_count >= support_dynamic) { + response->code = COAP_RESPONSE_CODE(406); return; } - if (dynamic_count >= support_dynamic) { - response->code = COAP_RESPONSE_CODE(406); + /* get the uri_path - will get used by coap_resource_init() */ + uri_path = coap_get_uri_path(request); + if (!uri_path) { + response->code = COAP_RESPONSE_CODE(404); return; } @@ -675,8 +676,6 @@ hnd_unknown_put(coap_context_t *ctx, /* Do the PUT for this first call */ hnd_put(ctx, r, session, request, token, query, response); - - return; } static void