From b801b0372a924a60f97fbd58a2a318f7d56f3fd1 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sat, 15 Dec 2018 21:20:28 +0100 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/25069 Reviewed-By: Richard Lau Reviewed-By: Joyee Cheung Reviewed-By: Colin Ihrig Reviewed-By: Luigi Pinca Reviewed-By: James M Snell --- src/node.cc | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/node.cc b/src/node.cc index c246bb1c720e18..4e88d05510fad1 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1150,22 +1150,23 @@ void SetupProcessObject(Environment* env, SECURITY_REVERSIONS(V) #undef V - size_t exec_path_len = 2 * PATH_MAX; - char* exec_path = new char[exec_path_len]; - Local exec_path_value; - if (uv_exepath(exec_path, &exec_path_len) == 0) { - exec_path_value = String::NewFromUtf8(env->isolate(), - exec_path, - NewStringType::kInternalized, - exec_path_len).ToLocalChecked(); - } else { - exec_path_value = String::NewFromUtf8(env->isolate(), args[0].c_str(), - NewStringType::kInternalized).ToLocalChecked(); + { + size_t exec_path_len = 2 * PATH_MAX; + std::vector exec_path(exec_path_len); + Local exec_path_value; + if (uv_exepath(exec_path.data(), &exec_path_len) == 0) { + exec_path_value = String::NewFromUtf8(env->isolate(), + exec_path.data(), + NewStringType::kInternalized, + exec_path_len).ToLocalChecked(); + } else { + exec_path_value = String::NewFromUtf8(env->isolate(), args[0].c_str(), + NewStringType::kInternalized).ToLocalChecked(); + } + process->Set(env->context(), + FIXED_ONE_BYTE_STRING(env->isolate(), "execPath"), + exec_path_value).FromJust(); } - process->Set(env->context(), - FIXED_ONE_BYTE_STRING(env->isolate(), "execPath"), - exec_path_value).FromJust(); - delete[] exec_path; auto debug_port_string = FIXED_ONE_BYTE_STRING(env->isolate(), "debugPort"); CHECK(process->SetAccessor(env->context(),