From 69c13293758a91912e11b812e5e196a61301abf3 Mon Sep 17 00:00:00 2001 From: Sam Roberts Date: Wed, 5 Apr 2017 11:45:52 -0700 Subject: [PATCH] src: use a std::vector for preload_modules A dynamically allocated array was being used, simplify the memory management by using std::vector. Backport-PR-URL: https://github.com/nodejs/node/pull/12677 PR-URL: https://github.com/nodejs/node/pull/12241 Reviewed-By: Richard Lau Reviewed-By: James M Snell Reviewed-By: Daniel Bevenius Reviewed-By: Colin Ihrig Reviewed-By: Anna Henningsen --- src/node.cc | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/src/node.cc b/src/node.cc index 4d21f971117dbc..3214d58f48fcf3 100644 --- a/src/node.cc +++ b/src/node.cc @@ -142,8 +142,7 @@ static bool throw_deprecation = false; static bool trace_sync_io = false; static bool track_heap_objects = false; static const char* eval_string = nullptr; -static unsigned int preload_module_count = 0; -static const char** preload_modules = nullptr; +static std::vector preload_modules; #if HAVE_INSPECTOR static bool use_inspector = false; #else @@ -3328,21 +3327,18 @@ void SetupProcessObject(Environment* env, } // -r, --require - if (preload_module_count) { - CHECK(preload_modules); + if (!preload_modules.empty()) { Local array = Array::New(env->isolate()); - for (unsigned int i = 0; i < preload_module_count; ++i) { + for (unsigned int i = 0; i < preload_modules.size(); ++i) { Local module = String::NewFromUtf8(env->isolate(), - preload_modules[i]); + preload_modules[i].c_str()); array->Set(i, module); } READONLY_PROPERTY(process, "_preload_modules", array); - delete[] preload_modules; - preload_modules = nullptr; - preload_module_count = 0; + preload_modules.clear(); } // --no-deprecation @@ -3798,13 +3794,11 @@ static void ParseArgs(int* argc, const char** new_exec_argv = new const char*[nargs]; const char** new_v8_argv = new const char*[nargs]; const char** new_argv = new const char*[nargs]; - const char** local_preload_modules = new const char*[nargs]; for (unsigned int i = 0; i < nargs; ++i) { new_exec_argv[i] = nullptr; new_v8_argv[i] = nullptr; new_argv[i] = nullptr; - local_preload_modules[i] = nullptr; } // exec_argv starts with the first option, the other two start with argv[0]. @@ -3862,7 +3856,7 @@ static void ParseArgs(int* argc, exit(9); } args_consumed += 1; - local_preload_modules[preload_module_count++] = module; + preload_modules.push_back(module); } else if (strcmp(arg, "--check") == 0 || strcmp(arg, "-c") == 0) { syntax_check_only = true; } else if (strcmp(arg, "--interactive") == 0 || strcmp(arg, "-i") == 0) { @@ -3952,16 +3946,6 @@ static void ParseArgs(int* argc, memcpy(argv, new_argv, new_argc * sizeof(*argv)); delete[] new_argv; *argc = static_cast(new_argc); - - // Copy the preload_modules from the local array to an appropriately sized - // global array. - if (preload_module_count > 0) { - CHECK(!preload_modules); - preload_modules = new const char*[preload_module_count]; - memcpy(preload_modules, local_preload_modules, - preload_module_count * sizeof(*preload_modules)); - } - delete[] local_preload_modules; }