diff --git a/frameworks/ans/src/notification_flags.cpp b/frameworks/ans/src/notification_flags.cpp index 33325e445b522de9a9990654a86c6d4c0a2d0c88..a60259cd81a6ac99e787aa079f3dd0d4ddeb07c1 100644 --- a/frameworks/ans/src/notification_flags.cpp +++ b/frameworks/ans/src/notification_flags.cpp @@ -102,8 +102,12 @@ bool NotificationFlags::Marshalling(Parcel &parcel) const NotificationFlags *NotificationFlags::Unmarshalling(Parcel &parcel) { - auto templ = new NotificationFlags(); - if ((templ != nullptr) && !templ->ReadFromParcel(parcel)) { + auto templ = new (std::nothrow) NotificationFlags(); + if (templ == nullptr) { + ANS_LOGE("Failed to create NotificationFlags instance"); + return nullptr; + } + if (!templ->ReadFromParcel(parcel)) { delete templ; templ = nullptr; } diff --git a/frameworks/ans/src/notification_template.cpp b/frameworks/ans/src/notification_template.cpp index e359a01f2e4a946bfff68397578e45263b6d3b17..98a5fba3e9ef3426567df98d47ce374c53b328e1 100644 --- a/frameworks/ans/src/notification_template.cpp +++ b/frameworks/ans/src/notification_template.cpp @@ -72,8 +72,12 @@ bool NotificationTemplate::Marshalling(Parcel &parcel) const NotificationTemplate *NotificationTemplate::Unmarshalling(Parcel &parcel) { - auto templ = new NotificationTemplate(); - if ((templ != nullptr) && !templ->ReadFromParcel(parcel)) { + auto templ = new (std::nothrow) NotificationTemplate(); + if (templ == nullptr) { + ANS_LOGE("Failed to create NotificationTemplate instance"); + return nullptr; + } + if (!templ->ReadFromParcel(parcel)) { delete templ; templ = nullptr; } diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 7907290cac300907abd425c095c0ffe0e0048771..ad4e64d876d27c79fa65fcce4cebe84065a08047 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -162,7 +162,11 @@ inline ErrCode AssignValidNotificationSlot(const std::shared_ptrbundleOption, slotType, slot); if ((result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) || (result == ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST)) { - slot = new NotificationSlot(slotType); + slot = new (std::nothrow) NotificationSlot(slotType); + if (slot == nullptr) { + ANS_LOGE("Failed to create NotificationSlot instance"); + return ERR_NO_MEMORY; + } std::vector> slots; slots.push_back(slot); result = NotificationPreferences::GetInstance().AddNotificationSlots(record->bundleOption, slots); @@ -267,7 +271,11 @@ sptr AdvancedNotificationService::GetInstance() std::lock_guard lock(instanceMutex_); if (instance_ == nullptr) { - instance_ = new AdvancedNotificationService(); + instance_ = new (std::nothrow) AdvancedNotificationService(); + if (instance_ == nullptr) { + ANS_LOGE("Failed to create AdvancedNotificationService instance"); + return nullptr; + } } return instance_; } @@ -353,7 +361,11 @@ ErrCode AdvancedNotificationService::SetDefaultNotificationEnabled( return ERR_ANS_INVALID_BUNDLE; } sptr bundleData = - new EnabledNotificationCallbackData(bundle->GetBundleName(), bundle->GetUid(), enabled); + new (std::nothrow) EnabledNotificationCallbackData(bundle->GetBundleName(), bundle->GetUid(), enabled); + if (bundleData == nullptr) { + ANS_LOGE("Failed to create EnabledNotificationCallbackData instance"); + return ERR_NO_MEMORY; + } ErrCode result = ERR_OK; result = NotificationPreferences::GetInstance().SetNotificationsEnabledForBundle(bundle, enabled); @@ -379,7 +391,11 @@ sptr AdvancedNotificationService::GenerateBundleOption return nullptr; } int32_t uid = IPCSkeleton::GetCallingUid(); - bundleOption = new NotificationBundleOption(bundle, uid); + bundleOption = new (std::nothrow) NotificationBundleOption(bundle, uid); + if (bundleOption == nullptr) { + ANS_LOGE("Failed to create NotificationBundleOption instance"); + return nullptr; + } return bundleOption; } @@ -397,7 +413,11 @@ sptr AdvancedNotificationService::GenerateValidBundleO } int32_t uid = bundleManager->GetDefaultUidByBundleName(bundleOption->GetBundleName(), activeUserId); if (uid > 0) { - validBundleOption = new NotificationBundleOption(bundleOption->GetBundleName(), uid); + validBundleOption = new (std::nothrow) NotificationBundleOption(bundleOption->GetBundleName(), uid); + if (validBundleOption == nullptr) { + ANS_LOGE("Failed to create NotificationBundleOption instance"); + return nullptr; + } } } } else { @@ -696,7 +716,11 @@ sptr AdvancedNotificationService::GenerateSortingMap() sortingList.push_back(sorting); } - sptr sortingMap = new NotificationSortingMap(sortingList); + sptr sortingMap = new (std::nothrow) NotificationSortingMap(sortingList); + if (sortingMap == nullptr) { + ANS_LOGE("Failed to create NotificationSortingMap instance"); + return nullptr; + } return sortingMap; } @@ -1728,8 +1752,12 @@ ErrCode AdvancedNotificationService::SetNotificationsEnabledForSpecialBundle( return ERR_ANS_INVALID_BUNDLE; } - sptr bundleData = - new EnabledNotificationCallbackData(bundle->GetBundleName(), bundle->GetUid(), enabled); + sptr bundleData = new (std::nothrow) + EnabledNotificationCallbackData(bundle->GetBundleName(), bundle->GetUid(), enabled); + if (bundleData == nullptr) { + ANS_LOGE("Failed to create EnabledNotificationCallbackData instance"); + return ERR_NO_MEMORY; + } ErrCode result = ERR_OK; if (deviceId.empty()) { @@ -1919,9 +1947,10 @@ ErrCode AdvancedNotificationService::PublishContinuousTaskNotification(const spt } sptr bundleOption = nullptr; - bundleOption = new NotificationBundleOption(std::string(), uid); + bundleOption = new (std::nothrow) NotificationBundleOption(std::string(), uid); if (bundleOption == nullptr) { - return ERR_ANS_INVALID_BUNDLE; + ANS_LOGE("Failed to create NotificationBundleOption instance"); + return ERR_NO_MEMORY; } ErrCode result = PrepareContinuousTaskNotificationRequest(request, uid); @@ -1932,10 +1961,12 @@ ErrCode AdvancedNotificationService::PublishContinuousTaskNotification(const spt std::shared_ptr record = std::make_shared(); record->request = request; record->bundleOption = bundleOption; - record->notification = new Notification(request); - if (record->notification != nullptr) { - record->notification->SetSourceType(NotificationConstant::SourceType::TYPE_CONTINUOUS); + record->notification = new (std::nothrow) Notification(request); + if (record->notification == nullptr) { + ANS_LOGE("Failed to create Notification instance"); + return ERR_NO_MEMORY; } + record->notification->SetSourceType(NotificationConstant::SourceType::TYPE_CONTINUOUS); if (notificationSvrQueue_ == nullptr) { ANS_LOGE("Serial queue is invalid."); @@ -2503,7 +2534,12 @@ ErrCode AdvancedNotificationService::AddSlotByType(NotificationConstant::SlotTyp if ((result == ERR_OK) && (slot != nullptr)) { return; } else { - slot = new NotificationSlot(slotType); + slot = new (std::nothrow) NotificationSlot(slotType); + if (slot == nullptr) { + ANS_LOGE("Failed to create NotificationSlot instance"); + return; + } + std::vector> slots; slots.push_back(slot); result = NotificationPreferences::GetInstance().AddNotificationSlots(bundleOption, slots); @@ -3402,7 +3438,11 @@ void AdvancedNotificationService::OnDistributedPublish( return; } record->request = request; - record->notification = new Notification(deviceId, request); + record->notification = new (std::nothrow) Notification(deviceId, request); + if (record->notification == nullptr) { + ANS_LOGE("Failed to create Notification instance"); + return; + } record->bundleOption = bundleOption; record->deviceId = deviceId; record->bundleName = bundleName; @@ -3472,7 +3512,11 @@ void AdvancedNotificationService::OnDistributedUpdate( return; } record->request = request; - record->notification = new Notification(deviceId, request); + record->notification = new (std::nothrow) Notification(deviceId, request); + if (record->notification == nullptr) { + ANS_LOGE("Failed to create Notification instance"); + return; + } record->bundleOption = bundleOption; record->deviceId = deviceId; record->bundleName = bundleName; @@ -3841,11 +3885,15 @@ ErrCode AdvancedNotificationService::SetDoNotDisturbDateByUser(const int32_t &us } ANS_LOGD("Before set SetDoNotDisturbDate beginDate = %{public}" PRId64 ", endDate = %{public}" PRId64, beginDate, endDate); - const sptr newConfig = new NotificationDoNotDisturbDate( + const sptr newConfig = new (std::nothrow) NotificationDoNotDisturbDate( date->GetDoNotDisturbType(), beginDate, endDate ); + if (newConfig == nullptr) { + ANS_LOGE("Failed to create NotificationDoNotDisturbDate instance"); + return ERR_NO_MEMORY; + } sptr bundleOption = GenerateBundleOption(); if (bundleOption == nullptr) { @@ -3887,7 +3935,12 @@ ErrCode AdvancedNotificationService::GetDoNotDisturbDateByUser(const int32_t &us case NotificationConstant::DoNotDisturbType::CLEARLY: case NotificationConstant::DoNotDisturbType::ONCE: if (now >= currentConfig->GetEndDate()) { - date = new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0); + date = new (std::nothrow) NotificationDoNotDisturbDate( + NotificationConstant::DoNotDisturbType::NONE, 0, 0); + if (date == nullptr) { + ANS_LOGE("Failed to create NotificationDoNotDisturbDate instance"); + return; + } NotificationPreferences::GetInstance().SetDoNotDisturbDate(userId, date); } else { date = currentConfig; @@ -4461,7 +4514,7 @@ ErrCode AdvancedNotificationService::PublishNotificationBySa(const sptr bundleOption = new NotificationBundleOption(bundle, uid); + sptr bundleOption = new (std::nothrow) NotificationBundleOption(bundle, uid); if (bundleOption == nullptr) { ANS_LOGE("Failed to create bundleOption"); return ERR_ANS_NO_MEMORY; diff --git a/services/ans/src/bundle_manager_helper.cpp b/services/ans/src/bundle_manager_helper.cpp index 32156d51bcfa545106e5d1785fc8cad4d5b09991..c12f076effac76e625131fffd54531df17f11792 100644 --- a/services/ans/src/bundle_manager_helper.cpp +++ b/services/ans/src/bundle_manager_helper.cpp @@ -27,8 +27,11 @@ namespace OHOS { namespace Notification { BundleManagerHelper::BundleManagerHelper() { - deathRecipient_ = - new RemoteDeathRecipient(std::bind(&BundleManagerHelper::OnRemoteDied, this, std::placeholders::_1)); + deathRecipient_ = new (std::nothrow) + RemoteDeathRecipient(std::bind(&BundleManagerHelper::OnRemoteDied, this, std::placeholders::_1)); + if (deathRecipient_ == nullptr) { + ANS_LOGE("Failed to create RemoteDeathRecipient instance"); + } } BundleManagerHelper::~BundleManagerHelper() diff --git a/services/ans/src/notification_preferences_database.cpp b/services/ans/src/notification_preferences_database.cpp index 2eeaa68f7dc6b437661910f7bba340d477be2d69..bbf76c1a59e5499c6c561f19bac46b503251b473 100644 --- a/services/ans/src/notification_preferences_database.cpp +++ b/services/ans/src/notification_preferences_database.cpp @@ -864,7 +864,11 @@ void NotificationPreferencesDatabase::ParseSlotFromDisturbeDB(NotificationPrefer NotificationConstant::SlotType slotType = static_cast(StringToInt(typeStr)); sptr slot = nullptr; if (!bundleInfo.GetSlot(slotType, slot)) { - slot = new NotificationSlot(slotType); + slot = new (std::nothrow) NotificationSlot(slotType); + if (slot == nullptr) { + ANS_LOGE("Failed to create NotificationSlot instance"); + return; + } } std::string findString = GenerateSlotKey(bundleKey, typeStr) + KEY_UNDER_LINE; ParseSlot(findString, slot, entry); @@ -1216,8 +1220,12 @@ void NotificationPreferencesDatabase::GetDoNotDisturbType(NotificationPreference std::string().append(KEY_DO_NOT_DISTURB_TYPE).append(KEY_UNDER_LINE).append(std::to_string(userId)); GetValueFromDisturbeDB( key, [&](const int32_t &status, std::string &value) { - sptr disturbDate = - new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0); + sptr disturbDate = new (std::nothrow) + NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0); + if (disturbDate == nullptr) { + ANS_LOGE("Failed to create NotificationDoNotDisturbDate instance"); + return; + } info.GetDoNotDisturbDate(userId, disturbDate); if (status == NativeRdb::E_EMPTY_VALUES_BUCKET) { PutDoNotDisturbDate(userId, disturbDate); @@ -1241,8 +1249,12 @@ void NotificationPreferencesDatabase::GetDoNotDisturbBeginDate(NotificationPrefe std::string().append(KEY_DO_NOT_DISTURB_BEGIN_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId)); GetValueFromDisturbeDB( key, [&](const int32_t &status, std::string &value) { - sptr disturbDate = - new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0); + sptr disturbDate = new (std::nothrow) + NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0); + if (disturbDate == nullptr) { + ANS_LOGE("Failed to create NotificationDoNotDisturbDate instance"); + return; + } info.GetDoNotDisturbDate(userId, disturbDate); if (status == NativeRdb::E_EMPTY_VALUES_BUCKET) { PutDoNotDisturbDate(userId, disturbDate); @@ -1265,8 +1277,12 @@ void NotificationPreferencesDatabase::GetDoNotDisturbEndDate(NotificationPrefere std::string().append(KEY_DO_NOT_DISTURB_END_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId)); GetValueFromDisturbeDB( key, [&](const int32_t &status, std::string &value) { - sptr disturbDate = - new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0); + sptr disturbDate = new (std::nothrow) + NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0); + if (disturbDate == nullptr) { + ANS_LOGE("Failed to create NotificationDoNotDisturbDate instance"); + return; + } info.GetDoNotDisturbDate(userId, disturbDate); if (status == NativeRdb::E_EMPTY_VALUES_BUCKET) { PutDoNotDisturbDate(userId, disturbDate); diff --git a/services/ans/src/notification_subscriber_manager.cpp b/services/ans/src/notification_subscriber_manager.cpp index 0c1428b894ff2d36490c5d24fb3aa1dd8320fc77..389e787b4f542f4948d5908c717ec532fd9d785a 100644 --- a/services/ans/src/notification_subscriber_manager.cpp +++ b/services/ans/src/notification_subscriber_manager.cpp @@ -41,8 +41,11 @@ NotificationSubscriberManager::NotificationSubscriberManager() { ANS_LOGI("constructor"); notificationSubQueue_ = std::make_shared("NotificationSubscriberMgr"); - recipient_ = - new RemoteDeathRecipient(std::bind(&NotificationSubscriberManager::OnRemoteDied, this, std::placeholders::_1)); + recipient_ = new (std::nothrow) + RemoteDeathRecipient(std::bind(&NotificationSubscriberManager::OnRemoteDied, this, std::placeholders::_1)); + if (recipient_ == nullptr) { + ANS_LOGE("Failed to create RemoteDeathRecipient instance"); + } } NotificationSubscriberManager::~NotificationSubscriberManager() diff --git a/services/ans/src/system_event_observer.cpp b/services/ans/src/system_event_observer.cpp index 2cce8ebd9c3c3182f911e6bf542182c483beea7b..12a7c314731155fb3ed7cdb1ba2454e9d875c863 100644 --- a/services/ans/src/system_event_observer.cpp +++ b/services/ans/src/system_event_observer.cpp @@ -54,9 +54,10 @@ sptr SystemEventObserver::GetBundleOption(AAFwk::Want auto element = want.GetElement(); std::string bundleName = element.GetBundleName(); int32_t uid = want.GetIntParam(AppExecFwk::Constants::UID, -1); - sptr bundleOption = new NotificationBundleOption(bundleName, uid); + sptr bundleOption = new (std::nothrow) NotificationBundleOption(bundleName, uid); if (bundleOption == nullptr) { ANS_LOGE("Failed to create bundleOption."); + return nullptr; } return bundleOption; } diff --git a/tools/dump/src/notification_shell_command.cpp b/tools/dump/src/notification_shell_command.cpp index 1d4b04f71e30f1beeadfbc67a961bb49344c0f5f..3ca804a4593eb47d2c93b3aedb82348a2e96e130 100644 --- a/tools/dump/src/notification_shell_command.cpp +++ b/tools/dump/src/notification_shell_command.cpp @@ -107,7 +107,11 @@ ErrCode NotificationShellCommand::Init() void NotificationShellCommand::SetNativeToken() { uint64_t tokenId; - const char **perms = new const char *[1]; + const char **perms = new (std::nothrow) const char *[1]; + if (perms == nullptr) { + ANS_LOGE("Failed to create buffer."); + return; + } perms[0] = "ohos.permission.NOTIFICATION_CONTROLLER"; NativeTokenInfoParams infoInstance = { .dcapsNum = 0,