From 42bc6ac29c1bd32f9dde129ccc9589ac8db1dc81 Mon Sep 17 00:00:00 2001 From: Jin1K <1061208093@qq.com> Date: Mon, 26 May 2025 17:06:28 +0800 Subject: [PATCH] add nothrow Signed-off-by: Jin1K <1061208093@qq.com> Change-Id: I343c0b6dff0219a758015273add19ca82b48fb45 --- .../data_share/common/bundle_mgr_proxy.cpp | 2 +- .../data_share/common/bundle_mgr_proxy.h | 8 ++++---- .../service/data_share/common/common_utils.h | 6 ++++++ .../service/data_share/common/rdb_delegate.cpp | 17 +++++++++-------- .../data_share/data_share_service_impl.cpp | 12 ++++++++++-- 5 files changed, 30 insertions(+), 15 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp b/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp index 4e5e29b04..360724f83 100644 --- a/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp +++ b/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp @@ -197,7 +197,7 @@ void BundleMgrProxy::Delete(const std::string &bundleName, int32_t userId, int32 std::shared_ptr BundleMgrProxy::GetInstance() { - static std::shared_ptr proxy(new BundleMgrProxy()); + static std::shared_ptr proxy = std::make_shared(); return proxy; } diff --git a/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.h b/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.h index c0c3d774e..bc432d62c 100644 --- a/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.h +++ b/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.h @@ -57,24 +57,24 @@ struct ExtensionAbilityInfo { struct BundleConfig { std::string name; bool singleton = false; - bool isSystemApp = false; + bool isSystemApp = false; std::vector hapModuleInfos; std::vector extensionInfos; }; class BundleMgrProxy final : public std::enable_shared_from_this { public: + BundleMgrProxy() = default; ~BundleMgrProxy(); static std::shared_ptr GetInstance(); int GetBundleInfoFromBMS(const std::string &bundleName, int32_t userId, BundleConfig &bundleConfig, int32_t appIndex = 0); - int GetBundleInfoFromBMSWithCheck(const std::string &bundleName, int32_t userId, - BundleConfig &bundleConfig, int32_t appIndex = 0); + int GetBundleInfoFromBMSWithCheck(const std::string &bundleName, int32_t userId, + BundleConfig &bundleConfig, int32_t appIndex = 0); void Delete(const std::string &bundleName, int32_t userId, int32_t appIndex); sptr CheckBMS(); std::pair GetCallerAppIdentifier(const std::string &bundleName, int32_t userId); private: - BundleMgrProxy() = default; class ServiceDeathRecipient : public IRemoteObject::DeathRecipient { public: explicit ServiceDeathRecipient(std::weak_ptr owner) : owner_(owner) {} diff --git a/services/distributeddataservice/service/data_share/common/common_utils.h b/services/distributeddataservice/service/data_share/common/common_utils.h index 1b8379dc7..a526fbf62 100644 --- a/services/distributeddataservice/service/data_share/common/common_utils.h +++ b/services/distributeddataservice/service/data_share/common/common_utils.h @@ -30,5 +30,11 @@ bool CheckSystemAbility(uint32_t tokenId); bool CheckSystemCallingPermission(uint32_t tokenId, uint64_t fullTokenId); +inline int64_t GetSystemTime() +{ + return std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()).count(); +} + } // namespace OHOS::DataShare #endif // DATASHARESERVICE_COMMON_UTILS_H diff --git a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp index de1036ff2..f096e975b 100644 --- a/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/rdb_delegate.cpp @@ -35,6 +35,7 @@ #include "db_delegate.h" #include "log_debug.h" #include "ipc_skeleton.h" +#include "common_utils.h" namespace OHOS::DataShare { constexpr static int32_t MAX_RESULTSET_COUNT = 32; @@ -216,7 +217,6 @@ std::pair> RdbDelegate::Query(const std return std::make_pair(errCode_, nullptr); } int count = resultSetCount.fetch_add(1); - ZLOGD_MACRO("start query %{public}d", count); if (count >= MAX_RESULTSET_COUNT && IsLimit(count, callingPid, callingTokenId)) { resultSetCount--; return std::make_pair(E_RESULTSET_BUSY, nullptr); @@ -240,14 +240,15 @@ std::pair> RdbDelegate::Query(const std ++value; return true; }); - int64_t beginTime = std::chrono::duration_cast( - std::chrono::system_clock::now().time_since_epoch()).count(); + int64_t beginTime = GetSystemTime(); auto bridge = RdbDataShareAdapter::RdbUtils::ToResultSetBridge(resultSet); - std::shared_ptr result = { new DataShareResultSet(bridge), [callingPid, beginTime](auto p) { - ZLOGD_MACRO("release resultset"); + auto resultSetPtr = new (std::nothrow) DataShareResultSet(bridge); + if (resultSetPtr == nullptr) { + return std::make_pair(E_ERROR, nullptr); + } + auto result = std::shared_ptr(resultSetPtr, [callingPid, beginTime](auto p) { resultSetCount--; - int64_t endTime = std::chrono::duration_cast( - std::chrono::system_clock::now().time_since_epoch()).count(); + int64_t endTime = GetSystemTime(); if (endTime - beginTime > TIMEOUT_TIME) { ZLOGE("pid %{public}d query time is %{public}" PRId64 ", %{public}d resultSet is used.", callingPid, (endTime - beginTime), resultSetCount.load()); @@ -257,7 +258,7 @@ std::pair> RdbDelegate::Query(const std return value > 0; }); delete p; - }}; + }); return std::make_pair(E_OK, result); } diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp index 99bff919b..b914b7954 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -613,7 +613,11 @@ void DataShareServiceImpl::SubscribeCommonEvent() ZLOGE("System mgr is nullptr"); return; } - sptr callback(new SystemAbilityStatusChangeListener()); + sptr callback(new (std::nothrow)SystemAbilityStatusChangeListener()); + if (callback == nullptr) { + ZLOGE("new SystemAbilityStatusChangeListener failed! no memory"); + return; + } systemManager->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, callback); } @@ -624,7 +628,11 @@ void DataShareServiceImpl::SubscribeConcurrentTask() ZLOGE("System mgr is nullptr"); return; } - sptr callback(new SystemAbilityStatusChangeListener()); + sptr callback(new (std::nothrow)SystemAbilityStatusChangeListener()); + if (callback == nullptr) { + ZLOGE("new SystemAbilityStatusChangeListener failed! no memory"); + return; + } systemManager->SubscribeSystemAbility(CONCURRENT_TASK_SERVICE_ID, callback); } -- Gitee