From 9095e0e469eb0b6e91fe56a3c98930d464784b63 Mon Sep 17 00:00:00 2001 From: Tommi Date: Tue, 19 Jul 2022 12:46:51 +0200 Subject: [PATCH] [TCPConnection] Check for valid port_ in OnClose and OnConnect. Bug: chromium:1341261, chromium:1316996 Change-Id: I10931e731d2228f13f2df15419a8649e3e726cf0 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/268860 Commit-Queue: Tomas Gunnarsson Reviewed-by: Danil Chapovalov Cr-Commit-Position: refs/heads/main@{#37558} --- p2p/base/tcp_port.cc | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/p2p/base/tcp_port.cc b/p2p/base/tcp_port.cc index bc6ccab674..84d0a68252 100644 --- a/p2p/base/tcp_port.cc +++ b/p2p/base/tcp_port.cc @@ -282,7 +282,7 @@ ProtocolType TCPPort::GetProtocol() const { void TCPPort::OnNewConnection(rtc::AsyncListenSocket* socket, rtc::AsyncPacketSocket* new_socket) { - RTC_DCHECK(socket == listen_socket_.get()); + RTC_DCHECK_EQ(socket, listen_socket_.get()); for (const auto& option : socket_options_) { new_socket->SetOption(option.first, option.second); @@ -439,7 +439,13 @@ void TCPConnection::OnConnectionRequestResponse(StunRequest* req, } void TCPConnection::OnConnect(rtc::AsyncPacketSocket* socket) { - RTC_DCHECK(socket == socket_.get()); + RTC_DCHECK_EQ(socket, socket_.get()); + + if (!port_) { + RTC_LOG(LS_ERROR) << "TCPConnection: Port has been deleted."; + return; + } + // Do not use this port if the socket bound to an address not associated with // the desired network interface. This is seen in Chrome, where TCP sockets // cannot be given a binding address, and the platform is expected to pick @@ -492,10 +498,13 @@ void TCPConnection::OnConnect(rtc::AsyncPacketSocket* socket) { } void TCPConnection::OnClose(rtc::AsyncPacketSocket* socket, int error) { - RTC_DCHECK(socket == socket_.get()); + RTC_DCHECK_EQ(socket, socket_.get()); RTC_LOG(LS_INFO) << ToString() << ": Connection closed with error " << error; - RTC_DCHECK(port()); + if (!port_) { + RTC_LOG(LS_ERROR) << "TCPConnection: Port has been deleted."; + return; + } // Guard against the condition where IPC socket will call OnClose for every // packet it can't send. @@ -550,12 +559,12 @@ void TCPConnection::OnReadPacket(rtc::AsyncPacketSocket* socket, size_t size, const rtc::SocketAddress& remote_addr, const int64_t& packet_time_us) { - RTC_DCHECK(socket == socket_.get()); + RTC_DCHECK_EQ(socket, socket_.get()); Connection::OnReadPacket(data, size, packet_time_us); } void TCPConnection::OnReadyToSend(rtc::AsyncPacketSocket* socket) { - RTC_DCHECK(socket == socket_.get()); + RTC_DCHECK_EQ(socket, socket_.get()); Connection::OnReadyToSend(); }