From ca470ea18069410bb6a63115709617bc94ed6fc6 Mon Sep 17 00:00:00 2001 From: Sergey Polyakov Date: Fri, 9 Aug 2024 11:01:43 +0200 Subject: [PATCH] Bugfix --- communication/src/coap_util.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/communication/src/coap_util.cpp b/communication/src/coap_util.cpp index 54e62c402f..116ec38990 100644 --- a/communication/src/coap_util.cpp +++ b/communication/src/coap_util.cpp @@ -30,16 +30,25 @@ #include "coap_message_decoder.h" #include "str_util.h" #include "logging.h" +#include "scope_guard.h" #include "check.h" namespace particle::protocol { int sendEmptyAckOrRst(MessageChannel& channel, Message& msg, CoapType type) { + auto msgLen = msg.length(); + auto msgCapacity = msg.capacity(); Message resp; - auto err = channel.response(msg, resp, msg.capacity() - msg.length()); + auto err = channel.response(msg, resp, msgCapacity - msgLen); if (err != ProtocolError::NO_ERROR) { return toSystemError(err); } + SCOPE_GUARD({ + // BufferMessageChannel::response() alters the capacity of the original message. Restore it + // so that the calling code could still use the extra space available in the message + msg.set_buffer(msg.buf(), msgCapacity); + msg.set_length(msgLen); + }); CoapMessageEncoder e((char*)resp.buf(), resp.capacity()); e.type(type); e.code(CoapCode::EMPTY);