Use new (std::nothrow) throughout
If code uses "normal" `new`, the compiler is allowed to assume that
`nullptr` is never returned, even if built with -fno-exceptions.
Use `new (std::nothrow)` throughout so that `nullptr` gets returned
and can (in some cases) be handled.
Bug: 215451239
Test: VtsAidlKeyMintTargetTest
Ignore-AOSP-First: Cherry pick from AOSP
Change-Id: Ied97ce946c0fbfbd5449c533e7ce1acbd0f595c5
(cherry picked from commit b80ef359b047a6bb72947b582102a979568f5c30)
diff --git a/ng/AndroidKeymaster3Device.cpp b/ng/AndroidKeymaster3Device.cpp
index 5ab1739..8c17f69 100644
--- a/ng/AndroidKeymaster3Device.cpp
+++ b/ng/AndroidKeymaster3Device.cpp
@@ -85,7 +85,7 @@
class KmParamSet : public keymaster_key_param_set_t {
public:
explicit KmParamSet(const hidl_vec<KeyParameter>& keyParams) {
- params = new keymaster_key_param_t[keyParams.size()];
+ params = new (std::nothrow) keymaster_key_param_t[keyParams.size()];
length = keyParams.size();
for (size_t i = 0; i < keyParams.size(); ++i) {
auto tag = legacy_enum_conversion(keyParams[i].tag);
@@ -220,9 +220,9 @@
} // anonymous namespace
AndroidKeymaster3Device::AndroidKeymaster3Device()
- : impl_(new ::keymaster::AndroidKeymaster(
- []() -> auto {
- auto context = new PureSoftKeymasterContext(KmVersion::KEYMASTER_3);
+ : impl_(new (std::nothrow)::keymaster::AndroidKeymaster(
+ []() -> auto{
+ auto context = new (std::nothrow) PureSoftKeymasterContext(KmVersion::KEYMASTER_3);
context->SetSystemVersion(GetOsVersion(), GetOsPatchlevel());
context->SetVendorPatchlevel(GetVendorPatchlevel());
// Software devices cannot be configured by the boot loader but they have
@@ -237,7 +237,8 @@
AndroidKeymaster3Device::AndroidKeymaster3Device(KeymasterContext* context,
KeymasterHardwareProfile profile)
- : impl_(new ::keymaster::AndroidKeymaster(context, kOperationTableSize)), profile_(profile) {}
+ : impl_(new (std::nothrow)::keymaster::AndroidKeymaster(context, kOperationTableSize)),
+ profile_(profile) {}
AndroidKeymaster3Device::~AndroidKeymaster3Device() {}
@@ -504,20 +505,22 @@
}
IKeymasterDevice* CreateKeymasterDevice() {
- return new AndroidKeymaster3Device();
+ return new (std::nothrow) AndroidKeymaster3Device();
}
IKeymasterDevice* CreateKeymasterDevice(keymaster2_device_t* km2_device) {
if (ConfigureDevice(km2_device) != KM_ERROR_OK) return nullptr;
- auto context = new Keymaster2PassthroughContext(KmVersion::KEYMASTER_3, km2_device);
+ auto context =
+ new (std::nothrow) Keymaster2PassthroughContext(KmVersion::KEYMASTER_3, km2_device);
context->SetSystemVersion(GetOsVersion(), GetOsPatchlevel());
- return new AndroidKeymaster3Device(context, KeymasterHardwareProfile::KM2);
+ return new (std::nothrow) AndroidKeymaster3Device(context, KeymasterHardwareProfile::KM2);
}
IKeymasterDevice* CreateKeymasterDevice(keymaster1_device_t* km1_device) {
- auto context = new Keymaster1PassthroughContext(KmVersion::KEYMASTER_3, km1_device);
+ auto context =
+ new (std::nothrow) Keymaster1PassthroughContext(KmVersion::KEYMASTER_3, km1_device);
context->SetSystemVersion(GetOsVersion(), GetOsPatchlevel());
- return new AndroidKeymaster3Device(context, KeymasterHardwareProfile::KM1);
+ return new (std::nothrow) AndroidKeymaster3Device(context, KeymasterHardwareProfile::KM1);
}
} // namespace ng