Skip to content

Commit 1401234

Browse files
kghostpull[bot]
authored andcommitted
Fix UnauthenticatedSession leak (#10754)
* Fix UnauthenticatedSession leak * Resovle comments
1 parent ccfc01d commit 1401234

File tree

4 files changed

+51
-42
lines changed

4 files changed

+51
-42
lines changed

src/lib/core/Optional.h

+9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@
2929

3030
namespace chip {
3131

32+
/// An empty class type used to indicate optional type with uninitialized state.
33+
struct NullOptionalType
34+
{
35+
explicit NullOptionalType() = default;
36+
};
37+
constexpr NullOptionalType NullOptional{};
38+
3239
/**
3340
* Pairs an object with a boolean value to determine if the object value
3441
* is actually valid or not.
@@ -38,6 +45,8 @@ class Optional
3845
{
3946
public:
4047
constexpr Optional() : mHasValue(false) {}
48+
constexpr Optional(NullOptionalType) : mHasValue(false) {}
49+
4150
~Optional()
4251
{
4352
if (mHasValue)

src/transport/SessionManager.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ CHIP_ERROR SessionManager::SendPreparedMessage(SessionHandle session, const Encr
189189
else
190190
{
191191
auto unauthenticated = session.GetUnauthenticatedSession();
192-
mUnauthenticatedSessions.MarkSessionActive(unauthenticated.Get());
192+
mUnauthenticatedSessions.MarkSessionActive(unauthenticated);
193193
destination = &unauthenticated->GetPeerAddress();
194194

195195
ChipLogProgress(Inet,
@@ -339,13 +339,14 @@ void SessionManager::OnMessageReceived(const PeerAddress & peerAddress, System::
339339
void SessionManager::MessageDispatch(const PacketHeader & packetHeader, const Transport::PeerAddress & peerAddress,
340340
System::PacketBufferHandle && msg)
341341
{
342-
Transport::UnauthenticatedSession * session = mUnauthenticatedSessions.FindOrAllocateEntry(peerAddress);
343-
if (session == nullptr)
342+
Optional<Transport::UnauthenticatedSessionHandle> optionalSession = mUnauthenticatedSessions.FindOrAllocateEntry(peerAddress);
343+
if (!optionalSession.HasValue())
344344
{
345345
ChipLogError(Inet, "UnauthenticatedSession exhausted");
346346
return;
347347
}
348348

349+
Transport::UnauthenticatedSessionHandle session = optionalSession.Value();
349350
SessionManagerDelegate::DuplicateMessage isDuplicate = SessionManagerDelegate::DuplicateMessage::No;
350351

351352
// Verify message counter
@@ -357,7 +358,7 @@ void SessionManager::MessageDispatch(const PacketHeader & packetHeader, const Tr
357358
}
358359
VerifyOrDie(err == CHIP_NO_ERROR);
359360

360-
mUnauthenticatedSessions.MarkSessionActive(*session);
361+
mUnauthenticatedSessions.MarkSessionActive(session);
361362

362363
PayloadHeader payloadHeader;
363364
ReturnOnFailure(payloadHeader.DecodeAndConsume(msg));
@@ -374,8 +375,7 @@ void SessionManager::MessageDispatch(const PacketHeader & packetHeader, const Tr
374375

375376
if (mCB != nullptr)
376377
{
377-
mCB->OnMessageReceived(packetHeader, payloadHeader, SessionHandle(Transport::UnauthenticatedSessionHandle(*session)),
378-
peerAddress, isDuplicate, std::move(msg));
378+
mCB->OnMessageReceived(packetHeader, payloadHeader, SessionHandle(session), peerAddress, isDuplicate, std::move(msg));
379379
}
380380
}
381381

src/transport/SessionManager.h

+2-5
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,8 @@ class DLL_EXPORT SessionManager : public TransportMgrDelegate
265265

266266
Optional<SessionHandle> CreateUnauthenticatedSession(const Transport::PeerAddress & peerAddress)
267267
{
268-
Transport::UnauthenticatedSession * session = mUnauthenticatedSessions.FindOrAllocateEntry(peerAddress);
269-
if (session == nullptr)
270-
return Optional<SessionHandle>::Missing();
271-
272-
return Optional<SessionHandle>::Value(SessionHandle(Transport::UnauthenticatedSessionHandle(*session)));
268+
Optional<Transport::UnauthenticatedSessionHandle> session = mUnauthenticatedSessions.FindOrAllocateEntry(peerAddress);
269+
return session.HasValue() ? MakeOptional<SessionHandle>(session.Value()) : NullOptional;
273270
}
274271

275272
private:

src/transport/UnauthenticatedSessionTable.h

+34-31
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class UnauthenticatedSessionDeleter
4444
* @brief
4545
* An UnauthenticatedSession stores the binding of TransportAddress, and message counters.
4646
*/
47-
class UnauthenticatedSession : public ReferenceCounted<UnauthenticatedSession, UnauthenticatedSessionDeleter>
47+
class UnauthenticatedSession : public ReferenceCounted<UnauthenticatedSession, UnauthenticatedSessionDeleter, 0>
4848
{
4949
public:
5050
UnauthenticatedSession(const PeerAddress & address) : mPeerAddress(address) { mLocalMessageCounter.Init(); }
@@ -82,6 +82,39 @@ template <size_t kMaxConnectionCount, Time::Source kTimeSource = Time::Source::k
8282
class UnauthenticatedSessionTable
8383
{
8484
public:
85+
/**
86+
* Get a session given the peer address. If the session doesn't exist in the cache, allocate a new entry for it.
87+
*
88+
* @return the session found or allocated, nullptr if not found and allocation failed.
89+
*/
90+
CHECK_RETURN_VALUE
91+
Optional<UnauthenticatedSessionHandle> FindOrAllocateEntry(const PeerAddress & address)
92+
{
93+
UnauthenticatedSession * result = FindEntry(address);
94+
if (result != nullptr)
95+
return MakeOptional<UnauthenticatedSessionHandle>(*result);
96+
97+
CHIP_ERROR err = AllocEntry(address, result);
98+
if (err == CHIP_NO_ERROR)
99+
{
100+
return MakeOptional<UnauthenticatedSessionHandle>(*result);
101+
}
102+
else
103+
{
104+
return Optional<UnauthenticatedSessionHandle>::Missing();
105+
}
106+
}
107+
108+
/// Mark a session as active
109+
void MarkSessionActive(UnauthenticatedSessionHandle session)
110+
{
111+
session->SetLastActivityTimeMs(mTimeSource.GetCurrentMonotonicTimeMs());
112+
}
113+
114+
/// Allows access to the underlying time source used for keeping track of connection active time
115+
Time::TimeSource<kTimeSource> & GetTimeSource() { return mTimeSource; }
116+
117+
private:
85118
/**
86119
* Allocates a new session out of the internal resource pool.
87120
*
@@ -125,36 +158,6 @@ class UnauthenticatedSessionTable
125158
return result;
126159
}
127160

128-
/**
129-
* Get a peer given the peer id. If the peer doesn't exist in the cache, allocate a new entry for it.
130-
*
131-
* @return the peer found or allocated, nullptr if not found and allocate failed.
132-
*/
133-
CHECK_RETURN_VALUE
134-
UnauthenticatedSession * FindOrAllocateEntry(const PeerAddress & address)
135-
{
136-
UnauthenticatedSession * result = FindEntry(address);
137-
if (result != nullptr)
138-
return result;
139-
140-
CHIP_ERROR err = AllocEntry(address, result);
141-
if (err == CHIP_NO_ERROR)
142-
{
143-
return result;
144-
}
145-
else
146-
{
147-
return nullptr;
148-
}
149-
}
150-
151-
/// Mark a session as active
152-
void MarkSessionActive(UnauthenticatedSession & entry) { entry.SetLastActivityTimeMs(mTimeSource.GetCurrentMonotonicTimeMs()); }
153-
154-
/// Allows access to the underlying time source used for keeping track of connection active time
155-
Time::TimeSource<kTimeSource> & GetTimeSource() { return mTimeSource; }
156-
157-
private:
158161
UnauthenticatedSession * FindLeastRecentUsedEntry()
159162
{
160163
UnauthenticatedSession * result = nullptr;

0 commit comments

Comments
 (0)