From: Hwankyu Jhun Date: Sun, 30 Jun 2024 23:52:08 +0000 (+0900) Subject: Fix static analysis issues X-Git-Tag: accepted/tizen/unified/20240702.091903~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=91e0f6c2da1e64ac49cf0d5183a8c219f1db6ce1;p=platform%2Fcore%2Fapi%2Fpreference.git Fix static analysis issues Checker: - COMPARE_RESULT_OF_NEW Change-Id: Ifdbcff09dba722a42f368549fabac289f7a1c049 Signed-off-by: Hwankyu Jhun --- diff --git a/preference/file-internal.cc b/preference/file-internal.cc index a46ddc1..57e16fa 100644 --- a/preference/file-internal.cc +++ b/preference/file-internal.cc @@ -21,6 +21,8 @@ #include #include +#include + #include "preference/file-internal.hh" #include "preference/log-internal.hh" #include "preference/path-internal.hh" @@ -106,46 +108,46 @@ std::unique_ptr File::GetData() { std::copy(&raw_data_[index], &raw_data_[index] + sizeof(int), p); index += sizeof(int); - auto data = std::make_unique(key); - if (data.get() == nullptr) { - _E("Out of memory"); - return nullptr; - } + try { + auto data = std::make_unique(key); + Data::Type data_type = static_cast(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(&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(&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(&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(&b); + std::copy(&raw_data_[index], &raw_data_[index] + sizeof(bool), p); + data->SetBoolean(b); + } - Data::Type data_type = static_cast(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(&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(&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(&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(&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() {