From 068fc7bc3375860a9542e8def2fb4385dd7150c3 Mon Sep 17 00:00:00 2001 From: yuzhouhang Date: Wed, 27 Nov 2024 14:50:37 +0800 Subject: [PATCH] fix lambda risk issues Signed-off-by: yuzhouhang --- .../collect/device_networking_collect.h | 2 +- .../include/collect/device_param_collect.h | 3 ++- .../collect/device_networking_collect.cpp | 26 ++++++++++++++++--- .../source/collect/device_param_collect.cpp | 9 +++++-- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/services/samgr/native/include/collect/device_networking_collect.h b/services/samgr/native/include/collect/device_networking_collect.h index 3538ed96..3836e4ec 100644 --- a/services/samgr/native/include/collect/device_networking_collect.h +++ b/services/samgr/native/include/collect/device_networking_collect.h @@ -74,7 +74,7 @@ private: bool ReportMissedEvents(); }; -class WorkHandler { +class WorkHandler : public std::enable_shared_from_this { public: WorkHandler(const sptr& collect) : collect_(collect) { diff --git a/services/samgr/native/include/collect/device_param_collect.h b/services/samgr/native/include/collect/device_param_collect.h index 3441f4df..76563f1f 100644 --- a/services/samgr/native/include/collect/device_param_collect.h +++ b/services/samgr/native/include/collect/device_param_collect.h @@ -39,7 +39,8 @@ private: std::set params_; }; -class SystemAbilityStatusChange : public SystemAbilityStatusChangeStub { +class SystemAbilityStatusChange : public SystemAbilityStatusChangeStub, + public std::enable_shared_from_this { public: void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; diff --git a/services/samgr/native/source/collect/device_networking_collect.cpp b/services/samgr/native/source/collect/device_networking_collect.cpp index 97ad65b0..e2ef08c9 100644 --- a/services/samgr/native/source/collect/device_networking_collect.cpp +++ b/services/samgr/native/source/collect/device_networking_collect.cpp @@ -293,7 +293,13 @@ void WorkHandler::ProcessEvent(uint32_t eventId) } if (!collect_->AddDeviceChangeListener()) { HILOGW("AddDeviceChangeListener retry"); - auto task = [this] {this->ProcessEvent(INIT_EVENT);}; + auto workHandler = shared_from_this(); + auto task = [workHandler] { + if (workHandler == nullptr) { + return; + } + workHandler->ProcessEvent(INIT_EVENT); + }; if (handler_ == nullptr) { HILOGE("NetworkingCollect ProcessEvent handler is null!"); return; @@ -308,7 +314,14 @@ bool WorkHandler::SendEvent(uint32_t eventId) HILOGE("NetworkingCollect SendEvent handler is null!"); return false; } - auto task = [this, eventId] {this->ProcessEvent(eventId);}; + auto workHandler = shared_from_this(); + auto task = [workHandler, eventId] { + if (workHandler == nullptr) { + HILOGE("WorkHandler_eventId is null!"); + return; + } + workHandler->ProcessEvent(eventId); + }; return handler_->PostTask(task); } @@ -318,7 +331,14 @@ bool WorkHandler::SendEvent(uint32_t eventId, uint64_t delayTime) HILOGE("NetworkingCollect SendEvent handler is null!"); return false; } - auto task = [this, eventId] {this->ProcessEvent(eventId);}; + auto workHandler = shared_from_this(); + auto task = [workHandler, eventId] { + if (workHandler == nullptr) { + HILOGE("workHandler_eventId_delayTime is null!"); + return; + } + workHandler->ProcessEvent(eventId); + }; return handler_->PostTask(task, delayTime); } } // namespace OHOS diff --git a/services/samgr/native/source/collect/device_param_collect.cpp b/services/samgr/native/source/collect/device_param_collect.cpp index 1a89f822..642f56bc 100644 --- a/services/samgr/native/source/collect/device_param_collect.cpp +++ b/services/samgr/native/source/collect/device_param_collect.cpp @@ -138,8 +138,13 @@ void SystemAbilityStatusChange::OnAddSystemAbility(int32_t systemAbilityId, cons HILOGE("DeviceParamCollect is nullptr"); return; } - auto task = [this] () { - deviceParamCollect_->WatchParameters(); + auto share = shared_from_this(); + auto task = [share] () { + if (!share) { + HILOGE("SystemAbilityStatusChange is nullptr"); + return; + } + share->deviceParamCollect_->WatchParameters(); }; deviceParamCollect_->PostDelayTask(task, 0); break; -- Gitee