Skip to content

Commit 1309734

Browse files
bzbarsky-applepull[bot]
authored andcommitted
Make sure we clean up properly if StartWatchingSocket fails. (#28245)
If StartWatchingSocket failed (e.g. due to us being out of socket watch pool space), we would leave the UDPEndPointImplSockets in a bad state where its destructor would try to treat the un-initialized mWatch value as a pointer. The fix is to make sure we clean up properly on StartWatchingSocket failure.
1 parent a27952b commit 1309734

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/inet/UDPEndPointImplSockets.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,14 @@ CHIP_ERROR UDPEndPointImplSockets::GetSocket(IPAddressType addressType)
469469
{
470470
return CHIP_ERROR_POSIX(errno);
471471
}
472-
ReturnErrorOnFailure(static_cast<System::LayerSockets *>(&GetSystemLayer())->StartWatchingSocket(mSocket, &mWatch));
472+
CHIP_ERROR err = static_cast<System::LayerSockets *>(&GetSystemLayer())->StartWatchingSocket(mSocket, &mWatch);
473+
if (err != CHIP_NO_ERROR)
474+
{
475+
// Our mWatch is not valid; make sure we never use it.
476+
close(mSocket);
477+
mSocket = kInvalidSocketFd;
478+
return err;
479+
}
473480

474481
mAddrType = addressType;
475482

0 commit comments

Comments
 (0)