win: support retrieving empty env variables

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 <info@bnoordhuis.nl>
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
This commit is contained in:
cjihrig 2019-08-10 11:31:20 -04:00
parent 6c9c4fe991
commit 9e80057051
No known key found for this signature in database
GPG Key ID: 7434390BDBE9B9C5
2 changed files with 12 additions and 5 deletions

View File

@ -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 */

View File

@ -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);