From 3ca27faac72093480f9fc1e2fa1818239461abd0 Mon Sep 17 00:00:00 2001 From: wujianlin Date: Fri, 21 Jun 2024 16:06:09 +0800 Subject: [PATCH] Rectification of basic library CFI Issue:https://gitee.com/openharmony/commonlibrary_c_utils/issues/IA7A2V?from=project-issue Signed-off-by: wujianlin --- base/src/refbase.cpp | 6 +++--- base/src/thread_pool.cpp | 2 +- base/src/timer.cpp | 7 +++---- base/src/timer_event_handler.cpp | 2 +- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/base/src/refbase.cpp b/base/src/refbase.cpp index dff86ab..d7e007a 100644 --- a/base/src/refbase.cpp +++ b/base/src/refbase.cpp @@ -395,7 +395,7 @@ bool RefCounter::AttemptIncStrong(const void *objectId) RefBase::RefBase() : refs_(new RefCounter()) { refs_->IncRefCount(); - refs_->SetCallback(std::bind(&RefBase::RefPtrCallback, this)); + refs_->SetCallback([this] { this->RefPtrCallback(); }); } RefBase::RefBase(const RefBase &) @@ -403,7 +403,7 @@ RefBase::RefBase(const RefBase &) refs_ = new (std::nothrow) RefCounter(); if (refs_ != nullptr) { refs_->IncRefCount(); - refs_->SetCallback(std::bind(&RefBase::RefPtrCallback, this)); + refs_->SetCallback([this] { this->RefPtrCallback(); }); } } @@ -428,7 +428,7 @@ RefBase &RefBase::operator=(const RefBase &) refs_ = new (std::nothrow) RefCounter(); if (refs_ != nullptr) { refs_->IncRefCount(); - refs_->SetCallback(std::bind(&RefBase::RefPtrCallback, this)); + refs_->SetCallback([this] { this->RefPtrCallback(); }); } return *this; diff --git a/base/src/thread_pool.cpp b/base/src/thread_pool.cpp index 2a2c211..8cfa83f 100644 --- a/base/src/thread_pool.cpp +++ b/base/src/thread_pool.cpp @@ -47,7 +47,7 @@ uint32_t ThreadPool::Start(int numThreads) threads_.reserve(numThreads); for (int i = 0; i < numThreads; ++i) { - std::thread t(&ThreadPool::WorkInThread, this); + std::thread t([this] { this->WorkInThread(); }); // Give the name of ThreadPool to threads created by the ThreadPool. int err = pthread_setname_np(t.native_handle(), (myName_ + std::to_string(i)).c_str()); if (err != 0) { diff --git a/base/src/timer.cpp b/base/src/timer.cpp index 4d80e67..e921a41 100644 --- a/base/src/timer.cpp +++ b/base/src/timer.cpp @@ -41,7 +41,7 @@ uint32_t Timer::Setup() return TIMER_ERR_INVALID_VALUE; } reactor_->SwitchOn(); - std::thread loop_thread(std::bind(&Timer::MainLoop, this)); + std::thread loop_thread([this] { this->MainLoop(); }); thread_.swap(loop_thread); return TIMER_ERR_OK; @@ -85,7 +85,7 @@ uint32_t Timer::Register(const TimerCallback& callback, uint32_t interval /* ms static std::atomic_uint32_t timerId = 1; int timerFd = once ? INVALID_TIMER_FD : GetTimerFd(interval); if (timerFd == INVALID_TIMER_FD) { - uint32_t ret = DoRegister(std::bind(&Timer::OnTimer, this, std::placeholders::_1), interval, once, timerFd); + uint32_t ret = DoRegister([this](int fd) { this->OnTimer(fd); }, interval, once, timerFd); if (ret != TIMER_ERR_OK) { UTILS_LOGE("do register interval timer %{public}d failed, return %{public}u", interval, ret); return TIMER_ERR_DEAL_FAILED; @@ -155,8 +155,7 @@ void Timer::MainLoop() uint32_t Timer::DoRegister(const TimerListCallback& callback, uint32_t interval, bool once, int &timerFd) { - using namespace std::placeholders; - std::function cb = std::bind(&Timer::DoTimerListCallback, this, callback, _1); + std::function cb = [this, callback](int fd) { this->DoTimerListCallback(callback, fd); }; uint32_t ret = reactor_->ScheduleTimer(cb, interval, timerFd, once); if ((ret != TIMER_ERR_OK) || (timerFd < 0)) { UTILS_LOGE("ScheduleTimer failed!ret:%{public}d, timerFd:%{public}d", ret, timerFd); diff --git a/base/src/timer_event_handler.cpp b/base/src/timer_event_handler.cpp index c18f4f4..aa603ed 100644 --- a/base/src/timer_event_handler.cpp +++ b/base/src/timer_event_handler.cpp @@ -80,7 +80,7 @@ uint32_t TimerEventHandler::Initialize() return TIMER_ERR_DEAL_FAILED; } - SetReadCallback(std::bind(&TimerEventHandler::TimeOut, this)); + SetReadCallback([this] { this->TimeOut(); }); EnableRead(); return TIMER_ERR_OK; } -- Gitee