Skip to content

Commit 8f42148

Browse files
sam-githubMylesBorins
authored andcommitted
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: #12677 PR-URL: #12241 Reviewed-By: Richard Lau <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Daniel Bevenius <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 68f698c commit 8f42148

File tree

1 file changed

+6
-22
lines changed

1 file changed

+6
-22
lines changed

src/node.cc

+6-22
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,7 @@ static bool throw_deprecation = false;
142142
static bool trace_sync_io = false;
143143
static bool track_heap_objects = false;
144144
static const char* eval_string = nullptr;
145-
static unsigned int preload_module_count = 0;
146-
static const char** preload_modules = nullptr;
145+
static std::vector<std::string> preload_modules;
147146
#if HAVE_INSPECTOR
148147
static bool use_inspector = false;
149148
#else
@@ -3328,21 +3327,18 @@ void SetupProcessObject(Environment* env,
33283327
}
33293328

33303329
// -r, --require
3331-
if (preload_module_count) {
3332-
CHECK(preload_modules);
3330+
if (!preload_modules.empty()) {
33333331
Local<Array> array = Array::New(env->isolate());
3334-
for (unsigned int i = 0; i < preload_module_count; ++i) {
3332+
for (unsigned int i = 0; i < preload_modules.size(); ++i) {
33353333
Local<String> module = String::NewFromUtf8(env->isolate(),
3336-
preload_modules[i]);
3334+
preload_modules[i].c_str());
33373335
array->Set(i, module);
33383336
}
33393337
READONLY_PROPERTY(process,
33403338
"_preload_modules",
33413339
array);
33423340

3343-
delete[] preload_modules;
3344-
preload_modules = nullptr;
3345-
preload_module_count = 0;
3341+
preload_modules.clear();
33463342
}
33473343

33483344
// --no-deprecation
@@ -3798,13 +3794,11 @@ static void ParseArgs(int* argc,
37983794
const char** new_exec_argv = new const char*[nargs];
37993795
const char** new_v8_argv = new const char*[nargs];
38003796
const char** new_argv = new const char*[nargs];
3801-
const char** local_preload_modules = new const char*[nargs];
38023797

38033798
for (unsigned int i = 0; i < nargs; ++i) {
38043799
new_exec_argv[i] = nullptr;
38053800
new_v8_argv[i] = nullptr;
38063801
new_argv[i] = nullptr;
3807-
local_preload_modules[i] = nullptr;
38083802
}
38093803

38103804
// exec_argv starts with the first option, the other two start with argv[0].
@@ -3862,7 +3856,7 @@ static void ParseArgs(int* argc,
38623856
exit(9);
38633857
}
38643858
args_consumed += 1;
3865-
local_preload_modules[preload_module_count++] = module;
3859+
preload_modules.push_back(module);
38663860
} else if (strcmp(arg, "--check") == 0 || strcmp(arg, "-c") == 0) {
38673861
syntax_check_only = true;
38683862
} else if (strcmp(arg, "--interactive") == 0 || strcmp(arg, "-i") == 0) {
@@ -3952,16 +3946,6 @@ static void ParseArgs(int* argc,
39523946
memcpy(argv, new_argv, new_argc * sizeof(*argv));
39533947
delete[] new_argv;
39543948
*argc = static_cast<int>(new_argc);
3955-
3956-
// Copy the preload_modules from the local array to an appropriately sized
3957-
// global array.
3958-
if (preload_module_count > 0) {
3959-
CHECK(!preload_modules);
3960-
preload_modules = new const char*[preload_module_count];
3961-
memcpy(preload_modules, local_preload_modules,
3962-
preload_module_count * sizeof(*preload_modules));
3963-
}
3964-
delete[] local_preload_modules;
39653949
}
39663950

39673951

0 commit comments

Comments
 (0)