From bdbcf29bd1c467ac6251c88fe1ca8f78660bef3d Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Thu, 23 Oct 2025 15:30:14 +0100 Subject: [PATCH] coap_session.c: Fix GCC array bounds warning for memcpy Note assert() added from #1209 has now been removed as not needed by #1628, and if NDEBUG is defined then the assert() would have been a noop anyway. --- include/coap3/coap_pdu.h | 5 +++-- src/coap_session.c | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/include/coap3/coap_pdu.h b/include/coap3/coap_pdu.h index 8d3f8f3d..e3d795b3 100644 --- a/include/coap3/coap_pdu.h +++ b/include/coap3/coap_pdu.h @@ -307,8 +307,9 @@ coap_pdu_t *coap_pdu_from_pbuf(struct pbuf *pbuf); #endif /** -* CoAP protocol types -*/ + * CoAP protocol types + * Note: coap_layers_coap[] needs updating if extended. + */ typedef enum coap_proto_t { COAP_PROTO_NONE = 0, COAP_PROTO_UDP, diff --git a/src/coap_session.c b/src/coap_session.c index 95c0b727..dc746100 100644 --- a/src/coap_session.c +++ b/src/coap_session.c @@ -2096,6 +2096,13 @@ coap_new_endpoint_lkd(coap_context_t *context, const coap_address_t *listen_addr ep->sock.endpoint = ep; assert(proto < COAP_PROTO_LAST); memcpy(&ep->sock.lfunc, coap_layers_coap[proto], sizeof(ep->sock.lfunc)); + if (proto < COAP_PROTO_LAST) { + /* + * Should be caught above, but some compilers realize that proto + * includes COAP_PROTO_LAST, but there is no table entry for that + */ + memcpy(&ep->sock.lfunc, coap_layers_coap[proto], sizeof(ep->sock.lfunc)); + } if (COAP_PROTO_NOT_RELIABLE(proto)) { if (!coap_netif_dgrm_listen(ep, listen_addr))