Skip to content

Commit a2f630d

Browse files
authored
Handle a leaked TCP pointer more gracefully (#41753)
* Handle a leaked TCP pointer more gracefully This is ~ a use-after-free, i.e. someone keeping a pointer to a connection that has been closed & not using HandleConnectionClosed to reset the pointer A VerifyOrDie here does not help us track the culprit * Fix CI build failure
1 parent 9afb7cc commit a2f630d

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/transport/raw/TCP.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,9 @@ ActiveTCPConnectionState * TCPBase::AllocateConnection(const Inet::TCPEndPointHa
141141
{
142142
for (size_t i = 0; i < mActiveConnectionsSize; i++)
143143
{
144-
if (!mActiveConnections[i].InUse())
144+
ActiveTCPConnectionState * activeConnection = &mActiveConnections[i];
145+
if (!activeConnection->InUse() && (activeConnection->GetReferenceCount() == 0))
145146
{
146-
ActiveTCPConnectionState * activeConnection = &mActiveConnections[i];
147-
VerifyOrDie(activeConnection->GetReferenceCount() == 0);
148147
// Update state for the active connection
149148
activeConnection->Init(endpoint, address, [this](auto & conn) { TCPDisconnect(conn, true); });
150149
return activeConnection;
@@ -155,7 +154,14 @@ ActiveTCPConnectionState * TCPBase::AllocateConnection(const Inet::TCPEndPointHa
155154
// (i.e. that have a ref count of 0)
156155
for (size_t i = 0; i < mActiveConnectionsSize; i++)
157156
{
158-
ActiveTCPConnectionHandle releaseUnclaimed(&mActiveConnections[i]);
157+
ActiveTCPConnectionState * activeConnection = &mActiveConnections[i];
158+
if (!activeConnection->InUse() && (activeConnection->GetReferenceCount() != 0))
159+
{
160+
char addrStr[Transport::PeerAddress::kMaxToStringSize];
161+
activeConnection->mPeerAddr.ToString(addrStr);
162+
ChipLogError(Inet, "Leaked TCP connection %p to %s.", activeConnection, addrStr);
163+
}
164+
ActiveTCPConnectionHandle releaseUnclaimed(activeConnection);
159165
}
160166
}
161167
return nullptr;

0 commit comments

Comments
 (0)