From 6d6b78f28d706eeb9cba83ef6398ea11058394c5 Mon Sep 17 00:00:00 2001 From: Jacob Perron Date: Tue, 20 Sep 2022 13:17:30 -0700 Subject: [PATCH] Handle allocation errors during message deserialization (#313) (#419) Signed-off-by: Michel Hidalgo Signed-off-by: Michel Hidalgo Co-authored-by: Michel Hidalgo --- .../rmw_cyclonedds_cpp/TypeSupport_impl.hpp | 32 +++++++++++++++++-- rmw_cyclonedds_cpp/src/rmw_node.cpp | 3 ++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/rmw_cyclonedds_cpp/include/rmw_cyclonedds_cpp/TypeSupport_impl.hpp b/rmw_cyclonedds_cpp/include/rmw_cyclonedds_cpp/TypeSupport_impl.hpp index bf2d229..bbe9d47 100644 --- a/rmw_cyclonedds_cpp/include/rmw_cyclonedds_cpp/TypeSupport_impl.hpp +++ b/rmw_cyclonedds_cpp/include/rmw_cyclonedds_cpp/TypeSupport_impl.hpp @@ -127,6 +127,32 @@ align_int_(size_t __align, T __int) noexcept return (__int - 1u + __align) & ~(__align - 1); } +inline void resize_field( + const rosidl_typesupport_introspection_cpp::MessageMember * member, + void * field, + size_t size) +{ + if (!member->resize_function) { + throw std::runtime_error("unexpected error: resize function is null"); + } + + member->resize_function(field, size); +} + +inline void resize_field( + const rosidl_typesupport_introspection_c__MessageMember * member, + void * field, + size_t size) +{ + if (!member->resize_function) { + throw std::runtime_error("unexpected error: resize function is null"); + } + + if (!member->resize_function(field, size)) { + throw std::runtime_error("unable to resize field"); + } +} + template static inline T * align_ptr_(size_t __align, T * __ptr) noexcept @@ -315,7 +341,7 @@ inline void deserialize_field( size = static_cast(member->array_size_); } else { deser >> size; - member->resize_function(field, size); + resize_field(member, field, size); } for (size_t i = 0; i < size; ++i) { void * element = member->get_function(field, i); @@ -342,7 +368,9 @@ void deserialize_field( auto & data = *reinterpret_cast::type *>(field); int32_t dsize = 0; deser >> dsize; - GenericCSequence::init(&data, dsize); + if (!GenericCSequence::init(&data, dsize)) { + throw std::runtime_error("unable initialize generic sequence"); + } deser.deserializeA(reinterpret_cast(data.data), dsize); } } diff --git a/rmw_cyclonedds_cpp/src/rmw_node.cpp b/rmw_cyclonedds_cpp/src/rmw_node.cpp index 9a5c62a..393e522 100644 --- a/rmw_cyclonedds_cpp/src/rmw_node.cpp +++ b/rmw_cyclonedds_cpp/src/rmw_node.cpp @@ -1445,6 +1445,9 @@ extern "C" rmw_ret_t rmw_deserialize( } catch (rmw_cyclonedds_cpp::Exception & e) { RMW_SET_ERROR_MSG_WITH_FORMAT_STRING("rmw_serialize: %s", e.what()); ok = false; + } catch (std::runtime_error & e) { + RMW_SET_ERROR_MSG_WITH_FORMAT_STRING("rmw_serialize: %s", e.what()); + ok = false; } return ok ? RMW_RET_OK : RMW_RET_ERROR;