Skip to content

Commit 7e58bac

Browse files
use getenv_s instead of getenv for Windows. (#499) (#501)
(cherry picked from commit 46ab4d4) Signed-off-by: Tomoya Fujita <[email protected]> Co-authored-by: Tomoya Fujita <[email protected]>
1 parent 63a2414 commit 7e58bac

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/env.c

+20-1
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,27 @@ rcutils_get_env(const char * env_name, const char ** env_value)
8181
return "argument env_value is null";
8282
}
8383

84-
// TODO(Suyash458): getenv is deprecated on Windows; consider using getenv_s instead
84+
#ifdef _WIN32
85+
size_t requiredSize = 0;
86+
char *buffer = NULL;
87+
if (getenv_s(&requiredSize, NULL, 0, env_name) == 0 && requiredSize > 0) {
88+
buffer = (char *)malloc(requiredSize * sizeof(char));
89+
if (buffer != NULL) {
90+
if (getenv_s(&requiredSize, buffer, requiredSize, env_name) == 0) {
91+
*env_value = buffer;
92+
} else {
93+
free(buffer);
94+
*env_value = NULL;
95+
}
96+
} else {
97+
*env_value = NULL;
98+
}
99+
} else {
100+
*env_value = NULL;
101+
}
102+
#else
85103
*env_value = getenv(env_name);
104+
#endif
86105

87106
if (NULL == *env_value) {
88107
*env_value = "";

0 commit comments

Comments
 (0)