From 9e800570516115d7fa52bd3cbc34c4466f5fc709 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sat, 10 Aug 2019 11:31:20 -0400 Subject: [PATCH] win: support retrieving empty env variables MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit adds Windows support for retrieving empty environment variables via uv_os_getenv(). Fixes: https://github.com/libuv/libuv/issues/2413 PR-URL: https://github.com/libuv/libuv/pull/2419 Reviewed-By: Ben Noordhuis Reviewed-By: Saúl Ibarra Corretgé --- src/win/util.c | 8 +++----- test/test-env-vars.c | 9 +++++++++ 2 files changed, 12 insertions(+), 5 deletions(-) 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);