Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve error message on Windows when rcutils_get_env fails #200

Merged
merged 1 commit into from
Jan 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/get_env.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ extern "C"
#include "rcutils/get_env.h"

#ifdef _WIN32
# include <errno.h>
# define WINDOWS_ENV_BUFFER_SIZE 2048
static char __env_buffer[WINDOWS_ENV_BUFFER_SIZE];
#endif // _WIN32
Expand All @@ -41,11 +42,18 @@ rcutils_get_env(const char * env_name, const char ** env_value)
#ifdef _WIN32
size_t required_size;
errno_t ret = getenv_s(&required_size, __env_buffer, sizeof(__env_buffer), env_name);
if (ret != 0) {
return "unable to read environment variable";
switch (ret) {
case 0:
__env_buffer[WINDOWS_ENV_BUFFER_SIZE - 1] = '\0';
*env_value = __env_buffer;
break;
case EINVAL:
return "invalid arguments when reading environment variable";
case ERANGE:
return "insufficient buffer size to read environment variable";
default:
return "unknown error code reading environment variable";
}
__env_buffer[WINDOWS_ENV_BUFFER_SIZE - 1] = '\0';
*env_value = __env_buffer;
#else
*env_value = getenv(env_name);
if (NULL == *env_value) {
Expand Down