From 49a3c0a89ec332623eda488e3d75d5b344ef9162 Mon Sep 17 00:00:00 2001 From: Michel Hidalgo Date: Wed, 26 Aug 2020 20:05:35 -0300 Subject: [PATCH 1/2] Improve __rmw_create_wait_set() implementation. Handle errors consistently. Signed-off-by: Michel Hidalgo --- rmw_fastrtps_shared_cpp/src/rmw_wait_set.cpp | 22 +++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/rmw_fastrtps_shared_cpp/src/rmw_wait_set.cpp b/rmw_fastrtps_shared_cpp/src/rmw_wait_set.cpp index f4c23c6ea..76350fbaf 100644 --- a/rmw_fastrtps_shared_cpp/src/rmw_wait_set.cpp +++ b/rmw_fastrtps_shared_cpp/src/rmw_wait_set.cpp @@ -35,32 +35,34 @@ __rmw_create_wait_set(const char * identifier, rmw_context_t * context, size_t m return nullptr); (void)max_conditions; - rmw_wait_set_t * wait_set = rmw_wait_set_allocate(); - CustomWaitsetInfo * wait_set_info = nullptr; // From here onward, error results in unrolling in the goto fail block. + CustomWaitsetInfo * wait_set_info = nullptr; + rmw_wait_set_t * wait_set = rmw_wait_set_allocate(); if (!wait_set) { RMW_SET_ERROR_MSG("failed to allocate wait set"); goto fail; } wait_set->implementation_identifier = identifier; wait_set->data = rmw_allocate(sizeof(CustomWaitsetInfo)); - // This should default-construct the fields of CustomWaitsetInfo - wait_set_info = static_cast(wait_set->data); - // cppcheck-suppress syntaxError - RMW_TRY_PLACEMENT_NEW(wait_set_info, wait_set_info, goto fail, CustomWaitsetInfo, ) - if (!wait_set_info) { - RMW_SET_ERROR_MSG("failed to construct wait set info struct"); + if (!wait_set->data) { + RMW_SET_ERROR_MSG("failed to allocate wait set info"); goto fail; } + // This should default-construct the fields of CustomWaitsetInfo + // cppcheck-suppress syntaxError + RMW_TRY_PLACEMENT_NEW( + wait_set_info, + wait_set->data, + goto fail, + CustomWaitsetInfo, ) + (void)wait_set_info; return wait_set; fail: if (wait_set) { if (wait_set->data) { - RMW_TRY_DESTRUCTOR_FROM_WITHIN_FAILURE( - wait_set_info->~CustomWaitsetInfo(), wait_set_info) rmw_free(wait_set->data); } rmw_wait_set_free(wait_set); From 0f34f90755dd550e022258af83cdc52fa4a4111e Mon Sep 17 00:00:00 2001 From: Michel Hidalgo Date: Thu, 27 Aug 2020 18:51:28 -0300 Subject: [PATCH 2/2] Please linters. Signed-off-by: Michel Hidalgo --- rmw_fastrtps_shared_cpp/src/rmw_wait_set.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rmw_fastrtps_shared_cpp/src/rmw_wait_set.cpp b/rmw_fastrtps_shared_cpp/src/rmw_wait_set.cpp index 76350fbaf..d1ac8b97f 100644 --- a/rmw_fastrtps_shared_cpp/src/rmw_wait_set.cpp +++ b/rmw_fastrtps_shared_cpp/src/rmw_wait_set.cpp @@ -50,13 +50,13 @@ __rmw_create_wait_set(const char * identifier, rmw_context_t * context, size_t m goto fail; } // This should default-construct the fields of CustomWaitsetInfo - // cppcheck-suppress syntaxError RMW_TRY_PLACEMENT_NEW( wait_set_info, wait_set->data, goto fail, - CustomWaitsetInfo, ) - (void)wait_set_info; + // cppcheck-suppress syntaxError + CustomWaitsetInfo, ); + (void) wait_set_info; return wait_set;