Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix segmentation fault error in response of echo message #3984

Merged
merged 1 commit into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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