diff --git a/examples/coap-server.c b/examples/coap-server.c index 87307add..8f9b2fe3 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -1966,23 +1966,24 @@ do_call_home(coap_context_t *ctx, const char *local_bind, const char *local_port if (pdu) { coap_optlist_t *optlist_chain = NULL; - int ret; + int ret = -1; coap_pdu_t *resp_pdu = NULL; if (!coap_path_into_optlist(call_home_uri.path.s, call_home_uri.path.length, COAP_OPTION_URI_PATH, &optlist_chain)) - return 0; + goto error; if (call_home_uri.query.length) { if (!coap_query_into_optlist(call_home_uri.query.s, call_home_uri.query.length, COAP_OPTION_URI_QUERY, &optlist_chain)) - return 0; + goto error; } if (optlist_chain) { coap_add_optlist_pdu(pdu, &optlist_chain); coap_delete_optlist(optlist_chain); } ret = coap_send_recv(session, pdu, &resp_pdu, 2000); +error: coap_delete_pdu(pdu); coap_delete_pdu(resp_pdu); if (ret < 0) { diff --git a/src/coap_block.c b/src/coap_block.c index 90a01993..7dd7d094 100644 --- a/src/coap_block.c +++ b/src/coap_block.c @@ -4092,6 +4092,7 @@ coap_block_build_body_lkd(coap_binary_t *body_data, size_t length, /* Check no overflow (including a 8 byte small headroom) */ if (SIZE_MAX - length < 8 || offset > SIZE_MAX - length - 8) { + coap_delete_binary(body_data); return NULL; } diff --git a/src/coap_net.c b/src/coap_net.c index 01b9483a..7a79f863 100644 --- a/src/coap_net.c +++ b/src/coap_net.c @@ -776,8 +776,7 @@ coap_new_context(const coap_address_t *listen_addr) { c->dtls_context = coap_dtls_new_context(c); if (!c->dtls_context) { coap_log_emerg("coap_init: no DTLS context available\n"); - coap_free_context_lkd(c); - return NULL; + goto onerror; } } @@ -799,11 +798,10 @@ coap_new_context(const coap_address_t *listen_addr) { coap_lock_unlock(); return c; -#if defined(COAP_EPOLL_SUPPORT) || COAP_SERVER_SUPPORT onerror: + coap_lock_unlock(); coap_free_type(COAP_CONTEXT, c); return NULL; -#endif /* COAP_EPOLL_SUPPORT || COAP_SERVER_SUPPORT */ } COAP_API void @@ -4118,15 +4116,11 @@ skip_handler: COAP_SEND_INC_PDU) == COAP_INVALID_MID) coap_log_debug("cannot send response for mid=0x%x\n", mid); response = NULL; - if (query) - coap_delete_string(query); goto finish; } #endif /* COAP_Q_BLOCK_SUPPORT */ if (coap_send_internal(session, response, orig_pdu ? orig_pdu : pdu) == COAP_INVALID_MID) { coap_log_debug("cannot send response for mid=0x%04x\n", mid); - if (query) - coap_delete_string(query); goto finish; } } else { @@ -4167,8 +4161,6 @@ skip_handler: drop_it_no_debug: coap_delete_pdu_lkd(response); } - if (query) - coap_delete_string(query); #if COAP_Q_BLOCK_SUPPORT if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK1, &block)) { if (COAP_PROTO_RELIABLE(session->proto)) { @@ -4186,6 +4178,8 @@ drop_it_no_debug: #endif /* COAP_Q_BLOCK_SUPPORT */ finish: + if (query) + coap_delete_string(query); if (resource) coap_resource_release_lkd(resource); coap_delete_string(uri_path); diff --git a/tests/test_sendqueue.c b/tests/test_sendqueue.c index a6ccba00..ecca8d96 100644 --- a/tests/test_sendqueue.c +++ b/tests/test_sendqueue.c @@ -19,6 +19,13 @@ CU_ASSERT_PTR_NOT_NULL(value); \ if ((void*)value == NULL) return; +#define ReturnIf_CU_ASSERT_PTR_NOT_NULL_Code(value,code) \ + CU_ASSERT_PTR_NOT_NULL(value); \ + if ((void*)value == NULL) { \ + code; \ + return; \ + } + static coap_context_t *ctx; /* Holds the coap context for most tests */ static coap_session_t *session; /* Holds a reference-counted session object */ @@ -296,7 +303,7 @@ t_sendqueue11(void) { COAP_REQUEST_CODE_GET, 0x1234, 64); - ReturnIf_CU_ASSERT_PTR_NOT_NULL(sent_pdu); + ReturnIf_CU_ASSERT_PTR_NOT_NULL_Code(sent_pdu, coap_delete_node(queued)); queued->next = NULL; queued->session = coap_session_reference(session);