Fix static analysis issues 55/313655/2
authorHwankyu Jhun <h.jhun@samsung.com>
Sun, 30 Jun 2024 23:52:08 +0000 (08:52 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Mon, 1 Jul 2024 02:16:12 +0000 (11:16 +0900)
Checker:
 - COMPARE_RESULT_OF_NEW

Change-Id: Ifdbcff09dba722a42f368549fabac289f7a1c049
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
preference/file-internal.cc

index a46ddc186b5f81ac934bd4243f2bf525c3e718c9..57e16fac0a24b749d7c01fccd6884d93f4fe27b7 100644 (file)
@@ -21,6 +21,8 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#include <new>
+
 #include "preference/file-internal.hh"
 #include "preference/log-internal.hh"
 #include "preference/path-internal.hh"
@@ -106,46 +108,46 @@ std::unique_ptr<Data> File::GetData() {
   std::copy(&raw_data_[index], &raw_data_[index] + sizeof(int), p);
   index += sizeof(int);
 
-  auto data = std::make_unique<Data>(key);
-  if (data.get() == nullptr) {
-    _E("Out of memory");
-    return nullptr;
-  }
+  try {
+    auto data = std::make_unique<Data>(key);
+    Data::Type data_type = static_cast<Data::Type>(type);
+    if (data_type == Data::Type::STRING) {
+      if (index == raw_data_.size() || raw_data_[raw_data_.size() - 1] != '\0')
+        raw_data_.push_back(0x00);
+
+      const char* str = reinterpret_cast<const char*>(&raw_data_[index]);
+      data->SetString(str);
+    } else if (data_type == Data::Type::INT) {
+      if (index + sizeof(int) > raw_data_.size())
+        return nullptr;
+
+      int i = 0;
+      p = reinterpret_cast<uint8_t*>(&i);
+      std::copy(&raw_data_[index], &raw_data_[index] + sizeof(int), p);
+      data->SetInt(i);
+    } else if (data_type == Data::Type::DOUBLE) {
+      if (index + sizeof(double) > raw_data_.size())
+        return nullptr;
+
+      double d = 0;
+      p = reinterpret_cast<uint8_t*>(&d);
+      std::copy(&raw_data_[index], &raw_data_[index] + sizeof(double), p);
+      data->SetDouble(d);
+    } else if (data_type == Data::Type::BOOLEAN) {
+      if (index + sizeof(bool) > raw_data_.size())
+        return nullptr;
+
+      bool b = 0;
+      p = reinterpret_cast<uint8_t*>(&b);
+      std::copy(&raw_data_[index], &raw_data_[index] + sizeof(bool), p);
+      data->SetBoolean(b);
+    }
 
-  Data::Type data_type = static_cast<Data::Type>(type);
-  if (data_type == Data::Type::STRING) {
-    if (index == raw_data_.size() || raw_data_[raw_data_.size() - 1] != '\0')
-      raw_data_.push_back(0x00);
-
-    const char* str = reinterpret_cast<const char*>(&raw_data_[index]);
-    data->SetString(str);
-  } else if (data_type == Data::Type::INT) {
-    if (index + sizeof(int) > raw_data_.size())
-      return nullptr;
-
-    int i = 0;
-    p = reinterpret_cast<uint8_t*>(&i);
-    std::copy(&raw_data_[index], &raw_data_[index] + sizeof(int), p);
-    data->SetInt(i);
-  } else if (data_type == Data::Type::DOUBLE) {
-    if (index + sizeof(double) > raw_data_.size())
-      return nullptr;
-
-    double d = 0;
-    p = reinterpret_cast<uint8_t*>(&d);
-    std::copy(&raw_data_[index], &raw_data_[index] + sizeof(double), p);
-    data->SetDouble(d);
-  } else if (data_type == Data::Type::BOOLEAN) {
-    if (index + sizeof(bool) > raw_data_.size())
-      return nullptr;
-
-    bool b = 0;
-    p = reinterpret_cast<uint8_t*>(&b);
-    std::copy(&raw_data_[index], &raw_data_[index] + sizeof(bool), p);
-    data->SetBoolean(b);
+    return data;
+  } catch (const std::bad_alloc& e) {
+    _E("Exception occurs. error=%s", e.what());
+    return nullptr;
   }
-
-  return data;
 }
 
 int File::Write() {