From c782004471d7e0591455e5cbdd5d45f8b52d4bd4 Mon Sep 17 00:00:00 2001 From: hanlin15 Date: Tue, 19 Nov 2024 16:17:09 +0800 Subject: [PATCH] fix:avoid using default capture mode for lambda expressions Signed-off-by: hanlin15 Change-Id: Ie0f239e13f3e00b0c2ff094f058b43aa20b25cd2 --- .../source/napi_remote_proxy_holder.cpp | 2 +- .../common/invoker_factory_unittest.cpp | 9 ++++++--- ipc/test/auxiliary/native/src/foo_service.cpp | 4 ++-- .../native/src/test_capi_skeleton.cpp | 8 ++++---- .../native/src/test_service_client.cpp | 18 +++++++++--------- .../dbinder_service/src/dbinder_service.cpp | 9 ++++++++- .../src/dbinder_distributed_test.cpp | 3 ++- 7 files changed, 32 insertions(+), 21 deletions(-) diff --git a/ipc/native/src/napi_common/source/napi_remote_proxy_holder.cpp b/ipc/native/src/napi_common/source/napi_remote_proxy_holder.cpp index 50bb506d..e61bfaaa 100644 --- a/ipc/native/src/napi_common/source/napi_remote_proxy_holder.cpp +++ b/ipc/native/src/napi_common/source/napi_remote_proxy_holder.cpp @@ -42,7 +42,7 @@ void NAPIDeathRecipient::AfterWorkCallback(uv_work_t *work, int status) napi_handle_scope scope = nullptr; napi_open_handle_scope(param->env, &scope); - auto CleanUp = [&]() { + auto CleanUp = [¶m, &scope, &work]() { napi_close_handle_scope(param->env, scope); delete param; delete work; diff --git a/ipc/native/test/unittest/common/invoker_factory_unittest.cpp b/ipc/native/test/unittest/common/invoker_factory_unittest.cpp index 173006bd..0dc21d2e 100644 --- a/ipc/native/test/unittest/common/invoker_factory_unittest.cpp +++ b/ipc/native/test/unittest/common/invoker_factory_unittest.cpp @@ -52,7 +52,7 @@ HWTEST_F(InvokerFactoryTest, Register001, TestSize.Level1) int protocol = 1; IRemoteInvoker* invoker = nullptr; - auto invokerObject = [&invoker]() -> IRemoteInvoker* { + auto creator = [&invoker]() -> IRemoteInvoker* { invoker = new (std::nothrow) BinderInvoker(); if (invoker == nullptr) { return nullptr; @@ -60,7 +60,7 @@ HWTEST_F(InvokerFactoryTest, Register001, TestSize.Level1) return invoker; }; - bool ret = invokerFactory.Register(protocol, invokerObject); + bool ret = invokerFactory.Register(protocol, creator); EXPECT_EQ(ret, false); if (invoker != nullptr) { delete invoker; @@ -68,7 +68,7 @@ HWTEST_F(InvokerFactoryTest, Register001, TestSize.Level1) } invokerFactory.isAvailable_ = true; - ret = invokerFactory.Register(protocol, invokerObject); + ret = invokerFactory.Register(protocol, creator); EXPECT_EQ(ret, false); IRemoteInvoker* iRemoteInvoker = invokerFactory.newInstance(protocol); EXPECT_NE(iRemoteInvoker, nullptr); @@ -76,6 +76,9 @@ HWTEST_F(InvokerFactoryTest, Register001, TestSize.Level1) delete invoker; invoker = nullptr; } + // after leaving the scope, the captured 'invoker' object will be invalid in 'creator' lambda expression + // so we need to delete 'creator' lambda expression + invokerFactory.Unregister(protocol); } /** diff --git a/ipc/test/auxiliary/native/src/foo_service.cpp b/ipc/test/auxiliary/native/src/foo_service.cpp index 4ba350d7..1934c212 100644 --- a/ipc/test/auxiliary/native/src/foo_service.cpp +++ b/ipc/test/auxiliary/native/src/foo_service.cpp @@ -56,8 +56,8 @@ int FooStub::WaitForAsyncReply(int timeout) { asyncReply_ = 0; std::unique_lock lck(mutex_); - cv_.wait_for(lck, std::chrono::milliseconds(timeout), [&]() { - return asyncReply_ != 0; + cv_.wait_for(lck, std::chrono::milliseconds(timeout), [this]() { + return this->asyncReply_ != 0; }); return asyncReply_; } diff --git a/ipc/test/auxiliary/native/src/test_capi_skeleton.cpp b/ipc/test/auxiliary/native/src/test_capi_skeleton.cpp index 70bbef03..76a3f592 100644 --- a/ipc/test/auxiliary/native/src/test_capi_skeleton.cpp +++ b/ipc/test/auxiliary/native/src/test_capi_skeleton.cpp @@ -211,8 +211,8 @@ int NativeRemoteProxyTest::WaitForAsyncReply(int timeout) { asyncReply_ = 0; std::unique_lock lck(mutex_); - cv_.wait_for(lck, std::chrono::seconds(timeout), [&]() { - return asyncReply_ != 0; + cv_.wait_for(lck, std::chrono::seconds(timeout), [this]() { + return this->asyncReply_ != 0; }); return asyncReply_; } @@ -478,7 +478,7 @@ int NativeRemoteProxyTest::SendErrorCode() if (proxy_ == nullptr) { return -1; } - auto func = [&, this](int val, int expect) -> int { + auto func = [proxy = this->proxy_](int val, int expect) -> int { OHIPCParcel *data = OH_IPCParcel_Create(); if (data == nullptr) { return -1; @@ -495,7 +495,7 @@ int NativeRemoteProxyTest::SendErrorCode() return -1; } OH_IPCParcel_WriteInt32(data, val); - int ret = OH_IPCRemoteProxy_SendRequest(this->proxy_, NATIVE_TEST_CMD_SEND_ERROR_CODE, data, reply, &option); + int ret = OH_IPCRemoteProxy_SendRequest(proxy, NATIVE_TEST_CMD_SEND_ERROR_CODE, data, reply, &option); OH_IPCParcel_Destroy(data); OH_IPCParcel_Destroy(reply); return (ret == expect) ? 0 : -1; diff --git a/ipc/test/auxiliary/native/src/test_service_client.cpp b/ipc/test/auxiliary/native/src/test_service_client.cpp index 07830ca7..7d4d4da2 100644 --- a/ipc/test/auxiliary/native/src/test_service_client.cpp +++ b/ipc/test/auxiliary/native/src/test_service_client.cpp @@ -196,15 +196,15 @@ bool TestServiceClient::TestNativeIPCSendRequests(int subCmd) } static std::map> commandMap = { - { NATIVE_TEST_CMD_SYNC_ADD, [&]() { return remoteProxy->SyncAdd(); }}, - { NATIVE_TEST_CMD_ASYNC_ADD, [&]() { return remoteProxy->ASyncAdd(); }}, - { NATIVE_TEST_CMD_SYNC_ADD_REPEAT, [&]() { return remoteProxy->AddParallel(true); }}, - { NATIVE_TEST_CMD_ASYNC_ADD_REPEAT, [&]() { return remoteProxy->AddParallel(false); }}, - { NATIVE_TEST_CMD_SEND_AND_ECHO_BASE, [&]() { return remoteProxy->SendAndEchoBase(); }}, - { NATIVE_TEST_CMD_SEND_AND_ECHO_SRING, [&]() { return remoteProxy->SendAndEchoString(); }}, - { NATIVE_TEST_CMD_SEND_AND_ECHO_BUFFER, [&]() { return remoteProxy->SendAndEchoBuffer(); }}, - { NATIVE_TEST_CMD_SEND_FILE_DESCRIPTOR, [&]() { return remoteProxy->SendAndEchoFileDescriptor(); }}, - { NATIVE_TEST_CMD_SEND_ERROR_CODE, [&]() { return remoteProxy->SendErrorCode(); }}, + { NATIVE_TEST_CMD_SYNC_ADD, [remoteProxy]() { return remoteProxy->SyncAdd(); }}, + { NATIVE_TEST_CMD_ASYNC_ADD, [remoteProxy]() { return remoteProxy->ASyncAdd(); }}, + { NATIVE_TEST_CMD_SYNC_ADD_REPEAT, [remoteProxy]() { return remoteProxy->AddParallel(true); }}, + { NATIVE_TEST_CMD_ASYNC_ADD_REPEAT, [remoteProxy]() { return remoteProxy->AddParallel(false); }}, + { NATIVE_TEST_CMD_SEND_AND_ECHO_BASE, [remoteProxy]() { return remoteProxy->SendAndEchoBase(); }}, + { NATIVE_TEST_CMD_SEND_AND_ECHO_SRING, [remoteProxy]() { return remoteProxy->SendAndEchoString(); }}, + { NATIVE_TEST_CMD_SEND_AND_ECHO_BUFFER, [remoteProxy]() { return remoteProxy->SendAndEchoBuffer(); }}, + { NATIVE_TEST_CMD_SEND_FILE_DESCRIPTOR, [remoteProxy]() { return remoteProxy->SendAndEchoFileDescriptor(); }}, + { NATIVE_TEST_CMD_SEND_ERROR_CODE, [remoteProxy]() { return remoteProxy->SendErrorCode(); }}, }; auto it = commandMap.find(subCmd); if (it == commandMap.end()) { diff --git a/services/dbinder/dbinder_service/src/dbinder_service.cpp b/services/dbinder/dbinder_service/src/dbinder_service.cpp index d08986a2..377766bb 100644 --- a/services/dbinder/dbinder_service/src/dbinder_service.cpp +++ b/services/dbinder/dbinder_service/src/dbinder_service.cpp @@ -857,7 +857,14 @@ bool DBinderService::RegisterRemoteProxyInner(std::u16string serviceName, binder void DBinderService::AddAsynMessageTask(std::shared_ptr message) { - auto task = [this, message] { this->OnRemoteMessageTask(message); }; + sptr servicePtr = this; + auto task = [servicePtr, message] { + if (servicePtr == nullptr) { + DBINDER_LOGE(LOG_LABEL, "invalid dbinder service object"); + return; + } + servicePtr->OnRemoteMessageTask(message); + }; ffrt::submit(task); } diff --git a/services/dbinder/test/distributedtest/src/dbinder_distributed_test.cpp b/services/dbinder/test/distributedtest/src/dbinder_distributed_test.cpp index 019b0696..ce4e933c 100644 --- a/services/dbinder/test/distributedtest/src/dbinder_distributed_test.cpp +++ b/services/dbinder/test/distributedtest/src/dbinder_distributed_test.cpp @@ -109,7 +109,8 @@ void DbinderTest::SetUp() bool DbinderTest::GetRemoteDeviceId() { std::string msg = "Ask Device ID"; - int ret = SendMessage(AGENT_NO::ONE, msg, strlen(msg.c_str()), [&](const std::string &retId, int retLen) -> bool { + int ret = SendMessage(AGENT_NO::ONE, msg, + strlen(msg.c_str()), [serverId_](const std::string &retId, int retLen) -> bool { if (memcpy_s(serverId_, DEVICEID_LENGTH, retId.c_str(), DEVICEID_LENGTH) != 0 || retLen != DEVICEID_LENGTH) { DBINDER_LOGE(LOG_LABEL, "fail to copy string"); return false;