From 08f7b370b3da0a2ff5d40d81e95d1c8a7caf585a Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Thu, 5 Mar 2026 14:53:41 -0800 Subject: [PATCH] [coap] remove `Clone()` method from `Coap::Message` (#12616) This commit removes the redundant `Clone()` methods from `Coap::Message`. These methods previously served to retain the `HeaderOffset` across the cloned message, which was internally tracked by modifying `MeshDest`. Since `ot::Message::Clone()` natively preserves the `MeshDest` property, the specialized `Coap::Message::Clone()` wrappers are unnecessary. Callers now directly use `ot::Message::Clone()` and type-cast the result using `AsCoapMessagePtr()`. --- src/core/coap/coap.cpp | 15 +++++++------- src/core/coap/coap_message.cpp | 16 --------------- src/core/coap/coap_message.hpp | 37 ---------------------------------- 3 files changed, 7 insertions(+), 61 deletions(-) diff --git a/src/core/coap/coap.cpp b/src/core/coap/coap.cpp index 303b635f4..95b220754 100644 --- a/src/core/coap/coap.cpp +++ b/src/core/coap/coap.cpp @@ -1024,11 +1024,10 @@ Error CoapBase::CacheLastBlockResponse(Message *aResponse) FreeLastBlockResponse(); - if ((mLastResponse = aResponse->Clone()) == nullptr) - { - error = kErrorNoBufs; - } + mLastResponse = AsCoapMessagePtr(aResponse->Clone()); + VerifyOrExit(mLastResponse != nullptr, error = kErrorNoBufs); +exit: return error; } @@ -1529,7 +1528,7 @@ Error CoapBase::PendingRequests::AddClone(const Message &aMessage, uint16_t aCop { Error error = kErrorNone; - aRequest.mMessage = aMessage.Clone(aCopyLength); + aRequest.mMessage = AsCoapMessagePtr(aMessage.Clone(aCopyLength)); VerifyOrExit(aRequest.HasMessage(), error = kErrorNoBufs); SuccessOrExit(error = aRequest.AppendMetadataToMessage()); @@ -1669,7 +1668,7 @@ void CoapBase::PendingRequests::RetransmitRequest(const Request &aRequest) Message *clone; Ip6::MessageInfo messageInfo; - clone = aRequest.mMessage->Clone(aRequest.mMessage->GetLength() - sizeof(Request::Metadata)); + clone = AsCoapMessagePtr(aRequest.mMessage->Clone(aRequest.mMessage->GetLength() - sizeof(Request::Metadata))); VerifyOrExit(clone != nullptr, error = kErrorNoBufs); aRequest.mMetadata.CopyInfoTo(messageInfo); @@ -1784,7 +1783,7 @@ Error CoapBase::ResponseCache::SendCachedResponse(const Msg &aRxMsg, CoapBase &a VerifyOrExit(match != nullptr, error = kErrorNotFound); - response = match->Clone(match->GetLength() - sizeof(ResponseMetadata)); + response = AsCoapMessagePtr(match->Clone(match->GetLength() - sizeof(ResponseMetadata))); VerifyOrExit(response != nullptr, error = kErrorNoBufs); error = aCoapBase.Send(*response, aRxMsg.mMessageInfo); @@ -1830,7 +1829,7 @@ void CoapBase::ResponseCache::Add(const Msg &aTxMsg, uint32_t aExchangeLifetime) MaintainCacheSize(); - responseClone = aTxMsg.mMessage.Clone(); + responseClone = AsCoapMessagePtr(aTxMsg.mMessage.Clone()); VerifyOrExit(responseClone != nullptr); metadata.mExpireTime = TimerMilli::GetNow() + aExchangeLifetime; diff --git a/src/core/coap/coap_message.cpp b/src/core/coap/coap_message.cpp index fad6fca2b..e53269875 100644 --- a/src/core/coap/coap_message.cpp +++ b/src/core/coap/coap_message.cpp @@ -690,22 +690,6 @@ exit: return hasSame; } -Message *Message::Clone(void) const { return Clone(GetLength()); } - -Message *Message::Clone(uint16_t aLength) const { return Clone(aLength, GetReserved()); } - -Message *Message::Clone(uint16_t aLength, uint16_t aReserveHeader) const -{ - Message *message = static_cast(ot::Message::Clone(aLength, aReserveHeader)); - - VerifyOrExit(message != nullptr); - - message->SetHeaderOffset(GetHeaderOffset()); - -exit: - return message; -} - #if OPENTHREAD_CONFIG_COAP_API_ENABLE const char *Message::CodeToString(void) const { diff --git a/src/core/coap/coap_message.hpp b/src/core/coap/coap_message.hpp index caefae8e7..db7d1bd48 100644 --- a/src/core/coap/coap_message.hpp +++ b/src/core/coap/coap_message.hpp @@ -826,43 +826,6 @@ public: */ Error AppendPayloadMarker(void); - /** - * Creates a copy of the message. - * - * It allocates the new message from the same message pool as the original one and copies the entire payload. The - * `Type`, `SubType`, `LinkSecurity`, `Offset`, and `Priority` fields on the cloned message are also - * copied from the original one. - * - * @returns A pointer to the message or `nullptr` if insufficient message buffers are available. - */ - Message *Clone(void) const; - - /** - * Creates a copy of this CoAP message. - * - * It allocates the new message from the same message pool as the original one and copies @p aLength octets - * of the payload. The `Type`, `SubType`, `LinkSecurity`, `Offset`, and `Priority` fields on the - * cloned message are also copied from the original one. - * - * @param[in] aLength Number of payload bytes to copy. - * - * @returns A pointer to the message or `nullptr` if insufficient message buffers are available. - */ - Message *Clone(uint16_t aLength) const; - - /** - * Creates a copy of the message using a given configuration. - * - * It allocates the new message from the same message pool as the original one. The `Type`, `SubType`, - * `LinkSecurity`, `Offset`, and `Priority` fields on the cloned message are copied from the original one. - * - * @param[in] aLength Number of message bytes to copy. - * @param[in] aReserveHeader Number of header bytes to reserve in the new cloned message. - * - * @returns A pointer to the message or `nullptr` if insufficient message buffers are available. - */ - Message *Clone(uint16_t aLength, uint16_t aReserveHeader) const; - /** * Returns a pointer to the next message after this as a `Coap::Message`. *