From 3c685a2b4d77c5e04c199c55b272595cd8dd3f63 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Sat, 27 Dec 2025 11:12:32 +0000 Subject: [PATCH] Coverity: Fix reported errors CID 1667651 CID 1667843 --- src/coap_block.c | 16 ++++++++++------ src/coap_resource.c | 5 +++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/coap_block.c b/src/coap_block.c index 890b2496..9058d5d9 100644 --- a/src/coap_block.c +++ b/src/coap_block.c @@ -982,13 +982,11 @@ coap_add_data_large_internal(coap_session_t *session, /* May need token of length 8, so account for this */ avail -= (pdu->actual_token.length < 8) ? 8 - pdu->actual_token.length : 0; - if (avail < 16 && ((ssize_t)length > avail || have_block_defined)) { - /* bad luck, this is the smallest block size */ - coap_log_debug("not enough space, even the smallest block does not fit (2)\n"); - goto fail; + if (avail < 16) { + blk_size = 0; + } else { + blk_size = coap_flsll((long long)avail) - 4 - 1; } - - blk_size = coap_flsll((long long)avail) - 4 - 1; if (blk_size > 6) blk_size = 6; @@ -1019,6 +1017,12 @@ coap_add_data_large_internal(coap_session_t *session, } #endif /* COAP_Q_BLOCK_SUPPORT */ + if (avail < 16 && ((ssize_t)length > avail || have_block_defined)) { + /* bad luck, this is the smallest block size */ + coap_log_debug("not enough space, even the smallest block does not fit (2)\n"); + goto fail; + } + chunk = (size_t)1 << (blk_size + 4); if ((have_block_defined && block.num != 0) || single_request || ((session->block_mode & COAP_BLOCK_STLESS_BLOCK2) && session->type != COAP_SESSION_TYPE_CLIENT)) { diff --git a/src/coap_resource.c b/src/coap_resource.c index ce3035a6..b902adc5 100644 --- a/src/coap_resource.c +++ b/src/coap_resource.c @@ -972,6 +972,11 @@ coap_add_observer(coap_resource_t *resource, ret += oscore_cbor_put_nil(&info_buf, &info_len); ret += oscore_cbor_put_nil(&info_buf, &info_len); } + if (ret > sizeof(info_buffer)) { + /* Should have been caught by assert() inoscborput_* functions */ + coap_log_warn("coap_add_observer overrun of info_buffer (%" PRIuS ")\n", ret); + ret = sizeof(info_buffer); + } oscore_info = coap_new_bin_const(info_buffer, ret); } #endif /* COAP_OSCORE_SUPPORT */