From 845899893073248391ab0c974956c4b686085f4a Mon Sep 17 00:00:00 2001 From: jiangminsen Date: Wed, 25 Jun 2025 10:27:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E5=86=85=E5=AD=98=E6=B3=84?= =?UTF-8?q?=E9=9C=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: jiangminsen --- services/dbms/include/dbms_scope_guard.h | 46 ++++++++++++++++++++++ services/dbms/src/distributed_bms_host.cpp | 20 ++++++++-- 2 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 services/dbms/include/dbms_scope_guard.h diff --git a/services/dbms/include/dbms_scope_guard.h b/services/dbms/include/dbms_scope_guard.h new file mode 100644 index 0000000..d9551e5 --- /dev/null +++ b/services/dbms/include/dbms_scope_guard.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_DBMS_SCOPE_GUARD +#define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_DBMS_SCOPE_GUARD + +#include + +namespace OHOS { +namespace AppExecFwk { +class DbmsScopeGuard final { +public: + using Function = std::function; + explicit DbmsScopeGuard(Function fn) : fn_(fn), dismissed_(false) + {} + + ~DbmsScopeGuard() + { + if (!dismissed_) { + fn_(); + } + } + + void Dismiss() + { + dismissed_ = true; + } + +private: + Function fn_; + bool dismissed_; +}; +} +} +#endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_DBMS_SCOPE_GUARD \ No newline at end of file diff --git a/services/dbms/src/distributed_bms_host.cpp b/services/dbms/src/distributed_bms_host.cpp index b4197ac..dada2a7 100644 --- a/services/dbms/src/distributed_bms_host.cpp +++ b/services/dbms/src/distributed_bms_host.cpp @@ -19,6 +19,7 @@ #include "appexecfwk_errors.h" #include "bundle_constants.h" #include "bundle_memory_guard.h" +#include "dbms_scope_guard.h" #include "distributed_bundle_ipc_interface_code.h" #include "remote_ability_info.h" @@ -137,13 +138,18 @@ int DistributedBmsHost::HandleGetAbilityInfo(Parcel &data, Parcel &reply) std::string localeInfo = data.ReadString(); bool hasInfo = data.ReadBool(); DistributedBmsAclInfo *info = nullptr; + DbmsScopeGuard deletePtrGuard([&info] { + if (info != nullptr) { + delete info; + info = nullptr; + } + }); if (hasInfo) { - std::unique_ptr readInfo(data.ReadParcelable()); - if (readInfo == nullptr) { - APP_LOGE("HandleGetAbilityInfo get parcelable info failed"); + info = data.ReadParcelable(); + if (info == nullptr) { + APP_LOGE("HandleGetAbilityInfos get parcelable info failed"); return ERR_APPEXECFWK_PARCEL_ERROR; } - info = readInfo.get(); } RemoteAbilityInfo remoteAbilityInfo; int ret = GetAbilityInfo(*elementName, localeInfo, remoteAbilityInfo, info); @@ -173,6 +179,12 @@ int DistributedBmsHost::HandleGetAbilityInfos(Parcel &data, Parcel &reply) std::string localeInfo = data.ReadString(); bool hasInfo = data.ReadBool(); DistributedBmsAclInfo *info = nullptr; + DbmsScopeGuard deletePtrGuard([&info] { + if (info != nullptr) { + delete info; + info = nullptr; + } + }); if (hasInfo) { info = data.ReadParcelable(); if (info == nullptr) { -- Gitee