net.c: re-arrange code to avoid memory leakage

On platforms other than Contiki coap_new_context() has returned NULL
when no listen address was supplied but did not free the memory that
was previously allocated for the context object.

This change re-arranges the checks to return before allocating memory.
This commit is contained in:
Olaf Bergmann committed 2016-12-01 16:50:11 +01:00
1 parent 41af3304f0
commit beeb6b62d4
1 file changed
+7 -8
+7 -8
View File
@@ -332,20 +332,19 @@ is_wkc(coap_key_t k) {
coap_context_t *
coap_new_context(
const coap_address_t *listen_addr) {
#ifndef WITH_CONTIKI
coap_context_t *c = coap_malloc_type(COAP_CONTEXT, sizeof( coap_context_t ) );
#endif /* not WITH_CONTIKI */
#ifdef WITH_CONTIKI
coap_context_t *c;
if (initialized)
return NULL;
#endif /* WITH_CONTIKI */
if (!listen_addr) {
coap_log(LOG_EMERG, "no listen address specified\n");
return NULL;
}
#ifdef WITH_CONTIKI
if (initialized)
return NULL;
#endif /* WITH_CONTIKI */
#ifndef WITH_CONTIKI
c = coap_malloc_type(COAP_CONTEXT, sizeof(coap_context_t));
#endif /* not WITH_CONTIKI */
coap_clock_init();
#ifdef WITH_LWIP