From ef722790fc615d740053c7136616c5f4d5bbd8e2 Mon Sep 17 00:00:00 2001 From: Sam Roberts Date: Tue, 10 Oct 2017 16:17:34 -0700 Subject: [PATCH] src: use SafeGetenv() for NODE_REDIRECT_WARNINGS Mutations of the environment can invalidate pointers to environment variables, so make `secure_getenv()` copy them out instead of returning pointers. This is the part of https://github.com/nodejs/node/pull/11051 that applies to be11fb48d2f1b3f6. This part wasn't backported to 6.x when #11051 was backported because the semver-minor introduction of NODE_REDIRECT_WARNINGS hadn't been backported yet. Now that the env var is backported, this last bit of #11051 is needed. PR-URL: https://github.com/nodejs/node/pull/12677 Reviewed-By: Gibson Fahnestock Reviewed-By: Michael Dawson --- src/node.cc | 7 +++---- src/node_config.cc | 7 ++++--- src/node_internals.h | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/node.cc b/src/node.cc index c5be22a7251330..4d21f971117dbc 100644 --- a/src/node.cc +++ b/src/node.cc @@ -207,7 +207,7 @@ bool config_preserve_symlinks = false; bool config_expose_internals = false; // Set in node.cc by ParseArgs when --redirect-warnings= is used. -const char* config_warning_file; +std::string config_warning_file; // NOLINT(runtime/string) // process-relative uptime base, initialized at start-up static double prog_start_time; @@ -4410,9 +4410,8 @@ void Init(int* argc, if (openssl_config.empty()) SafeGetenv("OPENSSL_CONF", &openssl_config); - if (auto redirect_warnings = secure_getenv("NODE_REDIRECT_WARNINGS")) { - config_warning_file = redirect_warnings; - } + if (config_warning_file.empty()) + SafeGetenv("NODE_REDIRECT_WARNINGS", &config_warning_file); // Parse a few arguments which are specific to Node. int v8_argc; diff --git a/src/node_config.cc b/src/node_config.cc index c80e3f640da99a..0e6184709642ea 100644 --- a/src/node_config.cc +++ b/src/node_config.cc @@ -49,11 +49,12 @@ void InitConfig(Local target, if (config_expose_internals) READONLY_BOOLEAN_PROPERTY("exposeInternals"); - if (config_warning_file != nullptr) { + if (!config_warning_file.empty()) { Local name = OneByteString(env->isolate(), "warningFile"); Local value = String::NewFromUtf8(env->isolate(), - config_warning_file, - v8::NewStringType::kNormal) + config_warning_file.data(), + v8::NewStringType::kNormal, + config_warning_file.size()) .ToLocalChecked(); target->DefineOwnProperty(env->context(), name, value).FromJust(); } diff --git a/src/node_internals.h b/src/node_internals.h index 642e61c2c26939..adcb7f835a3451 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -52,7 +52,7 @@ extern bool config_expose_internals; // Set in node.cc by ParseArgs when --redirect-warnings= is used. // Used to redirect warning output to a file rather than sending // it to stderr. -extern const char* config_warning_file; +extern std::string config_warning_file; // Forward declaration class Environment;