Skip to content

Commit

Permalink
Fix unprotected use of mutex-guarded variable (#345)
Browse files Browse the repository at this point in the history
Signed-off-by: Emerson Knapp <[email protected]>
  • Loading branch information
emersonknapp authored and ivanpauno committed Jan 17, 2020
1 parent 3e0aea7 commit 3adf852
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions rmw_fastrtps_shared_cpp/src/rmw_get_topic_endpoint_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,26 +181,29 @@ _set_rmw_topic_endpoint_info(
// and hence we must find its name and namespace from the discovered_names and
// discovered_namespace maps
// set node name
const auto & d_name_it = slave_target->discovered_names.find(topic_data.participant_guid);
if (d_name_it != slave_target->discovered_names.end()) {
ret = rmw_topic_endpoint_info_set_node_name(
topic_endpoint_info, d_name_it->second.c_str(), allocator);
} else {
ret = rmw_topic_endpoint_info_set_node_name(
topic_endpoint_info, "_NODE_NAME_UNKNOWN_", allocator);
}
if (ret != RMW_RET_OK) {
return ret;
}
// set node namespace
const auto & d_namespace_it =
slave_target->discovered_namespaces.find(topic_data.participant_guid);
if (d_namespace_it != slave_target->discovered_namespaces.end()) {
ret = rmw_topic_endpoint_info_set_node_namespace(
topic_endpoint_info, d_namespace_it->second.c_str(), allocator);
} else {
ret = rmw_topic_endpoint_info_set_node_namespace(
topic_endpoint_info, "_NODE_NAMESPACE_UNKNOWN_", allocator);
{ // Scope for lock guard
std::lock_guard<std::mutex> guard(slave_target->names_mutex_);
const auto & d_name_it = slave_target->discovered_names.find(topic_data.participant_guid);
if (d_name_it != slave_target->discovered_names.end()) {
ret = rmw_topic_endpoint_info_set_node_name(
topic_endpoint_info, d_name_it->second.c_str(), allocator);
} else {
ret = rmw_topic_endpoint_info_set_node_name(
topic_endpoint_info, "_NODE_NAME_UNKNOWN_", allocator);
}
if (ret != RMW_RET_OK) {
return ret;
}
// set node namespace
const auto & d_namespace_it =
slave_target->discovered_namespaces.find(topic_data.participant_guid);
if (d_namespace_it != slave_target->discovered_namespaces.end()) {
ret = rmw_topic_endpoint_info_set_node_namespace(
topic_endpoint_info, d_namespace_it->second.c_str(), allocator);
} else {
ret = rmw_topic_endpoint_info_set_node_namespace(
topic_endpoint_info, "_NODE_NAMESPACE_UNKNOWN_", allocator);
}
}
return ret;
}
Expand Down

0 comments on commit 3adf852

Please sign in to comment.