Skip to content

Commit ae21fca

Browse files
joyeecheungaddaleax
authored andcommitted
process: normalize process.execPath in CreateProcessObject()
Directly normalize `process.execPath` using `uv_fs_realpath` on OpenBSD before serializing it into the process object, instead of using `require('fs')` to normalize and override the path in `bootstrap/node.js`. PR-URL: #26002 Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent dfd47aa commit ae21fca

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

lib/internal/bootstrap/node.js

-7
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,6 @@ if (browserGlobals) {
187187

188188
setupDOMException();
189189

190-
// On OpenBSD process.execPath will be relative unless we
191-
// get the full path before process.execPath is used.
192-
if (process.platform === 'openbsd') {
193-
const { realpathSync } = NativeModule.require('fs');
194-
process.execPath = realpathSync.native(process.execPath);
195-
}
196-
197190
Object.defineProperty(process, 'argv0', {
198191
enumerable: true,
199192
configurable: false,

src/node_process_object.cc

+26-13
Original file line numberDiff line numberDiff line change
@@ -285,21 +285,34 @@ MaybeLocal<Object> CreateProcessObject(
285285

286286
// process.execPath
287287
{
288-
size_t exec_path_len = 2 * PATH_MAX;
289-
std::vector<char> exec_path(exec_path_len);
290-
Local<String> exec_path_value;
291-
if (uv_exepath(exec_path.data(), &exec_path_len) == 0) {
292-
exec_path_value = String::NewFromUtf8(env->isolate(),
293-
exec_path.data(),
294-
NewStringType::kInternalized,
295-
exec_path_len).ToLocalChecked();
288+
char exec_path_buf[2 * PATH_MAX];
289+
size_t exec_path_len = sizeof(exec_path_buf);
290+
std::string exec_path;
291+
if (uv_exepath(exec_path_buf, &exec_path_len) == 0) {
292+
exec_path = std::string(exec_path_buf, exec_path_len);
296293
} else {
297-
exec_path_value = String::NewFromUtf8(env->isolate(), args[0].c_str(),
298-
NewStringType::kInternalized).ToLocalChecked();
294+
exec_path = args[0];
299295
}
300-
process->Set(env->context(),
301-
FIXED_ONE_BYTE_STRING(env->isolate(), "execPath"),
302-
exec_path_value).FromJust();
296+
// On OpenBSD process.execPath will be relative unless we
297+
// get the full path before process.execPath is used.
298+
#if defined(__OpenBSD__)
299+
uv_fs_t req;
300+
req.ptr = nullptr;
301+
if (0 ==
302+
uv_fs_realpath(env->event_loop(), &req, exec_path.c_str(), nullptr)) {
303+
CHECK_NOT_NULL(req.ptr);
304+
exec_path = std::string(static_cast<char*>(req.ptr));
305+
}
306+
#endif
307+
process
308+
->Set(env->context(),
309+
FIXED_ONE_BYTE_STRING(env->isolate(), "execPath"),
310+
String::NewFromUtf8(env->isolate(),
311+
exec_path.c_str(),
312+
NewStringType::kInternalized,
313+
exec_path.size())
314+
.ToLocalChecked())
315+
.FromJust();
303316
}
304317

305318
// process.debugPort

0 commit comments

Comments
 (0)