Skip to content

Commit fd6efbe

Browse files
bzozBethGriggs
authored andcommitted
process: correctly parse Unicode in NODE_OPTIONS
Fixes an issue on Windows, where Unicode in NODE_OPTIONS was not parsed correctly. Fixes: #34399 PR-URL: #34476 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Denys Otrishko <[email protected]>
1 parent 6720624 commit fd6efbe

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/node_credentials.cc

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,20 @@ bool SafeGetenv(const char* key, std::string* text, Environment* env) {
5656

5757
{
5858
Mutex::ScopedLock lock(per_process::env_var_mutex);
59-
if (const char* value = getenv(key)) {
60-
*text = value;
59+
60+
size_t init_sz = 256;
61+
MaybeStackBuffer<char, 256> val;
62+
int ret = uv_os_getenv(key, *val, &init_sz);
63+
64+
if (ret == UV_ENOBUFS) {
65+
// Buffer is not large enough, reallocate to the updated init_sz
66+
// and fetch env value again.
67+
val.AllocateSufficientStorage(init_sz);
68+
ret = uv_os_getenv(key, *val, &init_sz);
69+
}
70+
71+
if (ret >= 0) { // Env key value fetch success.
72+
*text = *val;
6173
return true;
6274
}
6375
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
// Flags: --expose-internals
3+
require('../common');
4+
const { getOptionValue } = require('internal/options');
5+
const assert = require('assert');
6+
const cp = require('child_process');
7+
8+
const expected_redirect_value = 'foó';
9+
10+
if (process.argv.length === 2) {
11+
const NODE_OPTIONS = `--redirect-warnings=${expected_redirect_value}`;
12+
const result = cp.spawnSync(process.argv0,
13+
['--expose-internals', __filename, 'test'],
14+
{
15+
env: {
16+
...process.env,
17+
NODE_OPTIONS
18+
},
19+
stdio: 'inherit'
20+
});
21+
assert.strictEqual(result.status, 0);
22+
} else {
23+
const redirect_value = getOptionValue('--redirect-warnings');
24+
console.log(`--redirect-warings=${redirect_value}`);
25+
assert.strictEqual(redirect_value, expected_redirect_value);
26+
}

0 commit comments

Comments
 (0)