From 4e22c3ec493c26fcda0c620ab7f0f17149a24008 Mon Sep 17 00:00:00 2001 From: Ethan Gao <16472154+gaoethan@users.noreply.github.com> Date: Fri, 27 Oct 2017 23:46:57 +0800 Subject: [PATCH] Fix rmw_fastrtps dead code (#163) * * Fix rmw_fastrtps dead code Dead code in API rmw_create_subscription() becasue the condition "rmw_subscription" cannot be true in the "fail" clauses, the execution cannot reach this statement: rmw_subscription_free(rmw_subscription); finally, the "rmw_subscribtion" is free by rmw_destroy_subscription() when it's not a nullptr Dead code in API rmw_create_publisher() because the condition "rmw_publisher" cannot be true in the "fail" clauses, the execution cannot reach this statement: rmw_publisher_free(rmw_publisher); finally, the "rmw_publisher" is free by rmw_destroy_publisher() when it's not a nullptr Signed-off-by: Ethan Gao * * Add memory allocation checking for topic name this memory allocation checking avoids the unintended behaviour which derives from unknown memory free issue at rmw_destroy_publisher Signed-off-by: Ethan Gao * publiser -> publisher Signed-off-by: Chris Lalancette * tweak the log information more accurate Signed-off-by: Ethan Gao --- rmw_fastrtps_cpp/src/rmw_publisher.cpp | 6 ++++++ rmw_fastrtps_cpp/src/rmw_subscription.cpp | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/rmw_fastrtps_cpp/src/rmw_publisher.cpp b/rmw_fastrtps_cpp/src/rmw_publisher.cpp index 7b7ab92eb..e50aef3df 100644 --- a/rmw_fastrtps_cpp/src/rmw_publisher.cpp +++ b/rmw_fastrtps_cpp/src/rmw_publisher.cpp @@ -168,6 +168,12 @@ rmw_create_publisher( rmw_publisher->implementation_identifier = eprosima_fastrtps_identifier; rmw_publisher->data = info; rmw_publisher->topic_name = reinterpret_cast(rmw_allocate(strlen(topic_name) + 1)); + + if (!rmw_publisher->topic_name) { + RMW_SET_ERROR_MSG("failed to allocate memory for publisher topic name"); + goto fail; + } + memcpy(const_cast(rmw_publisher->topic_name), topic_name, strlen(topic_name) + 1); return rmw_publisher; diff --git a/rmw_fastrtps_cpp/src/rmw_subscription.cpp b/rmw_fastrtps_cpp/src/rmw_subscription.cpp index 28b9c54a6..7c222865c 100644 --- a/rmw_fastrtps_cpp/src/rmw_subscription.cpp +++ b/rmw_fastrtps_cpp/src/rmw_subscription.cpp @@ -150,6 +150,12 @@ rmw_create_subscription( rmw_subscription->data = info; rmw_subscription->topic_name = reinterpret_cast(rmw_allocate(strlen(topic_name) + 1)); + + if (!rmw_subscription->topic_name) { + RMW_SET_ERROR_MSG("failed to allocate memory for subscription topic name"); + goto fail; + } + memcpy(const_cast(rmw_subscription->topic_name), topic_name, strlen(topic_name) + 1); return rmw_subscription;