Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cec3d1e

Browse filesBrowse files
addaleaxMylesBorins
authored andcommitted
process: fix reading zero-length env vars on win32
Up until now, Node did not clear the current error code attempting to read environment variables on Windows. Since checking the error code is the way we distinguish between missing and zero-length environment variables, this could lead to a false positive when the error code was still tainted. In the simplest case, accessing a missing variable and then a zero-length one would lead Node to believe that both calls yielded an error. Before: > process.env.I=''; process.env.Q; process.env.I undefined > process.env.I=''; /*process.env.Q;*/ process.env.I '' After: > process.env.I=''; process.env.Q; process.env.I '' > process.env.I=''; /*process.env.Q;*/ process.env.I '' This only affects Node 8 and above, since before 1aa595e we always constructed a `v8::String::Value` instance for passing the lookup key to the OS, which in in turn always made a heap allocation and therefore reset the error code. PR-URL: #18463 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 450f5f4 commit cec3d1e
Copy full SHA for cec3d1e

File tree

Expand file treeCollapse file tree

2 files changed

+24
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+24
-0
lines changed
Open diff view settings
Collapse file

‎src/node.cc‎

Copy file name to clipboardExpand all lines: src/node.cc
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2916,6 +2916,7 @@ static void EnvGetter(Local<Name> property,
29162916
#else // _WIN32
29172917
node::TwoByteValue key(isolate, property);
29182918
WCHAR buffer[32767]; // The maximum size allowed for environment variables.
2919+
SetLastError(ERROR_SUCCESS);
29192920
DWORD result = GetEnvironmentVariableW(reinterpret_cast<WCHAR*>(*key),
29202921
buffer,
29212922
arraysize(buffer));
@@ -2964,6 +2965,7 @@ static void EnvQuery(Local<Name> property,
29642965
#else // _WIN32
29652966
node::TwoByteValue key(info.GetIsolate(), property);
29662967
WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
2968+
SetLastError(ERROR_SUCCESS);
29672969
if (GetEnvironmentVariableW(key_ptr, nullptr, 0) > 0 ||
29682970
GetLastError() == ERROR_SUCCESS) {
29692971
rc = 0;
Collapse file
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
5+
// This checks that after accessing a missing env var, a subsequent
6+
// env read will succeed even for empty variables.
7+
8+
{
9+
process.env.FOO = '';
10+
process.env.NONEXISTENT_ENV_VAR;
11+
const foo = process.env.FOO;
12+
13+
assert.strictEqual(foo, '');
14+
}
15+
16+
{
17+
process.env.FOO = '';
18+
process.env.NONEXISTENT_ENV_VAR;
19+
const hasFoo = 'FOO' in process.env;
20+
21+
assert.strictEqual(hasFoo, true);
22+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.