Skip to content

Commit 2a77e9d

Browse files
fix: contain fastcdr/std exceptions in serialize() so none cross the extern "C" boundary (#4)
1 parent 2ed0026 commit 2a77e9d

1 file changed

Lines changed: 24 additions & 15 deletions

File tree

‎rmw_unix_socket_cpp/src/serialization.cpp‎

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,24 +102,33 @@ bool serialize(
102102
const message_type_support_callbacks_t * callbacks,
103103
std::vector<uint8_t> & buffer)
104104
{
105-
// Get estimated serialized size (4 bytes encapsulation + message)
106-
auto data_length = 4 + callbacks->get_serialized_size(ros_message);
107-
buffer.resize(data_length);
108-
109-
eprosima::fastcdr::FastBuffer fastbuffer(
110-
reinterpret_cast<char *>(buffer.data()), data_length);
111-
eprosima::fastcdr::Cdr ser(
112-
fastbuffer,
113-
eprosima::fastcdr::Cdr::DEFAULT_ENDIAN,
114-
eprosima::fastcdr::CdrVersion::DDS_CDR);
105+
try {
106+
// Get estimated serialized size (4 bytes encapsulation + message)
107+
auto data_length = 4 + callbacks->get_serialized_size(ros_message);
108+
buffer.resize(data_length);
109+
110+
eprosima::fastcdr::FastBuffer fastbuffer(
111+
reinterpret_cast<char *>(buffer.data()), data_length);
112+
eprosima::fastcdr::Cdr ser(
113+
fastbuffer,
114+
eprosima::fastcdr::Cdr::DEFAULT_ENDIAN,
115+
eprosima::fastcdr::CdrVersion::DDS_CDR);
116+
117+
ser.serialize_encapsulation();
118+
if (!callbacks->cdr_serialize(ros_message, ser)) {
119+
return false;
120+
}
115121

116-
ser.serialize_encapsulation();
117-
if (!callbacks->cdr_serialize(ros_message, ser)) {
122+
// Trim buffer to actual serialized size
123+
buffer.resize(ser.get_serialized_data_length());
124+
} catch (const eprosima::fastcdr::exception::Exception &) {
125+
return false;
126+
} catch (const std::exception &) {
127+
// get_serialized_size() can throw std::overflow_error (string/sequence
128+
// overflow); buffer.resize() can throw std::bad_alloc. Callers are
129+
// extern "C", so nothing may escape across the C ABI.
118130
return false;
119131
}
120-
121-
// Trim buffer to actual serialized size
122-
buffer.resize(ser.get_serialized_data_length());
123132
return true;
124133
}
125134

0 commit comments

Comments
 (0)