diff --git a/frameworks/ans/src/notification_flags.cpp b/frameworks/ans/src/notification_flags.cpp index e26f1339d26ce32cc33289a6394ada08f31a6866..6107a87e4a8b6a5ef3f4dbcf790468af9f32eede 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 00f495bec7a54f5da79576b1f5b83d79cb28a987..fc81a0d44e489082ca78c1b8e57a72dcfc47088b 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -177,7 +177,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); @@ -277,7 +281,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_; } @@ -347,7 +355,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; } @@ -365,7 +377,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 { @@ -616,7 +632,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; } @@ -1527,8 +1547,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; handler_->PostSyncTask(std::bind([&]() { @@ -1711,9 +1735,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); @@ -1724,10 +1749,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); handler_->PostSyncTask(std::bind([&]() { if (!IsNotificationExists(record->notification->GetKey())) { @@ -2218,7 +2245,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); @@ -2953,7 +2985,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; @@ -3018,7 +3054,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; @@ -3343,11 +3383,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) { @@ -3379,7 +3423,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; @@ -3917,7 +3966,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 f73079cd8c80821b55a3b3e13eb3dff1f95bf334..a346ce069f3b6daf449531263b4ad54e2438a6f4 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 ce85d2d25be7ce331964b5c4b0e8d28110f53b35..00df14525571e58ff5a1c9dcd768c69327a98e6b 100644 --- a/services/ans/src/notification_preferences_database.cpp +++ b/services/ans/src/notification_preferences_database.cpp @@ -898,7 +898,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); @@ -1257,8 +1261,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); @@ -1282,8 +1290,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); @@ -1306,8 +1318,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 6dacae6fe63007bd1488241fb8ed22e5d55c1c97..1bfaec0e4ab4ed04f002360d5947edc1d1672c45 100644 --- a/services/ans/src/notification_subscriber_manager.cpp +++ b/services/ans/src/notification_subscriber_manager.cpp @@ -42,8 +42,11 @@ NotificationSubscriberManager::NotificationSubscriberManager() runner_ = OHOS::AppExecFwk::EventRunner::Create("NotificationSubscriberMgr"); handler_ = std::make_shared(runner_); AnsWatchdog::AddHandlerThread(handler_, runner_); - 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 33a8d2eeba6b69d085cd5d001a00f58154dddfb8..b9e979f6df25503f65514cd35f80bafb0c7f6d06 100644 --- a/services/ans/src/system_event_observer.cpp +++ b/services/ans/src/system_event_observer.cpp @@ -53,9 +53,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,