Fix implementation create methods.
authorBartlomiej Grzelewski <b.grzelewski@samsung.com>
Fri, 22 Aug 2014 16:30:26 +0000 (18:30 +0200)
committerBartlomiej Grzelewski <b.grzelewski@samsung.com>
Fri, 12 Sep 2014 12:59:28 +0000 (14:59 +0200)
Old immplementation was able to throw exception if memory allocation
faid. New implementation will return empty shared_ptr. Fixes were applied to:
 * Key::create
 * Certificate::create
 * Control::create
 * Storage::create

Change-Id: I6c9634b3df0d84f59bb0500c022d7dc3f4e97c72

src/manager/client/client-control.cpp
src/manager/client/client-manager-impl.cpp
src/manager/common/certificate-impl.cpp
src/manager/common/generic-key.cpp

index 6f3c5d3..7c2b927 100644 (file)
@@ -20,6 +20,8 @@
  * @version     1.0
  * @brief       This file is implementation of client-common functions.
  */
+#include <dpl/log/log.h>
+
 #include <client-common.h>
 #include <message-buffer.h>
 #include <protocols.h>
@@ -194,7 +196,14 @@ public:
 };
 
 ControlShPtr Control::create() {
-    return ControlShPtr(new ControlImpl());
+    try {
+        return std::make_shared<ControlImpl>();
+    } catch (const std::bad_alloc &) {
+        LogDebug("Bad alloc was caught during ControlImpl creation.");
+    } catch (...) {
+        LogError("Critical error: Unknown exception was caught druing ControlImpl creation!");
+    }
+    return ControlShPtr();
 }
 
 } // namespace CKM
index 7f74ef9..a838b8a 100644 (file)
@@ -644,7 +644,14 @@ int ManagerImpl::ocspCheck(const CertificateShPtrVector &certChain, int &ocspSta
 }
 
 ManagerShPtr Manager::create() {
-    return ManagerShPtr(new ManagerImpl());
+    try {
+        return std::make_shared<ManagerImpl>();
+    } catch (const std::bad_alloc &) {
+        LogDebug("Bad alloc was caught during ManagerImpl creation.");
+    } catch (...) {
+        LogError("Critical error: Unknown exception was caught during ManagerImpl creation!");
+    }
+    return ManagerShPtr();
 }
 
 } // namespace CKM
index 10d818a..71caf7e 100644 (file)
@@ -253,10 +253,17 @@ CertificateImpl::~CertificateImpl() {
 }
 
 CertificateShPtr Certificate::create(const RawBuffer &rawBuffer, DataFormat format) {
-    CertificateShPtr output(new CertificateImpl(rawBuffer, format));
-    if (output.get() == NULL)
-        output.reset();
-    return output;
+    try {
+        CertificateShPtr output = std::make_shared<CertificateImpl>(rawBuffer, format);
+        if (output->empty())
+            output.reset();
+        return output;
+    } catch (const std::bad_alloc &) {
+        LogDebug("Bad alloc was caught during CertificateImpl creation");
+    } catch (...) {
+        LogError("Critical error: Unknown exception was caught during CertificateImpl creation!");
+    }
+    return CertificateShPtr();
 }
 
 } // namespace CKM
index b5112fc..6835b8e 100644 (file)
@@ -205,10 +205,17 @@ RawBuffer GenericKey::getDER() const {
 }
 
 KeyShPtr Key::create(const RawBuffer &raw, const Password &password) {
-    KeyShPtr output(new GenericKey(raw, password));
-    if (output->empty())
-        output.reset();
-    return output;
+    try {
+        KeyShPtr output = std::make_shared<GenericKey>(raw, password);
+        if (output->empty())
+            output.reset();
+        return output;
+    } catch (const std::bad_alloc &) {
+        LogDebug("Bad alloc was catch during GenericKey creation");
+    } catch (...) {
+        LogError("Critical error: Unknown exception was caught during GenericKey creation");
+    }
+    return KeyShPtr();
 }
 
 } // namespace CKM