diff --git a/src/serialize.h b/src/serialize.h --- a/src/serialize.h +++ b/src/serialize.h @@ -625,7 +625,7 @@ #define VARINT_MODE(obj, mode) Using>(obj) #define VARINT(obj) Using>(obj) #define COMPACTSIZE(obj) Using>(obj) -#define LIMITED_STRING(obj, n) LimitedString(REF(obj)) +#define LIMITED_STRING(obj, n) Using>(obj) /** * Serialization wrapper class for integers in VarInt format. @@ -712,29 +712,20 @@ } }; -template class LimitedString { -protected: - std::string &string; - -public: - explicit LimitedString(std::string &_string) : string(_string) {} - - template void Unserialize(Stream &s) { +template struct LimitedStringFormatter { + template void Unser(Stream &s, std::string &v) { size_t size = ReadCompactSize(s); if (size > Limit) { throw std::ios_base::failure("String length limit exceeded"); } - string.resize(size); + v.resize(size); if (size != 0) { - s.read((char *)string.data(), size); + s.read((char *)v.data(), size); } } - template void Serialize(Stream &s) const { - WriteCompactSize(s, string.size()); - if (!string.empty()) { - s.write((char *)string.data(), string.size()); - } + template void Ser(Stream &s, const std::string &v) { + s << v; } }; diff --git a/src/test/fuzz/string.cpp b/src/test/fuzz/string.cpp --- a/src/test/fuzz/string.cpp +++ b/src/test/fuzz/string.cpp @@ -108,7 +108,7 @@ { CDataStream data_stream{SER_NETWORK, INIT_PROTO_VERSION}; std::string s; - LimitedString<10> limited_string = LIMITED_STRING(s, 10); + auto limited_string = LIMITED_STRING(s, 10); data_stream << random_string_1; try { data_stream >> limited_string; @@ -123,8 +123,7 @@ } { CDataStream data_stream{SER_NETWORK, INIT_PROTO_VERSION}; - const LimitedString<10> limited_string = - LIMITED_STRING(random_string_1, 10); + const auto limited_string = LIMITED_STRING(random_string_1, 10); data_stream << limited_string; std::string deserialized_string; data_stream >> deserialized_string;