Skip to content

Commit 6f1b823

Browse files
CP [IM]Fix leaked readClient in onFabricRemoved call (project-chip#37264)
1 parent f0a441f commit 6f1b823

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/app/InteractionModelEngine.cpp

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

18681868
#if CHIP_CONFIG_ENABLE_READ_CLIENT
1869-
for (auto * readClient = mpActiveReadClientList; readClient != nullptr; readClient = readClient->GetNextClient())
1869+
for (auto * readClient = mpActiveReadClientList; readClient != nullptr;)
18701870
{
1871+
// ReadClient::Close may delete the read client so that readClient->GetNextClient() will be use-after-free.
1872+
// We need save readClient as nextReadClient before closing.
18711873
if (readClient->GetFabricIndex() == fabricIndex)
18721874
{
18731875
ChipLogProgress(InteractionModel, "Fabric removed, deleting obsolete read client with FabricIndex: %u", fabricIndex);
1876+
auto * nextReadClient = readClient->GetNextClient();
18741877
readClient->Close(CHIP_ERROR_IM_FABRIC_DELETED, false);
1878+
readClient = nextReadClient;
1879+
}
1880+
else
1881+
{
1882+
readClient = readClient->GetNextClient();
18751883
}
18761884
}
18771885
#endif // CHIP_CONFIG_ENABLE_READ_CLIENT

src/app/InteractionModelEngine.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ class InteractionModelEngine : public Messaging::UnsolicitedMessageHandler,
318318
/**
319319
* @brief Function decrements the number of subscriptions to resume counter - mNumOfSubscriptionsToResume.
320320
* This should be called after we have completed a re-subscribe attempt on a persisted subscription wether the attempt
321-
* was succesful or not.
321+
* was successful or not.
322322
*/
323323
void DecrementNumSubscriptionsToResume();
324324
#endif // CHIP_CONFIG_PERSIST_SUBSCRIPTIONS
@@ -704,7 +704,7 @@ class InteractionModelEngine : public Messaging::UnsolicitedMessageHandler,
704704
#endif // CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION
705705
#endif // CHIP_CONFIG_PERSIST_SUBSCRIPTIONS
706706

707-
FabricTable * mpFabricTable;
707+
FabricTable * mpFabricTable = nullptr;
708708

709709
CASESessionManager * mpCASESessionMgr = nullptr;
710710

0 commit comments

Comments
 (0)