Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process: fix reading zero-length env vars on win32 #18463

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2598,6 +2598,7 @@ static void EnvGetter(Local<Name> property,
#else // _WIN32
node::TwoByteValue key(isolate, property);
WCHAR buffer[32767]; // The maximum size allowed for environment variables.
SetLastError(ERROR_SUCCESS);
DWORD result = GetEnvironmentVariableW(reinterpret_cast<WCHAR*>(*key),
buffer,
arraysize(buffer));
Expand Down Expand Up @@ -2646,6 +2647,7 @@ static void EnvQuery(Local<Name> property,
#else // _WIN32
node::TwoByteValue key(info.GetIsolate(), property);
WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
SetLastError(ERROR_SUCCESS);
if (GetEnvironmentVariableW(key_ptr, nullptr, 0) > 0 ||
GetLastError() == ERROR_SUCCESS) {
rc = 0;
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-process-env-windows-error-reset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';
require('../common');
const assert = require('assert');

// This checks that after accessing a missing env var, a subsequent
// env read will succeed even for empty variables.

{
process.env.FOO = '';
process.env.NONEXISTENT_ENV_VAR;
const foo = process.env.FOO;

assert.strictEqual(foo, '');
}

{
process.env.FOO = '';
process.env.NONEXISTENT_ENV_VAR;
const hasFoo = 'FOO' in process.env;

assert.strictEqual(hasFoo, true);
}