From 782b361fb1fc3ce3aa74553777c42d28bed31dd4 Mon Sep 17 00:00:00 2001 From: Kevin Schoedel Date: Thu, 16 Dec 2021 14:49:05 -0500 Subject: [PATCH] Avoid crash in reporting::Engine::ScheduleRun() #### Problem `reporting::Engine::ScheduleRun()` crashes if invoked after `SessionManager` has shut down. #### Change overview Check pointers. #### Testing Verified using pending PR #13060 with ``` server start server stop exit ``` which crashes without this change. --- src/app/reporting/Engine.cpp | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/app/reporting/Engine.cpp b/src/app/reporting/Engine.cpp index 089d2dae3b46ee..79471aef7556f6 100644 --- a/src/app/reporting/Engine.cpp +++ b/src/app/reporting/Engine.cpp @@ -432,16 +432,24 @@ CHIP_ERROR Engine::ScheduleRun() return CHIP_NO_ERROR; } - if (InteractionModelEngine::GetInstance()->GetExchangeManager() != nullptr) + Messaging::ExchangeManager * exchangeManager = InteractionModelEngine::GetInstance()->GetExchangeManager(); + if (exchangeManager == nullptr) { - mRunScheduled = true; - return InteractionModelEngine::GetInstance()->GetExchangeManager()->GetSessionManager()->SystemLayer()->ScheduleWork(Run, - this); + return CHIP_ERROR_INCORRECT_STATE; } - else + SessionManager * sessionManager = exchangeManager->GetSessionManager(); + if (sessionManager == nullptr) + { + return CHIP_ERROR_INCORRECT_STATE; + } + System::Layer * systemLayer = sessionManager->SystemLayer(); + if (systemLayer == nullptr) { return CHIP_ERROR_INCORRECT_STATE; } + ReturnErrorOnFailure(systemLayer->ScheduleWork(Run, this)); + mRunScheduled = true; + return CHIP_NO_ERROR; } void Engine::Run()