diff --git a/src/core/coap/coap_message.cpp b/src/core/coap/coap_message.cpp index 4998de7a6..6743f2055 100644 --- a/src/core/coap/coap_message.cpp +++ b/src/core/coap/coap_message.cpp @@ -97,7 +97,7 @@ void Message::Finish(void) if (GetHelpData().mPayloadMarkerSet && (GetHelpData().mHeaderLength == GetLength())) { - IgnoreError(SetLength(GetLength() - 1)); + RemoveFooter(sizeof(uint8_t)); } WriteBytes(0, &GetHelpData().mHeader, GetOptionStart()); diff --git a/src/core/common/message.cpp b/src/core/common/message.cpp index 3a4c342e7..72dd7d66f 100644 --- a/src/core/common/message.cpp +++ b/src/core/common/message.cpp @@ -547,6 +547,8 @@ exit: return error; } +void Message::RemoveFooter(uint16_t aLength) { IgnoreError(SetLength(GetLength() - Min(aLength, GetLength()))); } + void Message::GetFirstChunk(uint16_t aOffset, uint16_t &aLength, Chunk &aChunk) const { // This method gets the first message chunk (contiguous data diff --git a/src/core/common/message.hpp b/src/core/common/message.hpp index f3dfd5402..739d23776 100644 --- a/src/core/common/message.hpp +++ b/src/core/common/message.hpp @@ -666,6 +666,17 @@ public: */ Error InsertHeader(uint16_t aOffset, uint16_t aLength); + /** + * Removes footer bytes from the end of the message. + * + * The caller should ensure the message contains the bytes to be removed, otherwise as many bytes as available + * will be removed. + * + * @param[in] aLength Number of footer bytes to remove from end of the `Message`. + * + */ + void RemoveFooter(uint16_t aLength); + /** * Appends bytes to the end of the message. * diff --git a/src/core/net/dnssd_server.cpp b/src/core/net/dnssd_server.cpp index e05a7d9c1..f415f966d 100644 --- a/src/core/net/dnssd_server.cpp +++ b/src/core/net/dnssd_server.cpp @@ -974,10 +974,7 @@ void Server::ProxyQueryInfo::ReadFrom(const ProxyQuery &aQuery) SuccessOrAssert(aQuery.Read(aQuery.GetLength() - sizeof(ProxyQueryInfo), *this)); } -void Server::ProxyQueryInfo::RemoveFrom(ProxyQuery &aQuery) const -{ - SuccessOrAssert(aQuery.SetLength(aQuery.GetLength() - sizeof(ProxyQueryInfo))); -} +void Server::ProxyQueryInfo::RemoveFrom(ProxyQuery &aQuery) const { aQuery.RemoveFooter(sizeof(ProxyQueryInfo)); } void Server::ProxyQueryInfo::UpdateIn(ProxyQuery &aQuery) const { diff --git a/src/core/net/ip6_mpl.cpp b/src/core/net/ip6_mpl.cpp index 61be9ea11..698c66f7a 100644 --- a/src/core/net/ip6_mpl.cpp +++ b/src/core/net/ip6_mpl.cpp @@ -465,10 +465,7 @@ void Mpl::Metadata::ReadFrom(const Message &aMessage) IgnoreError(aMessage.Read(length - sizeof(*this), *this)); } -void Mpl::Metadata::RemoveFrom(Message &aMessage) const -{ - SuccessOrAssert(aMessage.SetLength(aMessage.GetLength() - sizeof(*this))); -} +void Mpl::Metadata::RemoveFrom(Message &aMessage) const { aMessage.RemoveFooter(sizeof(*this)); } void Mpl::Metadata::UpdateIn(Message &aMessage) const { aMessage.Write(aMessage.GetLength() - sizeof(*this), *this); } diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 99e6809f8..85e0039c8 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -2397,7 +2397,7 @@ Error Mle::ProcessMessageSecurity(Crypto::AesCcm::Mode aMode, if (aMode == Crypto::AesCcm::kDecrypt) { // Skip decrypting the message under fuzz build mode - IgnoreError(aMessage.SetLength(aMessage.GetLength() - kMleSecurityTagSize)); + aMessage.RemoveFooter(kMleSecurityTagSize); ExitNow(); } #endif @@ -2412,7 +2412,7 @@ Error Mle::ProcessMessageSecurity(Crypto::AesCcm::Mode aMode, else { VerifyOrExit(aMessage.Compare(aMessage.GetLength() - kMleSecurityTagSize, tag), error = kErrorSecurity); - IgnoreError(aMessage.SetLength(aMessage.GetLength() - kMleSecurityTagSize)); + aMessage.RemoveFooter(kMleSecurityTagSize); } exit: @@ -4423,10 +4423,7 @@ void Mle::DelayedResponseMetadata::ReadFrom(const Message &aMessage) IgnoreError(aMessage.Read(length - sizeof(*this), *this)); } -void Mle::DelayedResponseMetadata::RemoveFrom(Message &aMessage) const -{ - SuccessOrAssert(aMessage.SetLength(aMessage.GetLength() - sizeof(*this))); -} +void Mle::DelayedResponseMetadata::RemoveFrom(Message &aMessage) const { aMessage.RemoveFooter(sizeof(*this)); } //--------------------------------------------------------------------------------------------------------------------- // TxMessage