Skip to content

Commit

Permalink
Fix segmentation fault error in response echo message (#3984)
Browse files Browse the repository at this point in the history
  • Loading branch information
yufengwangca authored and pull[bot] committed Jan 12, 2021
1 parent ad344ad commit 2532016
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
4 changes: 1 addition & 3 deletions src/protocols/echo/EchoClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void EchoClient::OnMessageReceived(ExchangeContext * ec, const PacketHeader & pa
{
ec->Close();
mExchangeCtx = nullptr;
ExitNow();
return;
}

// Remove the EC from the app state now. OnEchoResponseReceived can call
Expand All @@ -116,8 +116,6 @@ void EchoClient::OnMessageReceived(ExchangeContext * ec, const PacketHeader & pa
{
OnEchoResponseReceived(packetHeader.GetSourceNodeId().ValueOr(0), std::move(payload));
}

exit:;
}

void EchoClient::OnResponseTimeout(ExchangeContext * ec)
Expand Down
13 changes: 11 additions & 2 deletions src/protocols/echo/EchoServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,30 @@ void EchoServer::Shutdown()
void EchoServer::OnMessageReceived(ExchangeContext * ec, const PacketHeader & packetHeader, uint32_t protocolId, uint8_t msgType,
System::PacketBufferHandle payload)
{
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.

// Call the registered OnEchoRequestReceived handler, if any.
if (OnEchoRequestReceived != nullptr)
{
response = payload.Retain();
OnEchoRequestReceived(ec->GetPeerNodeId(), std::move(payload));
}
else
{
response = std::move(payload);
}

// 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,
// the incoming header size may be smaller than the outgoing size.
payload->EnsureReservedSize(CHIP_SYSTEM_CONFIG_HEADER_RESERVE_SIZE);
response->EnsureReservedSize(CHIP_SYSTEM_CONFIG_HEADER_RESERVE_SIZE);

// Send an Echo Response back to the sender.
ec->SendMessage(kProtocol_Echo, kEchoMessageType_EchoResponse, payload.Release_ForNow());
ec->SendMessage(kProtocol_Echo, kEchoMessageType_EchoResponse, response.Release_ForNow());

// Discard the exchange context.
ec->Close();
Expand Down

0 comments on commit 2532016

Please sign in to comment.