Make usage of new/delete and malloc/free consistent.
All buffers returned to the caller should be allocated with malloc,
since the caller assumes it's calling a C API. Internally, stuff
allocated with new should be freed with delete, and so on.
Change-Id: Ie08d910b9f6ebee38dc39127310e695453d1256f
diff --git a/softkeymaster/keymaster_openssl.cpp b/softkeymaster/keymaster_openssl.cpp
index f81e844..3bf4cec 100644
--- a/softkeymaster/keymaster_openssl.cpp
+++ b/softkeymaster/keymaster_openssl.cpp
@@ -78,6 +78,12 @@
};
typedef UniquePtr<RSA, RSA_Delete> Unique_RSA;
+struct Malloc_Free {
+ void operator()(void* p) const {
+ free(p);
+ }
+};
+
typedef UniquePtr<keymaster_device_t> Unique_keymaster_device_t;
/**
@@ -123,7 +129,9 @@
*keyBlobLength = get_softkey_header_size() + sizeof(type) + sizeof(publicLen) + privateLen +
sizeof(privateLen) + publicLen;
- UniquePtr<unsigned char> derData(new unsigned char[*keyBlobLength]);
+ // derData will be returned to the caller, so allocate it with malloc.
+ UniquePtr<unsigned char, Malloc_Free> derData(
+ static_cast<unsigned char*>(malloc(*keyBlobLength)));
if (derData.get() == NULL) {
ALOGE("could not allocate memory for key blob");
return -1;
@@ -452,7 +460,7 @@
return -1;
}
- UniquePtr<uint8_t> key(static_cast<uint8_t*>(malloc(len)));
+ UniquePtr<uint8_t, Malloc_Free> key(static_cast<uint8_t*>(malloc(len)));
if (key.get() == NULL) {
ALOGE("Could not allocate memory for public key data");
return -1;
@@ -485,7 +493,7 @@
}
unsigned int dsaSize = DSA_size(dsa.get());
- UniquePtr<uint8_t> signedDataPtr(reinterpret_cast<uint8_t*>(malloc(dsaSize)));
+ UniquePtr<uint8_t, Malloc_Free> signedDataPtr(reinterpret_cast<uint8_t*>(malloc(dsaSize)));
if (signedDataPtr.get() == NULL) {
logOpenSSLError("openssl_sign_dsa");
return -1;
@@ -517,7 +525,7 @@
}
unsigned int ecdsaSize = ECDSA_size(eckey.get());
- UniquePtr<uint8_t> signedDataPtr(reinterpret_cast<uint8_t*>(malloc(ecdsaSize)));
+ UniquePtr<uint8_t, Malloc_Free> signedDataPtr(reinterpret_cast<uint8_t*>(malloc(ecdsaSize)));
if (signedDataPtr.get() == NULL) {
logOpenSSLError("openssl_sign_ec");
return -1;
@@ -551,7 +559,7 @@
return -1;
}
- UniquePtr<uint8_t> signedDataPtr(reinterpret_cast<uint8_t*>(malloc(dataLength)));
+ UniquePtr<uint8_t, Malloc_Free> signedDataPtr(reinterpret_cast<uint8_t*>(malloc(dataLength)));
if (signedDataPtr.get() == NULL) {
logOpenSSLError("openssl_sign_rsa");
return -1;
@@ -673,7 +681,7 @@
return -1;
}
- UniquePtr<uint8_t> dataPtr(reinterpret_cast<uint8_t*>(malloc(signedDataLength)));
+ UniquePtr<uint8_t[]> dataPtr(new uint8_t[signedDataLength]);
if (dataPtr.get() == NULL) {
logOpenSSLError("openssl_verify_data");
return -1;