From c1147eb096b4810354ccb5796c7d4d84fb10980f Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Wed, 13 May 2026 10:06:51 +0100 Subject: [PATCH] coap_pdu.c: Support checking for extended tokens when large pdu created Picked up by oss-fuzz --- src/coap_pdu.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/coap_pdu.c b/src/coap_pdu.c index 708c512b..f88a706e 100644 --- a/src/coap_pdu.c +++ b/src/coap_pdu.c @@ -136,21 +136,23 @@ coap_pdu_init(coap_pdu_type_t type, coap_pdu_code_t code, coap_mid_t mid, pdu->alloc_size = min(size, COAP_DEFAULT_MTU); #ifdef WITH_LWIP - pdu->pbuf = pbuf_alloc(PBUF_TRANSPORT, pdu->alloc_size + pdu->max_hdr_size, PBUF_RAM); + pdu->pbuf = pbuf_alloc(PBUF_TRANSPORT, max(size, COAP_DEFAULT_MTU) + pdu->max_hdr_size, + PBUF_RAM); if (pdu->pbuf == NULL) { coap_free_type(COAP_PDU, pdu); return NULL; } pdu->token = (uint8_t *)pdu->pbuf->payload + pdu->max_hdr_size; -#else /* WITH_LWIP */ +#else /* ! WITH_LWIP */ uint8_t *buf; - buf = coap_malloc_type(COAP_PDU_BUF, pdu->alloc_size + pdu->max_hdr_size); + /* + 2 is needed if size == 0 and we are checking for extended tokens */ + buf = coap_malloc_type(COAP_PDU_BUF, max(2, pdu->alloc_size) + pdu->max_hdr_size); if (buf == NULL) { coap_free_type(COAP_PDU, pdu); return NULL; } pdu->token = buf + pdu->max_hdr_size; -#endif /* WITH_LWIP */ +#endif /* ! WITH_LWIP */ coap_pdu_clear(pdu, size); pdu->mid = mid; pdu->type = type;