From 90853405767a19829ae3db8d3d32b2d42a2e52eb Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Mon, 4 Nov 2024 20:54:41 +0100 Subject: [PATCH] fixup! sys/net/nanocoap: fix coap_build_reply_header() --- sys/net/application_layer/nanocoap/nanocoap.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/sys/net/application_layer/nanocoap/nanocoap.c b/sys/net/application_layer/nanocoap/nanocoap.c index 2f35e066b734..e1ccd2e9a14a 100644 --- a/sys/net/application_layer/nanocoap/nanocoap.c +++ b/sys/net/application_layer/nanocoap/nanocoap.c @@ -592,20 +592,21 @@ ssize_t coap_build_reply_header(coap_pkt_t *pkt, unsigned code, } } + /* we need to update the header length with the actual one, as we may have + * used less bytes for the extended token length fields as our worst case + * assumption */ + hdr_len = bufpos - (uint8_t *)buf; + if (payload) { if (ct >= 0) { bufpos += coap_put_option_ct(bufpos, 0, ct); } *bufpos++ = COAP_PAYLOAD_MARKER; + hdr_len = bufpos - (uint8_t *)buf; *payload = bufpos; *payload_len_max = len - hdr_len; } - /* we need to update the header length with the actual one, as we may have - * used less bytes for the extended token length fields as our worst case - * assumption */ - hdr_len = bufpos - (uint8_t *)buf; - /* with the nanoCoAP API we can't detect the overflow before it happens */ assert(hdr_len <= len);