From 008cff1d5172a00f29c1e1bf089550efcc72d2d0 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Thu, 26 Mar 2026 09:43:40 +0000 Subject: [PATCH] Coverity: Fix CID 1483550, 1670422-1670425 CID 1594475 is a false positive as multiple threads cannot execute in the same code as global_lock is set. --- src/coap_block.c | 6 ++++++ src/coap_net.c | 12 ++++-------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/coap_block.c b/src/coap_block.c index 8e604f2d..d83d2f6a 100644 --- a/src/coap_block.c +++ b/src/coap_block.c @@ -3659,6 +3659,12 @@ coap_block_build_body_lkd(coap_binary_t *body_data, size_t length, if (body_data == NULL) return NULL; + /* 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; + } + /* Update saved data */ if (offset + length <= total && body_data->length >= total) { memcpy(&body_data->s[offset], data, length); diff --git a/src/coap_net.c b/src/coap_net.c index 96ec632f..fb08067d 100644 --- a/src/coap_net.c +++ b/src/coap_net.c @@ -700,8 +700,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; } } @@ -723,11 +722,10 @@ coap_new_context(const coap_address_t *listen_addr) { coap_lock_unlock(c); 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 */ } void @@ -3579,8 +3577,6 @@ 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 */ @@ -3643,9 +3639,9 @@ drop_it_no_debug: } #endif /* COAP_Q_BLOCK_SUPPORT */ -#if COAP_Q_BLOCK_SUPPORT || COAP_THREAD_SAFE finish: -#endif /* COAP_Q_BLOCK_SUPPORT || COAP_THREAD_SAFE */ + if (query) + coap_delete_string(query); coap_delete_string(uri_path); return;