From 0e7e269ecb7bcdda6bcab264dcb6a46f8833b1e3 Mon Sep 17 00:00:00 2001 From: chrysn Date: Tue, 21 Jun 2016 18:57:15 +0200 Subject: [PATCH] net: Fix coap_new_endpoint error handling The wrong sequence caused segfaults instead of regular error returns using LWIP (where the error is visible eg. when binding to a different address type than the socket was created for). The free in the error path is fixed as well. (coap_malloc_type requires a coap_free_type as per documentation, although some implementations might not be strict about it). --- src/net.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/net.c b/src/net.c index 19762199..b2576282 100644 --- a/src/net.c +++ b/src/net.c @@ -380,12 +380,12 @@ coap_new_context( prng((unsigned char *)&c->message_id, sizeof(unsigned short)); c->endpoint = coap_new_endpoint(listen_addr, COAP_ENDPOINT_NOSEC); -#ifdef WITH_LWIP - c->endpoint->context = c; -#endif if (c->endpoint == NULL) { goto onerror; } +#ifdef WITH_LWIP + c->endpoint->context = c; +#endif #ifdef WITH_POSIX c->sockfd = c->endpoint->handle.fd; @@ -411,7 +411,7 @@ coap_new_context( return c; onerror: - coap_free(c); + coap_free_type(COAP_CONTEXT, c); return NULL; }