diff --git a/data/Deserializer.h b/data/Deserializer.h index e58fea64..ec77f2bc 100644 --- a/data/Deserializer.h +++ b/data/Deserializer.h @@ -91,12 +91,14 @@ namespace cpputils { } inline std::string Deserializer::readString() { - uint64_t size = readUint64(); - if (_pos + size > _source->size()) { - throw std::runtime_error("Deserialization failed - size overflow"); + //TODO Test whether that works when string ends (a) at beginning (b) in middle (c) at end of data region + const void *nullbytepos = std::memchr(_source->dataOffset(_pos), '\0', _source->size()-_pos); + if (nullbytepos == nullptr) { + throw std::runtime_error("Deserialization failed - missing nullbyte for string termination"); } + uint64_t size = static_cast(nullbytepos) - static_cast(_source->dataOffset(_pos)); std::string result(reinterpret_cast(_source->dataOffset(_pos)), size); - _pos += size; + _pos += size + 1; return result; } diff --git a/data/Serializer.h b/data/Serializer.h index cd0745de..e308c729 100644 --- a/data/Serializer.h +++ b/data/Serializer.h @@ -97,16 +97,16 @@ namespace cpputils { } inline void Serializer::writeString(const std::string &value) { - writeUint64(value.size()); - if (_pos + value.size() > _result.size()) { + size_t size = value.size() + 1; // +1 for the nullbyte + if (_pos + size > _result.size()) { throw std::runtime_error("Serialization failed - size overflow"); } - std::memcpy(static_cast(_result.dataOffset(_pos)), value.c_str(), value.size()); - _pos += value.size(); + std::memcpy(static_cast(_result.dataOffset(_pos)), value.c_str(), size); + _pos += size; } inline size_t Serializer::StringSize(const std::string &value) { - return sizeof(uint64_t) + value.size(); + return value.size() + 1; // +1 for nullbyte } inline Data Serializer::finished() {