Skip to content

Commit 518c012

Browse files
CP [IM]Fix leaked readClient in onFabricRemoved call (#37265)
1 parent 593d5c6 commit 518c012

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/app/InteractionModelEngine.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -1864,12 +1864,20 @@ void InteractionModelEngine::OnFabricRemoved(const FabricTable & fabricTable, Fa
18641864
});
18651865

18661866
#if CHIP_CONFIG_ENABLE_READ_CLIENT
1867-
for (auto * readClient = mpActiveReadClientList; readClient != nullptr; readClient = readClient->GetNextClient())
1867+
for (auto * readClient = mpActiveReadClientList; readClient != nullptr;)
18681868
{
1869+
// ReadClient::Close may delete the read client so that readClient->GetNextClient() will be use-after-free.
1870+
// We need save readClient as nextReadClient before closing.
18691871
if (readClient->GetFabricIndex() == fabricIndex)
18701872
{
18711873
ChipLogProgress(InteractionModel, "Fabric removed, deleting obsolete read client with FabricIndex: %u", fabricIndex);
1874+
auto * nextReadClient = readClient->GetNextClient();
18721875
readClient->Close(CHIP_ERROR_IM_FABRIC_DELETED, false);
1876+
readClient = nextReadClient;
1877+
}
1878+
else
1879+
{
1880+
readClient = readClient->GetNextClient();
18731881
}
18741882
}
18751883
#endif // CHIP_CONFIG_ENABLE_READ_CLIENT

src/app/InteractionModelEngine.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ class InteractionModelEngine : public Messaging::UnsolicitedMessageHandler,
664664
bool mSubscriptionResumptionScheduled = false;
665665
#endif
666666

667-
FabricTable * mpFabricTable;
667+
FabricTable * mpFabricTable = nullptr;
668668

669669
CASESessionManager * mpCASESessionMgr = nullptr;
670670

0 commit comments

Comments
 (0)