From 390171590b3934e12f78e27ee879e00174941b42 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Wed, 26 Jul 2023 10:40:32 -0400 Subject: [PATCH] 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. --- src/inet/UDPEndPointImplSockets.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/inet/UDPEndPointImplSockets.cpp b/src/inet/UDPEndPointImplSockets.cpp index 5c9748d0cf32fb..b681a56fabe92f 100644 --- a/src/inet/UDPEndPointImplSockets.cpp +++ b/src/inet/UDPEndPointImplSockets.cpp @@ -469,7 +469,14 @@ CHIP_ERROR UDPEndPointImplSockets::GetSocket(IPAddressType addressType) { return CHIP_ERROR_POSIX(errno); } - ReturnErrorOnFailure(static_cast(&GetSystemLayer())->StartWatchingSocket(mSocket, &mWatch)); + CHIP_ERROR err = static_cast(&GetSystemLayer())->StartWatchingSocket(mSocket, &mWatch); + if (err != CHIP_NO_ERROR) + { + // Our mWatch is not valid; make sure we never use it. + close(mSocket); + mSocket = kInvalidSocketFd; + return err; + } mAddrType = addressType;