diff --git a/include/fastcdr/Cdr.h b/include/fastcdr/Cdr.h index 983c886b..f4d65594 100644 --- a/include/fastcdr/Cdr.h +++ b/include/fastcdr/Cdr.h @@ -706,10 +706,7 @@ class Cdr */ TEMPLATE_SPEC Cdr& serialize( - const std::string& string_t) - { - return serialize(string_t.c_str()); - } + const std::string& string_t); /*! * @brief This function serializes a std::wstring. diff --git a/src/cpp/Cdr.cpp b/src/cpp/Cdr.cpp index 1b31547e..4a1e378a 100644 --- a/src/cpp/Cdr.cpp +++ b/src/cpp/Cdr.cpp @@ -14,6 +14,7 @@ #include #include +#include #include @@ -851,6 +852,27 @@ Cdr& Cdr::serialize( return *this; } +TEMPLATE_SPEC +Cdr& Cdr::serialize( + const std::string& string_t) +{ + // An empty string is serialized as a 0 length string. + if (string_t.empty()) + { + return serialize(static_cast(0)); + } + + // Check there are no null characters in the string. + const char* c_str = string_t.c_str(); + const auto str_len = strlen(c_str); + if (string_t.size() > str_len) + { + throw BadParamException("The string contains null characters"); + } + + return serialize_sequence(c_str, str_len + 1); +} + Cdr& Cdr::serialize_array( const bool* bool_t, size_t num_elements)