Skip to content

Commit 0464856

Browse files
Anna Henningsenrefack
authored andcommitted
src: use std::vector for setting up process.execPath
Use `std::vector` as an RAII-style alternative to allocating and deleting raw memory storage. PR-URL: nodejs#25069 Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent df9f162 commit 0464856

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

src/node.cc

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,22 +1086,23 @@ void SetupProcessObject(Environment* env,
10861086
SECURITY_REVERSIONS(V)
10871087
#undef V
10881088

1089-
size_t exec_path_len = 2 * PATH_MAX;
1090-
char* exec_path = new char[exec_path_len];
1091-
Local<String> exec_path_value;
1092-
if (uv_exepath(exec_path, &exec_path_len) == 0) {
1093-
exec_path_value = String::NewFromUtf8(env->isolate(),
1094-
exec_path,
1095-
NewStringType::kInternalized,
1096-
exec_path_len).ToLocalChecked();
1097-
} else {
1098-
exec_path_value = String::NewFromUtf8(env->isolate(), args[0].c_str(),
1099-
NewStringType::kInternalized).ToLocalChecked();
1089+
{
1090+
size_t exec_path_len = 2 * PATH_MAX;
1091+
std::vector<char> exec_path(exec_path_len);
1092+
Local<String> exec_path_value;
1093+
if (uv_exepath(exec_path.data(), &exec_path_len) == 0) {
1094+
exec_path_value = String::NewFromUtf8(env->isolate(),
1095+
exec_path.data(),
1096+
NewStringType::kInternalized,
1097+
exec_path_len).ToLocalChecked();
1098+
} else {
1099+
exec_path_value = String::NewFromUtf8(env->isolate(), args[0].c_str(),
1100+
NewStringType::kInternalized).ToLocalChecked();
1101+
}
1102+
process->Set(env->context(),
1103+
FIXED_ONE_BYTE_STRING(env->isolate(), "execPath"),
1104+
exec_path_value).FromJust();
11001105
}
1101-
process->Set(env->context(),
1102-
FIXED_ONE_BYTE_STRING(env->isolate(), "execPath"),
1103-
exec_path_value).FromJust();
1104-
delete[] exec_path;
11051106

11061107
auto debug_port_string = FIXED_ONE_BYTE_STRING(env->isolate(), "debugPort");
11071108
CHECK(process->SetAccessor(env->context(),

0 commit comments

Comments
 (0)