-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Memory corruption during serialization and deserialization type …
…strings (#10) * New serializer module Removed serialization and deserialization methods from utlils/compression.h Added utils/serialization.h to handle serilization and deserialization of values * Serialization and deserialization of type strings Added overloaded methods to specifically handle type strings. Used constexpr to identify which overloaded method to invoke given V type.
- Loading branch information
Showing
6 changed files
with
93 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#ifndef SERIALIZATION_H | ||
#define SERIALIZATION_H | ||
|
||
#include <sstream> | ||
#include <cstring> | ||
#include <type_traits> | ||
|
||
class Serializer { | ||
|
||
public: | ||
|
||
// Serialize function for std::string | ||
static std::string serialize(const std::string& data) { | ||
return data; | ||
} | ||
|
||
// Deserialize function for std::string | ||
static std::string deserialize(const std::string& serialized_val) { | ||
return serialized_val; | ||
} | ||
|
||
template<typename T> | ||
static std::string serialize(const T& data, std::true_type) { | ||
return std::to_string(data); | ||
} | ||
|
||
template<typename T> | ||
static T deserialize(const std::string& serialized_val, std::true_type) { | ||
std::istringstream iss(serialized_val); | ||
T data; | ||
iss >> data; | ||
return data; | ||
} | ||
|
||
template<typename T> | ||
static std::string serialize(const T& data, std::false_type) { | ||
|
||
// A byte-by-byte copy of the memory representation of the value object. | ||
const char* byte_seq = reinterpret_cast<const char*>(&data); | ||
return std::string(byte_seq, sizeof(T)); | ||
} | ||
|
||
template<typename T> | ||
static T deserialize(const std::string& serialized_val, std::false_type) { | ||
T value; | ||
std::memcpy(&value, serialized_val.data(), sizeof(T)); | ||
return value; | ||
} | ||
}; | ||
|
||
#endif |