virtual ~IBackend() { }
virtual int Set(const Data& data) = 0;
- virtual std::shared_ptr<Data> Get(const std::string& key) = 0;
+ virtual std::unique_ptr<Data> Get(const std::string& key) = 0;
virtual int Remove(const std::string& key) = 0;
virtual int RemoveAll() = 0;
virtual int AddWatch(const std::string& key, IDataEvent* listener) = 0;
return file.Write();
}
-std::shared_ptr<Data> FileBackend::Get(const std::string& key) {
+std::unique_ptr<Data> FileBackend::Get(const std::string& key) {
File file(key, false);
if (file.Read() < 0)
return nullptr;
- return std::make_shared<Data>(file.GetData());
+ return file.GetData();
}
int FileBackend::Remove(const std::string& key) {
if (file.Read() < 0)
return std::string("");
- auto data = file.GetData();
- return data.GetKey();
+ std::unique_ptr<Data> data(file.GetData());
+ if (data.get() == nullptr)
+ return std::string("");
+
+ return data->GetKey();
}
} // namespace internal
virtual ~FileBackend();
int Set(const Data& data) override;
- std::shared_ptr<Data> Get(const std::string& key) override;
+ std::unique_ptr<Data> Get(const std::string& key) override;
int Remove(const std::string& key) override;
int RemoveAll() override;
int AddWatch(const std::string& key, IDataEvent* listener) override;
std::back_inserter(raw_data_));
}
-Data File::GetData() {
- int index = 0;
+std::unique_ptr<Data> File::GetData() {
+ if (raw_data_.size() < sizeof(int))
+ return nullptr;
+
+ size_t index = 0;
int key_length = 0;
uint8_t* p = reinterpret_cast<uint8_t*>(&key_length);
std::copy(&raw_data_[index], &raw_data_[index] + sizeof(int), p);
index += sizeof(int);
+ if (index + key_length > raw_data_.size())
+ return nullptr;
+
std::string key;
std::copy(&raw_data_[index], &raw_data_[index] + key_length,
std::back_inserter(key));
index += key_length;
+ if (index + sizeof(int) > raw_data_.size())
+ return nullptr;
+
int type = 0;
p = reinterpret_cast<uint8_t*>(&type);
std::copy(&raw_data_[index], &raw_data_[index] + sizeof(int), p);
index += sizeof(int);
- Data data(key);
+ auto data = std::make_unique<Data>(key);
+ if (data.get() == nullptr) {
+ _E("Out of memory");
+ return nullptr;
+ }
Data::Type data_type = static_cast<Data::Type>(type);
if (data_type == Data::Type::STRING) {
+ if (index == raw_data_.size())
+ return nullptr;
+
const char* str = reinterpret_cast<const char*>(&raw_data_[index]);
- data.SetString(str);
+ 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);
+ 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);
+ 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->SetBoolean(b);
}
return data;
}
void SetData(const Data& data);
- Data GetData();
+ std::unique_ptr<Data> GetData();
int Write();
int Read();
gid_t GetGid(const std::string& name);
Data::Type ConvertType(const std::string& type);
std::string GetTypeName(Data::Type type);
- void PrintData(const Data& data);
+ void PrintData(const Data* data);
};
PrefTool& PrefTool::GetInst() {
return;
}
- Data data = file.GetData();
- PrintData(data);
+ auto data = file.GetData();
+ if (data.get() == nullptr)
+ return;
+
+ PrintData(data.get());
}
void PrefTool::Get(const std::string& pkg_id) {
break;
}
- Data data = file.GetData();
- PrintData(data);
+ auto data = file.GetData();
+ if (data.get() != nullptr)
+ PrintData(data.get());
}
printf("=================================================================\n");
closedir(dp);
return "none";
}
-void PrefTool::PrintData(const Data& data) {
- auto type = data.GetType();
+void PrefTool::PrintData(const Data* data) {
+ auto type = data->GetType();
printf("[key] %s [type] %s [value] ",
- data.GetKey().c_str(), GetTypeName(type).c_str());
+ data->GetKey().c_str(), GetTypeName(type).c_str());
if (type == Data::Type::STRING)
- printf("%s\n", data.GetString());
+ printf("%s\n", data->GetString());
else if (type == Data::Type::INT)
- printf("%d\n", data.GetInt());
+ printf("%d\n", data->GetInt());
else if (type == Data::Type::DOUBLE)
- printf("%f\n", data.GetDouble());
+ printf("%f\n", data->GetDouble());
else
- printf("%s\n", data.GetBoolean() ? "true" : "false");
+ printf("%s\n", data->GetBoolean() ? "true" : "false");
}
} // namespace