Skip to content

Commit

Permalink
Fix rmw_fastrtps dead code (#163)
Browse files Browse the repository at this point in the history
* * 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 <ethan.gao@linux.intel.com>

* * 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 <ethan.gao@linux.intel.com>

* publiser -> publisher

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>

* tweak the log information more accurate

Signed-off-by: Ethan Gao <ethan.gao@linux.intel.com>
  • Loading branch information
gaoethan authored and dirk-thomas committed Oct 27, 2017
1 parent 6901bd5 commit 4e22c3e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions rmw_fastrtps_cpp/src/rmw_publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ rmw_create_publisher(
rmw_publisher->implementation_identifier = eprosima_fastrtps_identifier;
rmw_publisher->data = info;
rmw_publisher->topic_name = reinterpret_cast<char *>(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<char *>(rmw_publisher->topic_name), topic_name, strlen(topic_name) + 1);
return rmw_publisher;

Expand Down
6 changes: 6 additions & 0 deletions rmw_fastrtps_cpp/src/rmw_subscription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ rmw_create_subscription(
rmw_subscription->data = info;
rmw_subscription->topic_name =
reinterpret_cast<const char *>(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<char *>(rmw_subscription->topic_name), topic_name, strlen(topic_name) + 1);
return rmw_subscription;

Expand Down

0 comments on commit 4e22c3e

Please sign in to comment.