From e04a2db825d63b9ca3d027dea6c99663592484d2 Mon Sep 17 00:00:00 2001 From: Dirk Thomas Date: Sun, 16 Aug 2015 13:09:45 -0700 Subject: [PATCH] use _dupenv_s, strerror_s and strerror_r --- rclcpp/include/rclcpp/node_impl.hpp | 14 +++++++++++++- rclcpp/include/rclcpp/utilities.hpp | 11 +++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/rclcpp/include/rclcpp/node_impl.hpp b/rclcpp/include/rclcpp/node_impl.hpp index fb13bef..7752e38 100644 --- a/rclcpp/include/rclcpp/node_impl.hpp +++ b/rclcpp/include/rclcpp/node_impl.hpp @@ -49,7 +49,14 @@ Node::Node(const std::string & node_name, context::Context::SharedPtr context) number_of_subscriptions_(0), number_of_timers_(0), number_of_services_(0) { size_t domain_id = 0; - char * ros_domain_id = getenv("ROS_DOMAIN_ID"); + char * ros_domain_id = nullptr; + const char * env_var = "ROS_DOMAIN_ID"; +#ifndef _WIN32 + getenv(env_var); +#else + size_t ros_domain_id_size; + _dupenv_s(&ros_domain_id, &ros_domain_id_size, env_var); +#endif if (ros_domain_id) { unsigned long number = strtoul(ros_domain_id, NULL, 0); if (number == (std::numeric_limits::max)()) { @@ -57,6 +64,11 @@ Node::Node(const std::string & node_name, context::Context::SharedPtr context) } domain_id = static_cast(number); } +#ifdef _WIN32 + if (ros_domain_id) { + free(ros_domain_id); + } +#endif auto node = rmw_create_node(name_.c_str(), domain_id); if (!node) { diff --git a/rclcpp/include/rclcpp/utilities.hpp b/rclcpp/include/rclcpp/utilities.hpp index e82cb44..c0e067c 100644 --- a/rclcpp/include/rclcpp/utilities.hpp +++ b/rclcpp/include/rclcpp/utilities.hpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -121,11 +122,17 @@ init(int argc, char * argv[]) if (::old_signal_handler == SIG_ERR) #endif { + const size_t error_length = 1024; + char error_string[error_length]; +#ifndef _WIN32 + strerror_r(errno, error_string, error_length); +#else + strerror_s(error_string, error_length, errno); +#endif // *INDENT-OFF* throw std::runtime_error( std::string("Failed to set SIGINT signal handler: (" + std::to_string(errno) + ")") + - // TODO(wjwwood): use strerror_r on POSIX and strerror_s on Windows. - std::strerror(errno)); + error_string); // *INDENT-ON* } }