Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 912adec

Browse files
committedMar 8, 2024·
Use Optional<ExchangeHandle> variable instead of ExchangeContext in PairingSession.
This ensures that the ExchangeContext for the session is automatically reference counted without explicit manual Retain() and Release() calls. As a result, the ExchangeContext is held until PairingSession::Clear() gets called that, in turn, calls ClearValue() on the Optional to internally call Release() on the underlying target. Fixes Issue project-chip#32498.
1 parent b742587 commit 912adec

File tree

4 files changed

+63
-59
lines changed

4 files changed

+63
-59
lines changed
 

‎src/protocols/secure_channel/CASESession.cpp

+25-23
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,8 @@ CHIP_ERROR CASESession::EstablishSession(SessionManager & sessionManager, Fabric
514514

515515
// We are setting the exchange context specifically before checking for error.
516516
// This is to make sure the exchange will get closed if Init() returned an error.
517-
mExchangeCtxt = exchangeCtxt;
517+
ExchangeHandle ecHandle(*exchangeCtxt);
518+
mExchangeCtxt.SetValue(ecHandle);
518519

519520
// From here onwards, let's go to exit on error, as some state might have already
520521
// been initialized
@@ -527,7 +528,7 @@ CHIP_ERROR CASESession::EstablishSession(SessionManager & sessionManager, Fabric
527528
mSessionResumptionStorage = sessionResumptionStorage;
528529
mLocalMRPConfig = mrpLocalConfig.ValueOr(GetDefaultMRPConfig());
529530

530-
mExchangeCtxt->UseSuggestedResponseTimeout(kExpectedSigma1ProcessingTime);
531+
mExchangeCtxt.Value()->UseSuggestedResponseTimeout(kExpectedSigma1ProcessingTime);
531532
mPeerNodeId = peerScopedNodeId.GetNodeId();
532533
mLocalNodeId = fabricInfo->GetNodeId();
533534

@@ -549,7 +550,7 @@ void CASESession::OnResponseTimeout(ExchangeContext * ec)
549550
{
550551
MATTER_TRACE_SCOPE("OnResponseTimeout", "CASESession");
551552
VerifyOrReturn(ec != nullptr, ChipLogError(SecureChannel, "CASESession::OnResponseTimeout was called by null exchange"));
552-
VerifyOrReturn(mExchangeCtxt == ec, ChipLogError(SecureChannel, "CASESession::OnResponseTimeout exchange doesn't match"));
553+
VerifyOrReturn(&mExchangeCtxt.Value().Get() == ec, ChipLogError(SecureChannel, "CASESession::OnResponseTimeout exchange doesn't match"));
553554
ChipLogError(SecureChannel, "CASESession timed out while waiting for a response from the peer. Current state was %u",
554555
to_underlying(mState));
555556
MATTER_TRACE_COUNTER("CASETimeout");
@@ -735,8 +736,8 @@ CHIP_ERROR CASESession::SendSigma1()
735736
ReturnErrorOnFailure(mCommissioningHash.AddData(ByteSpan{ msg_R1->Start(), msg_R1->DataLength() }));
736737

737738
// Call delegate to send the msg to peer
738-
ReturnErrorOnFailure(mExchangeCtxt->SendMessage(Protocols::SecureChannel::MsgType::CASE_Sigma1, std::move(msg_R1),
739-
SendFlags(SendMessageFlags::kExpectResponse)));
739+
ReturnErrorOnFailure(mExchangeCtxt.Value()->SendMessage(Protocols::SecureChannel::MsgType::CASE_Sigma1, std::move(msg_R1),
740+
SendFlags(SendMessageFlags::kExpectResponse)));
740741

741742
mState = resuming ? State::kSentSigma1Resume : State::kSentSigma1;
742743

@@ -959,8 +960,8 @@ CHIP_ERROR CASESession::SendSigma2Resume()
959960
ReturnErrorOnFailure(tlvWriter.Finalize(&msg_R2_resume));
960961

961962
// Call delegate to send the msg to peer
962-
ReturnErrorOnFailure(mExchangeCtxt->SendMessage(Protocols::SecureChannel::MsgType::CASE_Sigma2Resume, std::move(msg_R2_resume),
963-
SendFlags(SendMessageFlags::kExpectResponse)));
963+
ReturnErrorOnFailure(mExchangeCtxt.Value()->SendMessage(Protocols::SecureChannel::MsgType::CASE_Sigma2Resume, std::move(msg_R2_resume),
964+
SendFlags(SendMessageFlags::kExpectResponse)));
964965

965966
mState = State::kSentSigma2Resume;
966967

@@ -1096,8 +1097,8 @@ CHIP_ERROR CASESession::SendSigma2()
10961097
ReturnErrorOnFailure(mCommissioningHash.AddData(ByteSpan{ msg_R2->Start(), msg_R2->DataLength() }));
10971098

10981099
// Call delegate to send the msg to peer
1099-
ReturnErrorOnFailure(mExchangeCtxt->SendMessage(Protocols::SecureChannel::MsgType::CASE_Sigma2, std::move(msg_R2),
1100-
SendFlags(SendMessageFlags::kExpectResponse)));
1100+
ReturnErrorOnFailure(mExchangeCtxt.Value()->SendMessage(Protocols::SecureChannel::MsgType::CASE_Sigma2, std::move(msg_R2),
1101+
SendFlags(SendMessageFlags::kExpectResponse)));
11011102

11021103
mState = State::kSentSigma2;
11031104

@@ -1148,7 +1149,7 @@ CHIP_ERROR CASESession::HandleSigma2Resume(System::PacketBufferHandle && msg)
11481149
if (tlvReader.Next() != CHIP_END_OF_TLV)
11491150
{
11501151
SuccessOrExit(err = DecodeMRPParametersIfPresent(TLV::ContextTag(4), tlvReader));
1151-
mExchangeCtxt->GetSessionHandle()->AsUnauthenticatedSession()->SetRemoteSessionParameters(GetRemoteSessionParameters());
1152+
mExchangeCtxt.Value()->GetSessionHandle()->AsUnauthenticatedSession()->SetRemoteSessionParameters(GetRemoteSessionParameters());
11521153
}
11531154

11541155
ChipLogDetail(SecureChannel, "Peer assigned session session ID %d", responderSessionId);
@@ -1341,7 +1342,7 @@ CHIP_ERROR CASESession::HandleSigma2(System::PacketBufferHandle && msg)
13411342
if (tlvReader.Next() != CHIP_END_OF_TLV)
13421343
{
13431344
SuccessOrExit(err = DecodeMRPParametersIfPresent(TLV::ContextTag(kTag_Sigma2_ResponderMRPParams), tlvReader));
1344-
mExchangeCtxt->GetSessionHandle()->AsUnauthenticatedSession()->SetRemoteSessionParameters(GetRemoteSessionParameters());
1345+
mExchangeCtxt.Value()->GetSessionHandle()->AsUnauthenticatedSession()->SetRemoteSessionParameters(GetRemoteSessionParameters());
13451346
}
13461347

13471348
exit:
@@ -1410,7 +1411,7 @@ CHIP_ERROR CASESession::SendSigma3a()
14101411
{
14111412
SuccessOrExit(err = helper->ScheduleWork());
14121413
mSendSigma3Helper = helper;
1413-
mExchangeCtxt->WillSendMessage();
1414+
mExchangeCtxt.Value()->WillSendMessage();
14141415
mState = State::kSendSigma3Pending;
14151416
}
14161417
else
@@ -1537,8 +1538,8 @@ CHIP_ERROR CASESession::SendSigma3c(SendSigma3Data & data, CHIP_ERROR status)
15371538
SuccessOrExit(err);
15381539

15391540
// Call delegate to send the Msg3 to peer
1540-
err = mExchangeCtxt->SendMessage(Protocols::SecureChannel::MsgType::CASE_Sigma3, std::move(msg_R3),
1541-
SendFlags(SendMessageFlags::kExpectResponse));
1541+
err = mExchangeCtxt.Value()->SendMessage(Protocols::SecureChannel::MsgType::CASE_Sigma3, std::move(msg_R3),
1542+
SendFlags(SendMessageFlags::kExpectResponse));
15421543
SuccessOrExit(err);
15431544

15441545
ChipLogProgress(SecureChannel, "Sent Sigma3 msg");
@@ -1704,7 +1705,7 @@ CHIP_ERROR CASESession::HandleSigma3a(System::PacketBufferHandle && msg)
17041705

17051706
SuccessOrExit(err = helper->ScheduleWork());
17061707
mHandleSigma3Helper = helper;
1707-
mExchangeCtxt->WillSendMessage();
1708+
mExchangeCtxt.Value()->WillSendMessage();
17081709
mState = State::kHandleSigma3Pending;
17091710
}
17101711

@@ -2031,7 +2032,7 @@ CHIP_ERROR CASESession::ParseSigma1(TLV::ContiguousBufferTLVReader & tlvReader,
20312032
if (err == CHIP_NO_ERROR && tlvReader.GetTag() == ContextTag(kInitiatorMRPParamsTag))
20322033
{
20332034
ReturnErrorOnFailure(DecodeMRPParametersIfPresent(TLV::ContextTag(kInitiatorMRPParamsTag), tlvReader));
2034-
mExchangeCtxt->GetSessionHandle()->AsUnauthenticatedSession()->SetRemoteSessionParameters(GetRemoteSessionParameters());
2035+
mExchangeCtxt.Value()->GetSessionHandle()->AsUnauthenticatedSession()->SetRemoteSessionParameters(GetRemoteSessionParameters());
20352036
err = tlvReader.Next();
20362037
}
20372038

@@ -2079,26 +2080,27 @@ CHIP_ERROR CASESession::ParseSigma1(TLV::ContiguousBufferTLVReader & tlvReader,
20792080
return CHIP_NO_ERROR;
20802081
}
20812082

2082-
CHIP_ERROR CASESession::ValidateReceivedMessage(ExchangeContext * ec, const PayloadHeader & payloadHeader,
2083+
CHIP_ERROR CASESession::ValidateReceivedMessage(chip::Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader,
20832084
const System::PacketBufferHandle & msg)
20842085
{
20852086
VerifyOrReturnError(ec != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
20862087

20872088
// mExchangeCtxt can be nullptr if this is the first message (CASE_Sigma1) received by CASESession
20882089
// via UnsolicitedMessageHandler. The exchange context is allocated by exchange manager and provided
20892090
// to the handler (CASESession object).
2090-
if (mExchangeCtxt != nullptr)
2091+
if (mExchangeCtxt.HasValue())
20912092
{
2092-
if (mExchangeCtxt != ec)
2093+
if (&mExchangeCtxt.Value().Get() != ec)
20932094
{
20942095
ReturnErrorOnFailure(CHIP_ERROR_INVALID_ARGUMENT);
20952096
}
20962097
}
20972098
else
20982099
{
2099-
mExchangeCtxt = ec;
2100+
ExchangeHandle ecHandle(*ec);
2101+
mExchangeCtxt.SetValue(ecHandle);
21002102
}
2101-
mExchangeCtxt->UseSuggestedResponseTimeout(kExpectedHighProcessingTime);
2103+
mExchangeCtxt.Value()->UseSuggestedResponseTimeout(kExpectedHighProcessingTime);
21022104

21032105
VerifyOrReturnError(!msg.IsNull(), CHIP_ERROR_INVALID_ARGUMENT);
21042106
return CHIP_NO_ERROR;
@@ -2123,7 +2125,7 @@ CHIP_ERROR CASESession::OnMessageReceived(ExchangeContext * ec, const PayloadHea
21232125
//
21242126
// Should you need to resume the CASESession, you could theoretically pass along the msg to a callback that gets
21252127
// registered when setting mStopHandshakeAtState.
2126-
mExchangeCtxt->WillSendMessage();
2128+
mExchangeCtxt.Value()->WillSendMessage();
21272129
return CHIP_NO_ERROR;
21282130
}
21292131
#endif // CONFIG_BUILD_FOR_HOST_UNIT_TEST
@@ -2133,7 +2135,7 @@ CHIP_ERROR CASESession::OnMessageReceived(ExchangeContext * ec, const PayloadHea
21332135
msgType == Protocols::SecureChannel::MsgType::CASE_Sigma2Resume ||
21342136
msgType == Protocols::SecureChannel::MsgType::CASE_Sigma3)
21352137
{
2136-
SuccessOrExit(err = mExchangeCtxt->FlushAcks());
2138+
SuccessOrExit(err = mExchangeCtxt.Value()->FlushAcks());
21372139
}
21382140
#endif // CHIP_CONFIG_SLOW_CRYPTO
21392141

‎src/protocols/secure_channel/PASESession.cpp

+20-18
Original file line numberDiff line numberDiff line change
@@ -213,17 +213,18 @@ CHIP_ERROR PASESession::Pair(SessionManager & sessionManager, uint32_t peerSetUp
213213
{
214214
MATTER_TRACE_SCOPE("Pair", "PASESession");
215215
ReturnErrorCodeIf(exchangeCtxt == nullptr, CHIP_ERROR_INVALID_ARGUMENT);
216+
ExchangeHandle ecHandle(*exchangeCtxt);
216217
CHIP_ERROR err = Init(sessionManager, peerSetUpPINCode, delegate);
217218
SuccessOrExit(err);
218219

219220
mRole = CryptoContext::SessionRole::kInitiator;
220221

221-
mExchangeCtxt = exchangeCtxt;
222+
mExchangeCtxt.SetValue(ecHandle);
222223

223224
// When commissioning starts, the peer is assumed to be active.
224-
mExchangeCtxt->GetSessionHandle()->AsUnauthenticatedSession()->MarkActiveRx();
225+
mExchangeCtxt.Value()->GetSessionHandle()->AsUnauthenticatedSession()->MarkActiveRx();
225226

226-
mExchangeCtxt->UseSuggestedResponseTimeout(kExpectedLowProcessingTime);
227+
mExchangeCtxt.Value()->UseSuggestedResponseTimeout(kExpectedLowProcessingTime);
227228

228229
mLocalMRPConfig = mrpLocalConfig.ValueOr(GetDefaultMRPConfig());
229230

@@ -244,7 +245,7 @@ void PASESession::OnResponseTimeout(ExchangeContext * ec)
244245
{
245246
MATTER_TRACE_SCOPE("OnResponseTimeout", "PASESession");
246247
VerifyOrReturn(ec != nullptr, ChipLogError(SecureChannel, "PASESession::OnResponseTimeout was called by null exchange"));
247-
VerifyOrReturn(mExchangeCtxt == nullptr || mExchangeCtxt == ec,
248+
VerifyOrReturn(!mExchangeCtxt.HasValue() || &mExchangeCtxt.Value().Get() == ec,
248249
ChipLogError(SecureChannel, "PASESession::OnResponseTimeout exchange doesn't match"));
249250
// If we were waiting for something, mNextExpectedMsg had better have a value.
250251
ChipLogError(SecureChannel, "PASESession timed out while waiting for a response from the peer. Expected message type was %u",
@@ -309,7 +310,7 @@ CHIP_ERROR PASESession::SendPBKDFParamRequest()
309310
ReturnErrorOnFailure(mCommissioningHash.AddData(ByteSpan{ req->Start(), req->DataLength() }));
310311

311312
ReturnErrorOnFailure(
312-
mExchangeCtxt->SendMessage(MsgType::PBKDFParamRequest, std::move(req), SendFlags(SendMessageFlags::kExpectResponse)));
313+
mExchangeCtxt.Value()->SendMessage(MsgType::PBKDFParamRequest, std::move(req), SendFlags(SendMessageFlags::kExpectResponse)));
313314

314315
mNextExpectedMsg.SetValue(MsgType::PBKDFParamResponse);
315316

@@ -364,7 +365,7 @@ CHIP_ERROR PASESession::HandlePBKDFParamRequest(System::PacketBufferHandle && ms
364365
if (tlvReader.Next() != CHIP_END_OF_TLV)
365366
{
366367
SuccessOrExit(err = DecodeMRPParametersIfPresent(TLV::ContextTag(5), tlvReader));
367-
mExchangeCtxt->GetSessionHandle()->AsUnauthenticatedSession()->SetRemoteSessionParameters(GetRemoteSessionParameters());
368+
mExchangeCtxt.Value()->GetSessionHandle()->AsUnauthenticatedSession()->SetRemoteSessionParameters(GetRemoteSessionParameters());
368369
}
369370

370371
err = SendPBKDFParamResponse(ByteSpan(initiatorRandom), hasPBKDFParameters);
@@ -429,7 +430,7 @@ CHIP_ERROR PASESession::SendPBKDFParamResponse(ByteSpan initiatorRandom, bool in
429430
ReturnErrorOnFailure(SetupSpake2p());
430431

431432
ReturnErrorOnFailure(
432-
mExchangeCtxt->SendMessage(MsgType::PBKDFParamResponse, std::move(resp), SendFlags(SendMessageFlags::kExpectResponse)));
433+
mExchangeCtxt.Value()->SendMessage(MsgType::PBKDFParamResponse, std::move(resp), SendFlags(SendMessageFlags::kExpectResponse)));
433434
ChipLogDetail(SecureChannel, "Sent PBKDF param response");
434435

435436
mNextExpectedMsg.SetValue(MsgType::PASE_Pake1);
@@ -483,7 +484,7 @@ CHIP_ERROR PASESession::HandlePBKDFParamResponse(System::PacketBufferHandle && m
483484
if (tlvReader.Next() != CHIP_END_OF_TLV)
484485
{
485486
SuccessOrExit(err = DecodeMRPParametersIfPresent(TLV::ContextTag(5), tlvReader));
486-
mExchangeCtxt->GetSessionHandle()->AsUnauthenticatedSession()->SetRemoteSessionParameters(GetRemoteSessionParameters());
487+
mExchangeCtxt.Value()->GetSessionHandle()->AsUnauthenticatedSession()->SetRemoteSessionParameters(GetRemoteSessionParameters());
487488
}
488489

489490
// TODO - Add a unit test that exercises mHavePBKDFParameters path
@@ -508,7 +509,7 @@ CHIP_ERROR PASESession::HandlePBKDFParamResponse(System::PacketBufferHandle && m
508509
if (tlvReader.Next() != CHIP_END_OF_TLV)
509510
{
510511
SuccessOrExit(err = DecodeMRPParametersIfPresent(TLV::ContextTag(5), tlvReader));
511-
mExchangeCtxt->GetSessionHandle()->AsUnauthenticatedSession()->SetRemoteSessionParameters(GetRemoteSessionParameters());
512+
mExchangeCtxt.Value()->GetSessionHandle()->AsUnauthenticatedSession()->SetRemoteSessionParameters(GetRemoteSessionParameters());
512513
}
513514
}
514515

@@ -558,7 +559,7 @@ CHIP_ERROR PASESession::SendMsg1()
558559
ReturnErrorOnFailure(tlvWriter.Finalize(&msg));
559560

560561
ReturnErrorOnFailure(
561-
mExchangeCtxt->SendMessage(MsgType::PASE_Pake1, std::move(msg), SendFlags(SendMessageFlags::kExpectResponse)));
562+
mExchangeCtxt.Value()->SendMessage(MsgType::PASE_Pake1, std::move(msg), SendFlags(SendMessageFlags::kExpectResponse)));
562563
ChipLogDetail(SecureChannel, "Sent spake2p msg1");
563564

564565
mNextExpectedMsg.SetValue(MsgType::PASE_Pake2);
@@ -620,7 +621,7 @@ CHIP_ERROR PASESession::HandleMsg1_and_SendMsg2(System::PacketBufferHandle && ms
620621
SuccessOrExit(err = tlvWriter.EndContainer(outerContainerType));
621622
SuccessOrExit(err = tlvWriter.Finalize(&msg2));
622623

623-
err = mExchangeCtxt->SendMessage(MsgType::PASE_Pake2, std::move(msg2), SendFlags(SendMessageFlags::kExpectResponse));
624+
err = mExchangeCtxt.Value()->SendMessage(MsgType::PASE_Pake2, std::move(msg2), SendFlags(SendMessageFlags::kExpectResponse));
624625
SuccessOrExit(err);
625626

626627
mNextExpectedMsg.SetValue(MsgType::PASE_Pake3);
@@ -696,7 +697,7 @@ CHIP_ERROR PASESession::HandleMsg2_and_SendMsg3(System::PacketBufferHandle && ms
696697
SuccessOrExit(err = tlvWriter.EndContainer(outerContainerType));
697698
SuccessOrExit(err = tlvWriter.Finalize(&msg3));
698699

699-
err = mExchangeCtxt->SendMessage(MsgType::PASE_Pake3, std::move(msg3), SendFlags(SendMessageFlags::kExpectResponse));
700+
err = mExchangeCtxt.Value()->SendMessage(MsgType::PASE_Pake3, std::move(msg3), SendFlags(SendMessageFlags::kExpectResponse));
700701
SuccessOrExit(err);
701702

702703
mNextExpectedMsg.SetValue(MsgType::StatusReport);
@@ -785,25 +786,26 @@ CHIP_ERROR PASESession::ValidateReceivedMessage(ExchangeContext * exchange, cons
785786
// mExchangeCtxt can be nullptr if this is the first message (PBKDFParamRequest) received by PASESession
786787
// via UnsolicitedMessageHandler. The exchange context is allocated by exchange manager and provided
787788
// to the handler (PASESession object).
788-
if (mExchangeCtxt != nullptr)
789+
if (mExchangeCtxt.HasValue())
789790
{
790-
if (mExchangeCtxt != exchange)
791+
if (&mExchangeCtxt.Value().Get() != exchange)
791792
{
792793
ReturnErrorOnFailure(CHIP_ERROR_INVALID_ARGUMENT);
793794
}
794795
}
795796
else
796797
{
797-
mExchangeCtxt = exchange;
798+
ExchangeHandle ecHandle(*exchange);
799+
mExchangeCtxt.SetValue(ecHandle);
798800
}
799801

800-
if (!mExchangeCtxt->GetSessionHandle()->IsUnauthenticatedSession())
802+
if (!mExchangeCtxt.Value()->GetSessionHandle()->IsUnauthenticatedSession())
801803
{
802804
ChipLogError(SecureChannel, "PASESession received PBKDFParamRequest over encrypted session. Ignoring.");
803805
return CHIP_ERROR_INCORRECT_STATE;
804806
}
805807

806-
mExchangeCtxt->UseSuggestedResponseTimeout(kExpectedHighProcessingTime);
808+
mExchangeCtxt.Value()->UseSuggestedResponseTimeout(kExpectedHighProcessingTime);
807809

808810
VerifyOrReturnError(!msg.IsNull(), CHIP_ERROR_INVALID_ARGUMENT);
809811
VerifyOrReturnError((mNextExpectedMsg.HasValue() && payloadHeader.HasMessageType(mNextExpectedMsg.Value())) ||
@@ -832,7 +834,7 @@ CHIP_ERROR PASESession::OnMessageReceived(ExchangeContext * exchange, const Payl
832834
if (msgType == MsgType::PBKDFParamRequest || msgType == MsgType::PBKDFParamResponse || msgType == MsgType::PASE_Pake1 ||
833835
msgType == MsgType::PASE_Pake2 || msgType == MsgType::PASE_Pake3)
834836
{
835-
SuccessOrExit(err = mExchangeCtxt->FlushAcks());
837+
SuccessOrExit(err = mExchangeCtxt.Value()->FlushAcks());
836838
}
837839
#endif // CHIP_CONFIG_SLOW_CRYPTO
838840

‎src/protocols/secure_channel/PairingSession.cpp

+14-14
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ CHIP_ERROR PairingSession::ActivateSecureSession(const Transport::PeerAddress &
5656

5757
void PairingSession::Finish()
5858
{
59-
Transport::PeerAddress address = mExchangeCtxt->GetSessionHandle()->AsUnauthenticatedSession()->GetPeerAddress();
59+
Transport::PeerAddress address = mExchangeCtxt.Value()->GetSessionHandle()->AsUnauthenticatedSession()->GetPeerAddress();
6060

6161
// Discard the exchange so that Clear() doesn't try closing it. The exchange will handle that.
6262
DiscardExchange();
@@ -79,14 +79,15 @@ void PairingSession::Finish()
7979

8080
void PairingSession::DiscardExchange()
8181
{
82-
if (mExchangeCtxt != nullptr)
82+
if (mExchangeCtxt.HasValue())
8383
{
8484
// Make sure the exchange doesn't try to notify us when it closes,
8585
// since we might be dead by then.
86-
mExchangeCtxt->SetDelegate(nullptr);
86+
mExchangeCtxt.Value()->SetDelegate(nullptr);
87+
8788
// Null out mExchangeCtxt so that Clear() doesn't try closing it. The
8889
// exchange will handle that.
89-
mExchangeCtxt = nullptr;
90+
mExchangeCtxt.ClearValue();
9091
}
9192
}
9293

@@ -227,19 +228,18 @@ bool PairingSession::IsSessionEstablishmentInProgress()
227228

228229
void PairingSession::Clear()
229230
{
230-
// Clear acts like the destructor if PairingSession, if it is call during
231-
// middle of a pairing, means we should terminate the exchange. For normal
232-
// path, the exchange should already be discarded before calling Clear.
233-
if (mExchangeCtxt != nullptr)
231+
// Clear acts like the destructor of PairingSession, if it is called during
232+
// the middle of pairing, which means we should terminate the exchange. For the
233+
// normal path, the exchange should already be discarded before calling Clear.
234+
if (mExchangeCtxt.HasValue())
234235
{
235-
// The only time we reach this is if we are getting destroyed in the
236-
// middle of our handshake. In that case, there is no point trying to
237-
// do MRP resends of the last message we sent, so abort the exchange
236+
// The only time we reach this is when we are getting destroyed in the
237+
// middle of our handshake. In that case, there is no point in trying to
238+
// do MRP resends of the last message we sent. So, abort the exchange
238239
// instead of just closing it.
239-
mExchangeCtxt->Abort();
240-
mExchangeCtxt = nullptr;
240+
mExchangeCtxt.Value()->Abort();
241+
mExchangeCtxt.ClearValue();
241242
}
242-
243243
mSecureSessionHolder.Release();
244244
mPeerSessionId.ClearValue();
245245
mSessionManager = nullptr;

0 commit comments

Comments
 (0)