Skip to content
Merged
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
39 changes: 24 additions & 15 deletions rmw_unix_socket_cpp/src/serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,24 +102,33 @@ bool serialize(
const message_type_support_callbacks_t * callbacks,
std::vector<uint8_t> & buffer)
{
// Get estimated serialized size (4 bytes encapsulation + message)
auto data_length = 4 + callbacks->get_serialized_size(ros_message);
buffer.resize(data_length);

eprosima::fastcdr::FastBuffer fastbuffer(
reinterpret_cast<char *>(buffer.data()), data_length);
eprosima::fastcdr::Cdr ser(
fastbuffer,
eprosima::fastcdr::Cdr::DEFAULT_ENDIAN,
eprosima::fastcdr::CdrVersion::DDS_CDR);
try {
// Get estimated serialized size (4 bytes encapsulation + message)
auto data_length = 4 + callbacks->get_serialized_size(ros_message);
buffer.resize(data_length);

eprosima::fastcdr::FastBuffer fastbuffer(
reinterpret_cast<char *>(buffer.data()), data_length);
eprosima::fastcdr::Cdr ser(
fastbuffer,
eprosima::fastcdr::Cdr::DEFAULT_ENDIAN,
eprosima::fastcdr::CdrVersion::DDS_CDR);

ser.serialize_encapsulation();
if (!callbacks->cdr_serialize(ros_message, ser)) {
return false;
}

ser.serialize_encapsulation();
if (!callbacks->cdr_serialize(ros_message, ser)) {
// Trim buffer to actual serialized size
buffer.resize(ser.get_serialized_data_length());
} catch (const eprosima::fastcdr::exception::Exception &) {
return false;
} catch (const std::exception &) {
// get_serialized_size() can throw std::overflow_error (string/sequence
// overflow); buffer.resize() can throw std::bad_alloc. Callers are
// extern "C", so nothing may escape across the C ABI.
return false;
}

// Trim buffer to actual serialized size
buffer.resize(ser.get_serialized_data_length());
return true;
}

Expand Down
Loading