diff --git a/src/win/util.c b/src/win/util.c index d548c86c8e5..0d3b43f2db7 100644 --- a/src/win/util.c +++ b/src/win/util.c @@ -1481,17 +1481,15 @@ 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 */ if (len == 0) { r = GetLastError(); - - if (r == ERROR_ENVVAR_NOT_FOUND) - return UV_ENOENT; - - return uv_translate_sys_error(r); + if (r != ERROR_SUCCESS) + return uv_translate_sys_error(r); } /* Check how much space we need */ diff --git a/test/test-env-vars.c b/test/test-env-vars.c index d7abb424956..3814699356d 100644 --- a/test/test-env-vars.c +++ b/test/test-env-vars.c @@ -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);