From bb4eed9cf78b27ed7fceced5ce476a8ee5a316e0 Mon Sep 17 00:00:00 2001 From: chenbingbing Date: Fri, 27 Jun 2025 19:46:19 +0800 Subject: [PATCH 1/2] fix ffrt post task crash Signed-off-by: chenbingbing --- services/samgr/native/source/ffrt_handler.cpp | 12 +++++ .../src/device_timed_collect_test.cpp | 45 +++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/services/samgr/native/source/ffrt_handler.cpp b/services/samgr/native/source/ffrt_handler.cpp index 9753dcb3..315abed4 100644 --- a/services/samgr/native/source/ffrt_handler.cpp +++ b/services/samgr/native/source/ffrt_handler.cpp @@ -56,6 +56,10 @@ void FFRTHandler::SetFfrt(const std::string& name) bool FFRTHandler::PostTask(std::function func) { + if (!func) { + HILOGE("FFRTHandler post task failed, func is null"); + return false; + } task_handle handler = queue_->submit_h(func); if (handler == nullptr) { HILOGE("FFRTHandler post task failed"); @@ -66,6 +70,10 @@ bool FFRTHandler::PostTask(std::function func) bool FFRTHandler::PostTask(std::function func, uint64_t delayTime) { + if (!func) { + HILOGE("FFRTHandler post task failed, func is null"); + return false; + } if (delayTime > std::numeric_limits::max() / CONVERSION_FACTOR) { HILOGE("invalid delay time"); return false; @@ -80,6 +88,10 @@ bool FFRTHandler::PostTask(std::function func, uint64_t delayTime) bool FFRTHandler::PostTask(std::function func, const std::string& name, uint64_t delayTime) { + if (!func) { + HILOGE("FFRTHandler post task failed, func is null"); + return false; + } if (delayTime > std::numeric_limits::max() / CONVERSION_FACTOR) { HILOGE("invalid delay time"); return false; diff --git a/services/samgr/native/test/unittest/src/device_timed_collect_test.cpp b/services/samgr/native/test/unittest/src/device_timed_collect_test.cpp index dee78a4d..cd383149 100644 --- a/services/samgr/native/test/unittest/src/device_timed_collect_test.cpp +++ b/services/samgr/native/test/unittest/src/device_timed_collect_test.cpp @@ -938,4 +938,49 @@ HWTEST_F(DeviceTimedCollectTest, RemovePersistenceLoopTask001, TestSize.Level3) EXPECT_TRUE(deviceTimedCollect->persitenceLoopEventSet_.empty()); DTEST_LOG << " RemovePersistenceLoopTask001 end" << std::endl; } + +/** + * @tc.name: TestFFRTHandlerPostTask001 + * @tc.desc: test FFRTHandler PostTask, with func is null + * @tc.type: FUNC + * @tc.require: I7VZ98 + */ +HWTEST_F(DeviceTimedCollectTest, TestFFRTHandlerPostTask001, TestSize.Level3) +{ + DTEST_LOG << " TestFFRTHandlerPostTask001 begin" << std::endl; + std::shared_ptr collectHandler = std::make_shared("collect"); + std::function func = nullptr; + EXPECT_FALSE(collectHandler->PostTask(func)); + DTEST_LOG << " TestFFRTHandlerPostTask001 end" << std::endl; +} + +/** + * @tc.name: TestFFRTHandlerPostTask002 + * @tc.desc: test FFRTHandler PostTask, with func is null + * @tc.type: FUNC + * @tc.require: I7VZ98 + */ +HWTEST_F(DeviceTimedCollectTest, TestFFRTHandlerPostTask002, TestSize.Level3) +{ + DTEST_LOG << " TestFFRTHandlerPostTask002 begin" << std::endl; + std::shared_ptr collectHandler = std::make_shared("collect"); + std::function func = nullptr; + EXPECT_FALSE(collectHandler->PostTask(func, 1)); + DTEST_LOG << " TestFFRTHandlerPostTask002 end" << std::endl; +} + +/** + * @tc.name: TestFFRTHandlerPostTask003 + * @tc.desc: test FFRTHandler PostTask, with func is null + * @tc.type: FUNC + * @tc.require: I7VZ98 + */ +HWTEST_F(DeviceTimedCollectTest, TestFFRTHandlerPostTask003, TestSize.Level3) +{ + DTEST_LOG << " TestFFRTHandlerPostTask003 begin" << std::endl; + std::shared_ptr collectHandler = std::make_shared("collect"); + std::function func = nullptr; + EXPECT_FALSE(collectHandler->PostTask(func, "test", 1)); + DTEST_LOG << " TestFFRTHandlerPostTask003 end" << std::endl; +} } \ No newline at end of file -- Gitee From ddac58638e9e77c3acdc2874228815c0257733b6 Mon Sep 17 00:00:00 2001 From: chenbingbing Date: Fri, 27 Jun 2025 12:15:46 +0000 Subject: [PATCH 2/2] update services/samgr/native/source/ffrt_handler.cpp. Signed-off-by: chenbingbing --- services/samgr/native/source/ffrt_handler.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/samgr/native/source/ffrt_handler.cpp b/services/samgr/native/source/ffrt_handler.cpp index 315abed4..e08110b8 100644 --- a/services/samgr/native/source/ffrt_handler.cpp +++ b/services/samgr/native/source/ffrt_handler.cpp @@ -71,7 +71,7 @@ bool FFRTHandler::PostTask(std::function func) bool FFRTHandler::PostTask(std::function func, uint64_t delayTime) { if (!func) { - HILOGE("FFRTHandler post task failed, func is null"); + HILOGE("FFRTHandler post delay task failed, func is null"); return false; } if (delayTime > std::numeric_limits::max() / CONVERSION_FACTOR) { @@ -89,7 +89,7 @@ bool FFRTHandler::PostTask(std::function func, uint64_t delayTime) bool FFRTHandler::PostTask(std::function func, const std::string& name, uint64_t delayTime) { if (!func) { - HILOGE("FFRTHandler post task failed, func is null"); + HILOGE("FFRTHandler post delay task with name failed, func is null"); return false; } if (delayTime > std::numeric_limits::max() / CONVERSION_FACTOR) { -- Gitee