Skip to content

Commit

Permalink
Fix wchar deserialize error when swaping the bytes (#186)
Browse files Browse the repository at this point in the history
* Regression test for #185 in SimpleTest

Signed-off-by: weihanwu <weihanwu@lixiang.com>

* Fix wchar deserialize error when swaping the bytes

Signed-off-by: weihanwu <weihanwu@lixiang.com>
Co-authored-by: maxin <maxin@lixiang.com>

* Apply suggestion

Signed-off-by: Miguel Company <miguelcompany@eprosima.com>

Co-authored-by: Ricardo González <correoricky@gmail.com>

---------

Signed-off-by: weihanwu <weihanwu@lixiang.com>
Co-authored-by: maxin <maxin@lixiang.com>
Co-authored-by: Miguel Company <miguelcompany@eprosima.com>
Co-authored-by: Ricardo González <correoricky@gmail.com>
  • Loading branch information
4 people authored and EduPonz committed Feb 22, 2024
1 parent df325ce commit adfd1ce
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
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

0 comments on commit adfd1ce

Please sign in to comment.