From 5935867e687ef7f161f5c1c2d5aabeede830e888 Mon Sep 17 00:00:00 2001 From: jcmichelou Date: Mon, 8 Oct 2018 18:00:18 +0200 Subject: [PATCH] resource.c: Fix run-time exceptions and use C89 syntax. Fix read-after-free of the token by moving the code before the free. Fix print of uninitialized heap memory if the token length is 0. Use C89 syntax for compatibility. --- src/resource.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/resource.c b/src/resource.c index 1a24b818..dff489ec 100644 --- a/src/resource.c +++ b/src/resource.c @@ -706,6 +706,14 @@ coap_delete_observer(coap_resource_t *resource, coap_session_t *session, s = coap_find_observer(resource, session, token); + if ( s && coap_get_log_level() >= LOG_DEBUG ) { + char outbuf[2 * 8 + 1] = ""; + unsigned int i; + for ( i = 0; i < s->token_length; i++ ) + snprintf( &outbuf[2 * i], 3, "%02x", s->token[i] ); + coap_log( LOG_DEBUG, "removed observer tid %s\n", outbuf ); + } + if (resource->subscribers && s) { LL_DELETE(resource->subscribers, s); coap_session_release( session ); @@ -714,16 +722,6 @@ coap_delete_observer(coap_resource_t *resource, coap_session_t *session, COAP_FREE_TYPE(subscription,s); } - if (s) { - char outbuf[2*s->token_length+1]; - unsigned int i; - for (i = 0; i < s->token_length; i++) { - snprintf(&outbuf[2*i], 3, - "%02x", s->token[i]); - } - coap_log(LOG_DEBUG, "removed observer tid %s\n", outbuf); - } - return s != NULL; }