Skip to content

Commit

Permalink
Refs #21362. Fix behavior.
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Company <[email protected]>
  • Loading branch information
MiguelCompany committed Nov 4, 2024
1 parent 2fcd408 commit cc19839
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
5 changes: 1 addition & 4 deletions include/fastcdr/Cdr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 22 additions & 0 deletions src/cpp/Cdr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <cstring>
#include <limits>
#include <string>

#include <fastcdr/Cdr.h>

Expand Down Expand Up @@ -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<uint32_t>(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)
Expand Down

0 comments on commit cc19839

Please sign in to comment.