From 89462d1d45e0eb627c8fb3b82473429bd8a10bec Mon Sep 17 00:00:00 2001 From: crisrael Date: Thu, 9 Jun 2022 11:07:10 -0700 Subject: [PATCH] [NC] Fix nullptr crashes in BasePcpHandler PiperOrigin-RevId: 453967262 --- .../implementation/base_pcp_handler.cc | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/connections/implementation/base_pcp_handler.cc b/connections/implementation/base_pcp_handler.cc index 187947aa..1b76b0fb 100644 --- a/connections/implementation/base_pcp_handler.cc +++ b/connections/implementation/base_pcp_handler.cc @@ -1417,28 +1417,20 @@ void BasePcpHandler::EvaluateConnectionResult(ClientProxy* client, CHECK(context); // there is no way how this can fail, if Verify succeeded. // If it did, it's a UKEY2 protocol bug. - channel_manager_->EncryptChannelForEndpoint(endpoint_id, - std::move(context)); - - client->GetAnalyticsRecorder().OnConnectionEstablished( - endpoint_id, - channel_manager_->GetChannelForEndpoint(endpoint_id)->GetMedium(), - connection_info.connection_token); + if (!channel_manager_->EncryptChannelForEndpoint(endpoint_id, + std::move(context))) { + response_code = {Status::kEndpointUnknown}; + } } else { NEARBY_LOGS(INFO) << "Pending connection rejected; endpoint_id=" << endpoint_id; response_code = {Status::kConnectionRejected}; } - // Invoke the client callback to let it know of the connection result. - if (response_code.Ok()) { - client->OnConnectionAccepted(endpoint_id); - } else { - client->OnConnectionRejected(endpoint_id, response_code); - } - // If the connection failed, clean everything up and short circuit. - if (!is_connection_accepted) { + if (!response_code.Ok()) { + client->OnConnectionRejected(endpoint_id, response_code); + // Clean up the channel in EndpointManager if it's no longer required. if (can_close_immediately) { endpoint_manager_->DiscardEndpoint(client, endpoint_id); @@ -1456,6 +1448,14 @@ void BasePcpHandler::EvaluateConnectionResult(ClientProxy* client, return; } + client->GetAnalyticsRecorder().OnConnectionEstablished( + endpoint_id, + channel_manager_->GetChannelForEndpoint(endpoint_id)->GetMedium(), + connection_info.connection_token); + + // Invoke the client callback to let it know of the connection result. + client->OnConnectionAccepted(endpoint_id); + // Kick off the bandwidth upgrade for incoming connections. if (connection_info.is_incoming && AutoUpgradeBandwidth(client->GetAdvertisingOptions())) {