Skip to content

Commit

Permalink
win: support retrieving empty env variables
Browse files Browse the repository at this point in the history
This commit adds Windows support for retrieving empty
environment variables via uv_os_getenv().
  • Loading branch information
cjihrig committed Aug 12, 2019
1 parent 8ed9112 commit 1357a52
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/win/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,7 @@ int uv_os_getenv(const char* name, char* buffer, size_t* size) {
if (r != 0)
return r;

SetLastError(ERROR_SUCCESS);
len = GetEnvironmentVariableW(name_w, var, MAX_ENV_VAR_LENGTH);
uv__free(name_w);
assert(len < MAX_ENV_VAR_LENGTH); /* len does not include the null */
Expand All @@ -1491,7 +1492,9 @@ int uv_os_getenv(const char* name, char* buffer, size_t* size) {
if (r == ERROR_ENVVAR_NOT_FOUND)
return UV_ENOENT;

return uv_translate_sys_error(r);
/* Handle defined, but empty environment variables. */
if (r != ERROR_SUCCESS)
return uv_translate_sys_error(r);
}

/* Check how much space we need */
Expand Down
9 changes: 9 additions & 0 deletions test/test-env-vars.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ TEST_IMPL(env_vars) {
r = uv_os_unsetenv(name);
ASSERT(r == 0);

/* Setting an environment variable to the empty string does not delete it. */
r = uv_os_setenv(name, "");
ASSERT(r == 0);
size = BUF_SIZE;
r = uv_os_getenv(name, buf, &size);
ASSERT(r == 0);
ASSERT(size == 0);
ASSERT(strlen(buf) == 0);

/* Check getting all env variables. */
r = uv_os_setenv(name, "123456789");
ASSERT(r == 0);
Expand Down

0 comments on commit 1357a52

Please sign in to comment.