diff --git a/services/dbms/include/dbms_scope_guard.h b/services/dbms/include/dbms_scope_guard.h new file mode 100644 index 0000000000000000000000000000000000000000..d9551e5be42a23b1243492aa4a985d042c5c2164 --- /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 b4197ac2e2e8dd01b4692bf8e01f775147407822..dada2a738662e494893ed3c160f5b5cc4f2b1ac6 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) {