Skip to content

Commit f28b8ed

Browse files
authored
improve error message on Windows when rcutils_get_env fails (#200)
Signed-off-by: Dirk Thomas <dirk-thomas@users.noreply.github.com>
1 parent 128b0ce commit f28b8ed

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/get_env.c

+12-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern "C"
2323
#include "rcutils/get_env.h"
2424

2525
#ifdef _WIN32
26+
# include <errno.h>
2627
# define WINDOWS_ENV_BUFFER_SIZE 2048
2728
static char __env_buffer[WINDOWS_ENV_BUFFER_SIZE];
2829
#endif // _WIN32
@@ -41,11 +42,18 @@ rcutils_get_env(const char * env_name, const char ** env_value)
4142
#ifdef _WIN32
4243
size_t required_size;
4344
errno_t ret = getenv_s(&required_size, __env_buffer, sizeof(__env_buffer), env_name);
44-
if (ret != 0) {
45-
return "unable to read environment variable";
45+
switch (ret) {
46+
case 0:
47+
__env_buffer[WINDOWS_ENV_BUFFER_SIZE - 1] = '\0';
48+
*env_value = __env_buffer;
49+
break;
50+
case EINVAL:
51+
return "invalid arguments when reading environment variable";
52+
case ERANGE:
53+
return "insufficient buffer size to read environment variable";
54+
default:
55+
return "unknown error code reading environment variable";
4656
}
47-
__env_buffer[WINDOWS_ENV_BUFFER_SIZE - 1] = '\0';
48-
*env_value = __env_buffer;
4957
#else
5058
*env_value = getenv(env_name);
5159
if (NULL == *env_value) {

0 commit comments

Comments
 (0)