Skip to content

Commit 1213364

Browse files
vivien-applepull[bot]
authored andcommitted
[PacketBufferHandle::IsNull] The response PacketBufferHandle is missing a null check (#25520)
1 parent d041d17 commit 1213364

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

src/protocols/echo/EchoServer.cpp

+5-8
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ namespace Echo {
3232
CHIP_ERROR EchoServer::Init(Messaging::ExchangeManager * exchangeMgr)
3333
{
3434
// Error if already initialized.
35-
if (mExchangeMgr != nullptr)
36-
return CHIP_ERROR_INCORRECT_STATE;
35+
VerifyOrReturnError(mExchangeMgr == nullptr, CHIP_ERROR_INCORRECT_STATE);
3736

3837
mExchangeMgr = exchangeMgr;
3938
OnEchoRequestReceived = nullptr;
@@ -63,9 +62,6 @@ CHIP_ERROR EchoServer::OnUnsolicitedMessageReceived(const PayloadHeader & payloa
6362
CHIP_ERROR EchoServer::OnMessageReceived(Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader,
6463
System::PacketBufferHandle && payload)
6564
{
66-
CHIP_ERROR err = CHIP_NO_ERROR;
67-
System::PacketBufferHandle response;
68-
6965
// NOTE: we already know this is an Echo Request message because we explicitly registered with the
7066
// Exchange Manager for unsolicited Echo Requests.
7167

@@ -75,6 +71,8 @@ CHIP_ERROR EchoServer::OnMessageReceived(Messaging::ExchangeContext * ec, const
7571
OnEchoRequestReceived(ec, payload.Retain());
7672
}
7773

74+
System::PacketBufferHandle response;
75+
7876
// Since we are re-using the inbound EchoRequest buffer to send the EchoResponse, if necessary,
7977
// adjust the position of the payload within the buffer to ensure there is enough room for the
8078
// outgoing network headers. This is necessary because in some network stack configurations,
@@ -86,12 +84,11 @@ CHIP_ERROR EchoServer::OnMessageReceived(Messaging::ExchangeContext * ec, const
8684
else
8785
{
8886
response = MessagePacketBuffer::NewWithData(payload->Start(), payload->DataLength());
87+
VerifyOrReturnError(!response.IsNull(), CHIP_ERROR_NO_MEMORY);
8988
}
9089

9190
// Send an Echo Response back to the sender.
92-
err = ec->SendMessage(MsgType::EchoResponse, std::move(response));
93-
94-
return err;
91+
return ec->SendMessage(MsgType::EchoResponse, std::move(response));
9592
}
9693

9794
} // namespace Echo

0 commit comments

Comments
 (0)