Skip to content

Commit

Permalink
Serialize single null-character for empty strings (#249)
Browse files Browse the repository at this point in the history
* Refs #22284. Add regression test.

Signed-off-by: Miguel Company <[email protected]>

* Refs #22284. Fix issue.

Signed-off-by: Miguel Company <[email protected]>

---------

Signed-off-by: Miguel Company <[email protected]>
  • Loading branch information
MiguelCompany authored Nov 27, 2024
1 parent ae92052 commit bfa51aa
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
6 changes: 0 additions & 6 deletions include/fastcdr/Cdr.h
Original file line number Diff line number Diff line change
Expand Up @@ -689,12 +689,6 @@ class 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);
Expand Down
6 changes: 0 additions & 6 deletions include/fastcdr/FastCdr.h
Original file line number Diff line number Diff line change
Expand Up @@ -891,12 +891,6 @@ class Cdr_DllAPI FastCdr
FastCdr& 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);
Expand Down
26 changes: 26 additions & 0 deletions test/cdr/SimpleTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7111,3 +7111,29 @@ TEST(FastCDRTests, StringWithNullChars)
},
BadParamException);
}

TEST(CDRTests, EmptyStringSerializationSize)
{
std::string str;
char buffer[256];
FastBuffer cdrbuffer(buffer, 256);
Cdr cdr_ser(cdrbuffer);
EXPECT_NO_THROW(
{
cdr_ser << str;
});
EXPECT_EQ(cdr_ser.get_serialized_data_length(), 5u);
}

TEST(FastCDRTests, EmptyStringSerializationSize)
{
std::string str;
char buffer[256];
FastBuffer cdrbuffer(buffer, 256);
FastCdr cdr_ser(cdrbuffer);
EXPECT_NO_THROW(
{
cdr_ser << str;
});
EXPECT_EQ(cdr_ser.get_serialized_data_length(), 5u);
}

0 comments on commit bfa51aa

Please sign in to comment.