From 76e87a291c7ee5e44aa95517fd75a7c47005fc87 Mon Sep 17 00:00:00 2001 From: Abderahmane BENALI Date: Tue, 2 Jun 2026 09:12:00 +0200 Subject: [PATCH] fix: contain fastcdr/std exceptions in serialize() so none cross the extern "C" boundary --- rmw_unix_socket_cpp/src/serialization.cpp | 39 ++++++++++++++--------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/rmw_unix_socket_cpp/src/serialization.cpp b/rmw_unix_socket_cpp/src/serialization.cpp index b81368e..449b7ff 100644 --- a/rmw_unix_socket_cpp/src/serialization.cpp +++ b/rmw_unix_socket_cpp/src/serialization.cpp @@ -102,24 +102,33 @@ bool serialize( const message_type_support_callbacks_t * callbacks, std::vector & 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(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(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; }