From bcee6f714935e116544a3ea154161ab02c488cf2 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Thu, 18 Dec 2025 10:20:19 +0000 Subject: [PATCH] Coverity: Fix reported errors CID 1667634 CID 1667636 CID 1667638 CID 1667639 CID 1667641 CID 1667642 CID 1667644 CID 1667646 CID 1667647 CID 1667649 CID 1667650 CID 1667651 --- examples/coap-server.c | 6 +++--- src/coap_block.c | 15 ++++++++------- src/coap_cache.c | 1 + src/coap_encode.c | 6 ++++++ src/coap_openssl.c | 3 ++- src/coap_proxy.c | 5 +++-- src/coap_resource.c | 16 ++++++++++++++-- 7 files changed, 37 insertions(+), 15 deletions(-) diff --git a/examples/coap-server.c b/examples/coap-server.c index 0f338916..139a0169 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -799,7 +799,7 @@ hnd_put_post(coap_resource_t *resource, dynamic_resource_t *resource_entry = NULL; coap_opt_iterator_t opt_iter; coap_opt_t *option; - coap_binary_t *data_so_far; + coap_binary_t *data_so_far = NULL; transient_value_t *transient_value; resource_entry = coap_resource_get_userdata(resource); @@ -967,8 +967,8 @@ individual_blocks(coap_session_t *session COAP_UNUSED, size_t offset, size_t total) { - fwrite(data, length, 1, stderr); - /* Keep all hte data, so that hnd_put_example_data() can also update things */ + (void)fwrite(data, length, 1, stderr); + /* Keep all the data, so that hnd_put_example_data() can also update things */ *body_data = coap_block_build_body(*body_data, length, data, offset, total); if (!(*body_data)) { diff --git a/src/coap_block.c b/src/coap_block.c index f3336dd7..890b2496 100644 --- a/src/coap_block.c +++ b/src/coap_block.c @@ -855,7 +855,7 @@ coap_add_data_large_internal(coap_session_t *session, if (dctx) { if (coap_is_mcast(&session->addr_info.local)) { - coap_digest_update(dctx, coap_unique_id, sizeof(coap_unique_id)); + (void)coap_digest_update(dctx, coap_unique_id, sizeof(coap_unique_id)); } if (coap_digest_update(dctx, data, length)) { if (coap_digest_final(dctx, &digest)) { @@ -981,6 +981,13 @@ coap_add_data_large_internal(coap_session_t *session, #endif /* COAP_OSCORE_SUPPORT */ /* 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; + } + blk_size = coap_flsll((long long)avail) - 4 - 1; if (blk_size > 6) blk_size = 6; @@ -1012,12 +1019,6 @@ 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_cache.c b/src/coap_cache.c index fe17a36d..efa0209e 100644 --- a/src/coap_cache.c +++ b/src/coap_cache.c @@ -208,6 +208,7 @@ coap_new_cache_entry_lkd(coap_session_t *session, const coap_pdu_t *pdu, } entry->cache_key = coap_cache_derive_key(session, pdu, session_based); if (!entry->cache_key) { + coap_delete_pdu_lkd(entry->pdu); coap_free_type(COAP_CACHE_ENTRY, entry); return NULL; } diff --git a/src/coap_encode.c b/src/coap_encode.c index 9431df24..844fd1f4 100644 --- a/src/coap_encode.c +++ b/src/coap_encode.c @@ -47,6 +47,9 @@ unsigned int coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int val) { unsigned int n, i; + if (val == 0) + return 0; + for (n = 0, i = val; i && n < sizeof(val); ++n) i >>= 8; @@ -78,6 +81,9 @@ coap_encode_var_safe8(uint8_t *buf, size_t length, uint64_t val) { unsigned int n, i; uint64_t tval = val; + if (val == 0) + return 0; + for (n = 0; tval && n < sizeof(val); ++n) tval >>= 8; diff --git a/src/coap_openssl.c b/src/coap_openssl.c index 4d67dc9c..6bf21ff3 100644 --- a/src/coap_openssl.c +++ b/src/coap_openssl.c @@ -1024,7 +1024,7 @@ coap_sock_write(BIO *a, const char *in, int inl) { if (!session) { errno = ENOMEM; - ret = -1; + return -1; } else { ret = (int)session->sock.lfunc[COAP_LAYER_TLS].l_write(session, (const uint8_t *)in, @@ -4631,6 +4631,7 @@ coap_crypto_hmac(cose_hmac_alg_t hmac_alg, return 1; } + coap_delete_binary(dummy); coap_crypto_output_errors("coap_crypto_hmac"); return 0; } diff --git a/src/coap_proxy.c b/src/coap_proxy.c index 6873da65..af416a3b 100644 --- a/src/coap_proxy.c +++ b/src/coap_proxy.c @@ -67,7 +67,7 @@ coap_proxy_log_entry(coap_session_t *incoming, const coap_pdu_t *pdu, "%02x", upstream_token->s[i]); } } - coap_print_addr(coap_session_get_addr_remote(incoming), addr, sizeof(addr)); + (void)coap_print_addr(coap_session_get_addr_remote(incoming), addr, sizeof(addr)); coap_log_debug(" proxy %-4s %s {%s}-{%s} \"%s\"%s\n", type, addr, scratch_u, scratch_d, url->s, coap_check_option(pdu, COAP_OPTION_OBSERVE, &opt_iter) ? " Observe" : ""); coap_delete_string(url); @@ -776,8 +776,9 @@ coap_proxy_call_response_handler(coap_session_t *session, const coap_pdu_t *sent if (!resp_pdu) return COAP_RESPONSE_FAIL; + assert(proxy_req); /* Incase change in type of request over the proxy */ - if (proxy_req && proxy_req->pdu) + if (proxy_req->pdu) resp_pdu->type = proxy_req->pdu->type; if (COAP_PROTO_NOT_RELIABLE(session->proto)) diff --git a/src/coap_resource.c b/src/coap_resource.c index 6227887e..ce3035a6 100644 --- a/src/coap_resource.c +++ b/src/coap_resource.c @@ -523,6 +523,7 @@ coap_free_resource(coap_resource_t *resource, coap_deleting_resource_t deleting) void coap_resource_release_lkd(coap_resource_t *resource) { +#ifndef __COVERITY__ assert(resource->ref); resource->ref--; if (resource->ref) @@ -538,6 +539,14 @@ coap_resource_release_lkd(coap_resource_t *resource) { } coap_free_type(COAP_RESOURCE, resource); +#else /* __COVERITY__ */ + /* Coverity scan is fooled by the reference counter leading to + * false positives for USE_AFTER_FREE. */ + --resource->ref; + __coverity_negative_sink__(resource->ref); + /* Indicate that resources are released properly. */ + __coverity_free__(resource); +#endif /* __COVERITY__ */ } COAP_API void @@ -1355,10 +1364,13 @@ cleanup: coap_session_release_lkd(obs_session); coap_pdu_release_lkd(obs_pdu); } - coap_resource_release_lkd(r); r->list_being_traversed = 0; + r->dirty = 0; + coap_resource_release_lkd(r); + /* r may be no more if elsewhere coap_free_resource() has been called */ + } else { + r->dirty = 0; } - r->dirty = 0; } COAP_API int