From db2ff4a64b8fe03c0c95cf79f30644694438ecd9 Mon Sep 17 00:00:00 2001 From: 18242988924 Date: Sat, 12 Aug 2023 07:22:49 +0000 Subject: [PATCH 1/2] fix memory leak Signed-off-by: 18242988924 Change-Id: I07727f2c71e3a5d955509a1a0eb35db9ad8859f7 --- services/bundlemgr_lite/src/gt_bundle_manager_service.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/services/bundlemgr_lite/src/gt_bundle_manager_service.cpp b/services/bundlemgr_lite/src/gt_bundle_manager_service.cpp index 496ed05..2caaa76 100644 --- a/services/bundlemgr_lite/src/gt_bundle_manager_service.cpp +++ b/services/bundlemgr_lite/src/gt_bundle_manager_service.cpp @@ -1194,6 +1194,7 @@ int32_t GtManagerService::ReportInstallCallback( bundleInstallMsg->smallIconPath = bundleInstallMsg_->smallIconPath; bundleInstallMsg->bigIconPath = bundleInstallMsg_->bigIconPath; (*installerCallback)(errCode, bundleInstallMsg); + AdapterFree(bundleInstallMsg); return 0; } @@ -1211,6 +1212,7 @@ int32_t GtManagerService::ReportUninstallCallback(uint8_t errCode, uint8_t insta bundleInstallMsg->bundleName = bundleName; bundleInstallMsg->installProcess = process; (*installerCallback)(errCode, bundleInstallMsg); + AdapterFree(bundleInstallMsg); return 0; } -- Gitee From 14e045a60e6fee980fca9b7098f2ae5f73dfc4c7 Mon Sep 17 00:00:00 2001 From: 18242988924 Date: Tue, 5 Sep 2023 03:50:17 +0000 Subject: [PATCH 2/2] =?UTF-8?q?new=E5=AF=B9=E8=B1=A1=E5=8A=A0nothrow?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 18242988924 Change-Id: I1583a0d82015766e6078c1cebef57899dc0d1f8a --- .../bundle_daemon/src/bundle_daemon.cpp | 5 ++++- .../bundlemgr_lite/src/bundle_manager_service.cpp | 4 ++-- services/bundlemgr_lite/src/bundle_map.cpp | 4 ++-- .../src/gt_bundle_manager_service.cpp | 14 ++++++-------- services/bundlemgr_lite/src/zip_file.cpp | 4 ++-- utils/bundle_lite/utils_list.h | 6 +++--- 6 files changed, 19 insertions(+), 18 deletions(-) diff --git a/services/bundlemgr_lite/bundle_daemon/src/bundle_daemon.cpp b/services/bundlemgr_lite/bundle_daemon/src/bundle_daemon.cpp index 9e36b5b..72535db 100644 --- a/services/bundlemgr_lite/bundle_daemon/src/bundle_daemon.cpp +++ b/services/bundlemgr_lite/bundle_daemon/src/bundle_daemon.cpp @@ -142,7 +142,10 @@ int32_t BundleDaemon::RegisterCallbackInvoke(IpcIo *req) if (!(ReadRemoteObject(req, &svcIdentity))) { return EC_INVALID; } - BundleDaemon::GetInstance().bundleMsClient_ = new BundleMsClient(svcIdentity); + BundleDaemon::GetInstance().bundleMsClient_ = new (std::nothrow) BundleMsClient(svcIdentity); + if (BundleDaemon::GetInstance().bundleMsClient_ == nullptr) { + return EC_BADPTR; + } return BundleDaemon::GetInstance().bundleMsClient_->SendReply(EC_SUCCESS); } diff --git a/services/bundlemgr_lite/src/bundle_manager_service.cpp b/services/bundlemgr_lite/src/bundle_manager_service.cpp index f72805e..12c817c 100644 --- a/services/bundlemgr_lite/src/bundle_manager_service.cpp +++ b/services/bundlemgr_lite/src/bundle_manager_service.cpp @@ -49,7 +49,7 @@ extern "C" { namespace OHOS { ManagerService::ManagerService() { - installer_ = new BundleInstaller(INSTALL_PATH, DATA_PATH); + installer_ = new (std::nothrow) BundleInstaller(INSTALL_PATH, DATA_PATH); bundleMap_ = BundleMap::GetInstance(); } @@ -182,7 +182,7 @@ void ManagerService::ServiceMsgProcess(Request* request) } if (installer_ == nullptr) { - installer_ = new BundleInstaller(INSTALL_PATH, DATA_PATH); + installer_ = new (std::nothrow) BundleInstaller(INSTALL_PATH, DATA_PATH); } switch (request->msgId) { diff --git a/services/bundlemgr_lite/src/bundle_map.cpp b/services/bundlemgr_lite/src/bundle_map.cpp index e20bfe6..2303f6c 100755 --- a/services/bundlemgr_lite/src/bundle_map.cpp +++ b/services/bundlemgr_lite/src/bundle_map.cpp @@ -41,7 +41,7 @@ BundleMap::BundleMap() #else g_bundleListMutex = osMutexNew(reinterpret_cast(NULL)); #endif - bundleInfos_ = new List(); + bundleInfos_ = new (std::nothrow) List(); } BundleMap::~BundleMap() @@ -83,7 +83,7 @@ bool BundleMap::Update(BundleInfo *bundleInfo) #else MutexAcquire(&g_bundleListMutex, BUNDLELIST_MUTEX_TIMEOUT); #endif - auto newNode = new Node(bundleInfo); + auto newNode = new (std::nothrow) Node(bundleInfo); if (newNode == nullptr) { MutexRelease(&g_bundleListMutex); return false; diff --git a/services/bundlemgr_lite/src/gt_bundle_manager_service.cpp b/services/bundlemgr_lite/src/gt_bundle_manager_service.cpp index 5b8d15f..510ef58 100644 --- a/services/bundlemgr_lite/src/gt_bundle_manager_service.cpp +++ b/services/bundlemgr_lite/src/gt_bundle_manager_service.cpp @@ -47,8 +47,8 @@ const uint8_t BMS_INSTALLATION_COMPLETED = 100; GtManagerService::GtManagerService() { - installer_ = new GtBundleInstaller(); - bundleResList_ = new List(); + installer_ = new (std::nothrow) GtBundleInstaller(); + bundleResList_ = new (std::nothrow) List(); bundleMap_ = BundleMap::GetInstance(); bundleInstallMsg_ = nullptr; jsEngineVer_ = nullptr; @@ -56,7 +56,7 @@ GtManagerService::GtManagerService() preAppList_ = nullptr; updateFlag_ = false; oldVersionCode_ = -1; - listenList_ = new List(); + listenList_ = new (std::nothrow) List(); } GtManagerService::~GtManagerService() @@ -73,7 +73,7 @@ bool GtManagerService::Install(const char *hapPath, const InstallParam *installP InstallerCallback installerCallback) { if (installer_ == nullptr) { - installer_ = new GtBundleInstaller(); + installer_ = new (std::nothrow) GtBundleInstaller(); } if (hapPath == nullptr) { return false; @@ -170,7 +170,7 @@ bool GtManagerService::Uninstall(const char *bundleName, const InstallParam *ins InstallerCallback installerCallback) { if (installer_ == nullptr) { - installer_ = new GtBundleInstaller(); + installer_ = new (std::nothrow) GtBundleInstaller(); } if (bundleName == nullptr) { HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] Parsed bundleName to be uninstalled is null"); @@ -298,7 +298,7 @@ uint8_t GtManagerService::QueryAbilityInfos(const Want *want, AbilityInfo **abil return 0; } int32_t abilityInfoLen = 0; - List *bundleInfos_ = new List(); + List *bundleInfos_ = new (std::nothrow) List(); bundleMap_->GetBundleInfosInner(*bundleInfos_); for (auto node = bundleInfos_->Begin(); node != bundleInfos_->End(); node = node->next_) { @@ -1197,7 +1197,6 @@ int32_t GtManagerService::ReportInstallCallback( bundleInstallMsg->smallIconPath = bundleInstallMsg_->smallIconPath; bundleInstallMsg->bigIconPath = bundleInstallMsg_->bigIconPath; (*installerCallback)(errCode, bundleInstallMsg); - AdapterFree(bundleInstallMsg); return 0; } @@ -1215,7 +1214,6 @@ int32_t GtManagerService::ReportUninstallCallback(uint8_t errCode, uint8_t insta bundleInstallMsg->bundleName = bundleName; bundleInstallMsg->installProcess = process; (*installerCallback)(errCode, bundleInstallMsg); - AdapterFree(bundleInstallMsg); return 0; } diff --git a/services/bundlemgr_lite/src/zip_file.cpp b/services/bundlemgr_lite/src/zip_file.cpp index c680ba5..c6b3184 100755 --- a/services/bundlemgr_lite/src/zip_file.cpp +++ b/services/bundlemgr_lite/src/zip_file.cpp @@ -437,13 +437,13 @@ bool ZipFile::InitZStream(z_stream &zstream) const return false; } - BytePtr bufOut = new Byte[UNZIP_BUF_OUT_LEN]; + BytePtr bufOut = new (std::nothrow) Byte[UNZIP_BUF_OUT_LEN]; if (bufOut == nullptr) { HILOG_ERROR(HILOG_MODULE_APP, "unzip inflated new out buffer failed"); return false; } - BytePtr bufIn = new Byte[UNZIP_BUF_IN_LEN]; + BytePtr bufIn = new (std::nothrow) Byte[UNZIP_BUF_IN_LEN]; if (bufIn == nullptr) { HILOG_ERROR(HILOG_MODULE_APP, "unzip inflated new in buffer failed"); delete[] bufOut; diff --git a/utils/bundle_lite/utils_list.h b/utils/bundle_lite/utils_list.h index 60c64e8..2f47291 100644 --- a/utils/bundle_lite/utils_list.h +++ b/utils/bundle_lite/utils_list.h @@ -32,7 +32,7 @@ class List { public: List() : count_(0) { - head_ = new Node(); + head_ = new (std::nothrow) Node(); head_->next_ = head_; head_->prev_ = head_; } @@ -55,7 +55,7 @@ public: void PushFront(T value) { - auto node = new Node(value); + auto node = new (std::nothrow) Node(value); if (node == nullptr) { return; } @@ -91,7 +91,7 @@ public: void PushBack(T value) { - auto node = new Node(value); + auto node = new (std::nothrow) Node(value); node->next_ = head_; node->prev_ = head_->prev_; -- Gitee