Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wchar deserialize error when swaping the bytes #186

Merged
merged 3 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/cpp/Cdr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1643,13 +1643,8 @@ Cdr& Cdr::deserialize(
// Allocate memory.
string_t = reinterpret_cast<wchar_t*>(calloc(length + 1, sizeof(wchar_t))); // WStrings never serialize terminating zero

for (size_t idx = 0; idx < length; ++idx)
{
uint16_t temp;
offset_ >> temp;
string_t[idx] = static_cast<wchar_t>(temp);
offset_ += sizeof(uint16_t);
}
deserialize_array(string_t, length);

return *this;
}

Expand Down
26 changes: 26 additions & 0 deletions test/cdr/SimpleTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1900,6 +1900,32 @@ TEST(CDRTests, CWString)

free(c_string_value);

// Check the big endianness case
char buffer_bigendi[BUFFER_LENGTH];

// Serialization.
FastBuffer cdrbuffer_bigendi(buffer_bigendi, BUFFER_LENGTH);
Cdr cdr_ser_bigendi(cdrbuffer_bigendi, eprosima::fastcdr::Cdr::Endianness::BIG_ENDIANNESS);

EXPECT_NO_THROW(
{
cdr_ser_bigendi.serialize(c_wstring_t);
});

// Deserialization.
Cdr cdr_des_bigendi(cdrbuffer_bigendi, eprosima::fastcdr::Cdr::Endianness::BIG_ENDIANNESS);

wchar_t* c_wstring_bigendi {nullptr};

EXPECT_NO_THROW(
{
cdr_des_bigendi.deserialize(c_wstring_bigendi);
});

EXPECT_EQ(wcscmp(c_wstring_bigendi, c_wstring_t), 0);

free(c_wstring_bigendi);

// Check bad case without space
char buffer_bad[1];

Expand Down
Loading