use _dupenv_s, strerror_s and strerror_r

This commit is contained in:
Dirk Thomas 2015-08-16 13:09:45 -07:00
parent adfee27b59
commit e04a2db825
2 changed files with 22 additions and 3 deletions

View file

@ -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) number_of_subscriptions_(0), number_of_timers_(0), number_of_services_(0)
{ {
size_t domain_id = 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) { if (ros_domain_id) {
unsigned long number = strtoul(ros_domain_id, NULL, 0); unsigned long number = strtoul(ros_domain_id, NULL, 0);
if (number == (std::numeric_limits<unsigned long>::max)()) { if (number == (std::numeric_limits<unsigned long>::max)()) {
@ -57,6 +64,11 @@ Node::Node(const std::string & node_name, context::Context::SharedPtr context)
} }
domain_id = static_cast<size_t>(number); domain_id = static_cast<size_t>(number);
} }
#ifdef _WIN32
if (ros_domain_id) {
free(ros_domain_id);
}
#endif
auto node = rmw_create_node(name_.c_str(), domain_id); auto node = rmw_create_node(name_.c_str(), domain_id);
if (!node) { if (!node) {

View file

@ -24,6 +24,7 @@
#include <csignal> #include <csignal>
#include <cstring> #include <cstring>
#include <mutex> #include <mutex>
#include <string.h>
#include <thread> #include <thread>
#include <rmw/macros.h> #include <rmw/macros.h>
@ -121,11 +122,17 @@ init(int argc, char * argv[])
if (::old_signal_handler == SIG_ERR) if (::old_signal_handler == SIG_ERR)
#endif #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* // *INDENT-OFF*
throw std::runtime_error( throw std::runtime_error(
std::string("Failed to set SIGINT signal handler: (" + std::to_string(errno) + ")") + std::string("Failed to set SIGINT signal handler: (" + std::to_string(errno) + ")") +
// TODO(wjwwood): use strerror_r on POSIX and strerror_s on Windows. error_string);
std::strerror(errno));
// *INDENT-ON* // *INDENT-ON*
} }
} }