From c5d51c5b520e9a845ce46f5cf6c51f44736cce64 Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Tue, 7 Mar 2023 16:22:07 +0100 Subject: [PATCH] [PacketBufferHandle::IsNull] The response PacketBufferHandle is missing a null check (#25520) --- src/protocols/echo/EchoServer.cpp | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/protocols/echo/EchoServer.cpp b/src/protocols/echo/EchoServer.cpp index 6c11da35d4b78d..fc3865d5ccdaf8 100644 --- a/src/protocols/echo/EchoServer.cpp +++ b/src/protocols/echo/EchoServer.cpp @@ -32,8 +32,7 @@ namespace Echo { CHIP_ERROR EchoServer::Init(Messaging::ExchangeManager * exchangeMgr) { // Error if already initialized. - if (mExchangeMgr != nullptr) - return CHIP_ERROR_INCORRECT_STATE; + VerifyOrReturnError(mExchangeMgr == nullptr, CHIP_ERROR_INCORRECT_STATE); mExchangeMgr = exchangeMgr; OnEchoRequestReceived = nullptr; @@ -63,9 +62,6 @@ CHIP_ERROR EchoServer::OnUnsolicitedMessageReceived(const PayloadHeader & payloa CHIP_ERROR EchoServer::OnMessageReceived(Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader, System::PacketBufferHandle && payload) { - CHIP_ERROR err = CHIP_NO_ERROR; - System::PacketBufferHandle response; - // NOTE: we already know this is an Echo Request message because we explicitly registered with the // Exchange Manager for unsolicited Echo Requests. @@ -75,6 +71,8 @@ CHIP_ERROR EchoServer::OnMessageReceived(Messaging::ExchangeContext * ec, const OnEchoRequestReceived(ec, payload.Retain()); } + System::PacketBufferHandle response; + // Since we are re-using the inbound EchoRequest buffer to send the EchoResponse, if necessary, // adjust the position of the payload within the buffer to ensure there is enough room for the // outgoing network headers. This is necessary because in some network stack configurations, @@ -86,12 +84,11 @@ CHIP_ERROR EchoServer::OnMessageReceived(Messaging::ExchangeContext * ec, const else { response = MessagePacketBuffer::NewWithData(payload->Start(), payload->DataLength()); + VerifyOrReturnError(!response.IsNull(), CHIP_ERROR_NO_MEMORY); } // Send an Echo Response back to the sender. - err = ec->SendMessage(MsgType::EchoResponse, std::move(response)); - - return err; + return ec->SendMessage(MsgType::EchoResponse, std::move(response)); } } // namespace Echo