From a16f0f427eb7bc8c31d8f93fd37bc42cdcf594f8 Mon Sep 17 00:00:00 2001 From: Bartosz Sosnowski Date: Wed, 22 Jul 2020 12:55:11 +0200 Subject: [PATCH] process: correctly parse Unicode in NODE_OPTIONS Fixes an issue on Windows, where Unicode in NODE_OPTIONS was not parsed correctly. Fixes: https://github.com/nodejs/node/issues/34399 PR-URL: https://github.com/nodejs/node/pull/34476 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Gus Caplan Reviewed-By: Denys Otrishko --- src/node_credentials.cc | 16 +++++++++++-- test/parallel/test-unicode-node-options.js | 26 ++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-unicode-node-options.js diff --git a/src/node_credentials.cc b/src/node_credentials.cc index 83db705e10df4f..b07067580af850 100644 --- a/src/node_credentials.cc +++ b/src/node_credentials.cc @@ -56,8 +56,20 @@ bool SafeGetenv(const char* key, std::string* text, Environment* env) { { Mutex::ScopedLock lock(per_process::env_var_mutex); - if (const char* value = getenv(key)) { - *text = value; + + size_t init_sz = 256; + MaybeStackBuffer val; + int ret = uv_os_getenv(key, *val, &init_sz); + + if (ret == UV_ENOBUFS) { + // Buffer is not large enough, reallocate to the updated init_sz + // and fetch env value again. + val.AllocateSufficientStorage(init_sz); + ret = uv_os_getenv(key, *val, &init_sz); + } + + if (ret >= 0) { // Env key value fetch success. + *text = *val; return true; } } diff --git a/test/parallel/test-unicode-node-options.js b/test/parallel/test-unicode-node-options.js new file mode 100644 index 00000000000000..e5a40d118791d3 --- /dev/null +++ b/test/parallel/test-unicode-node-options.js @@ -0,0 +1,26 @@ +'use strict'; +// Flags: --expose-internals +require('../common'); +const { getOptionValue } = require('internal/options'); +const assert = require('assert'); +const cp = require('child_process'); + +const expected_redirect_value = 'foĆ³'; + +if (process.argv.length === 2) { + const NODE_OPTIONS = `--redirect-warnings=${expected_redirect_value}`; + const result = cp.spawnSync(process.argv0, + ['--expose-internals', __filename, 'test'], + { + env: { + ...process.env, + NODE_OPTIONS + }, + stdio: 'inherit' + }); + assert.strictEqual(result.status, 0); +} else { + const redirect_value = getOptionValue('--redirect-warnings'); + console.log(`--redirect-warings=${redirect_value}`); + assert.strictEqual(redirect_value, expected_redirect_value); +}