From 521fed1bacdb8aac1371401e44690be3808fe022 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Tue, 4 Feb 2025 20:20:01 -0800 Subject: [PATCH] src: improve error handling in node_os by removing ToLocalChecked MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/56888 Reviewed-By: Juan José Arboleda Reviewed-By: Michaël Zasso --- src/node_os.cc | 103 ++++++++++++++++++++++++++----------------------- 1 file changed, 55 insertions(+), 48 deletions(-) diff --git a/src/node_os.cc b/src/node_os.cc index edfe34f330f73c..3cb2eded907efb 100644 --- a/src/node_os.cc +++ b/src/node_os.cc @@ -50,6 +50,7 @@ using v8::Isolate; using v8::Local; using v8::LocalVector; using v8::MaybeLocal; +using v8::Name; using v8::NewStringType; using v8::Null; using v8::Number; @@ -71,8 +72,10 @@ static void GetHostname(const FunctionCallbackInfo& args) { return args.GetReturnValue().SetUndefined(); } - args.GetReturnValue().Set( - String::NewFromUtf8(env->isolate(), buf).ToLocalChecked()); + Local ret; + if (String::NewFromUtf8(env->isolate(), buf).ToLocal(&ret)) { + args.GetReturnValue().Set(ret); + } } static void GetOSInformation(const FunctionCallbackInfo& args) { @@ -87,15 +90,18 @@ static void GetOSInformation(const FunctionCallbackInfo& args) { } // [sysname, version, release, machine] - Local osInformation[] = { - String::NewFromUtf8(env->isolate(), info.sysname).ToLocalChecked(), - String::NewFromUtf8(env->isolate(), info.version).ToLocalChecked(), - String::NewFromUtf8(env->isolate(), info.release).ToLocalChecked(), - String::NewFromUtf8(env->isolate(), info.machine).ToLocalChecked()}; - - args.GetReturnValue().Set(Array::New(env->isolate(), - osInformation, - arraysize(osInformation))); + Local osInformation[4]; + if (String::NewFromUtf8(env->isolate(), info.sysname) + .ToLocal(&osInformation[0]) && + String::NewFromUtf8(env->isolate(), info.version) + .ToLocal(&osInformation[1]) && + String::NewFromUtf8(env->isolate(), info.release) + .ToLocal(&osInformation[2]) && + String::NewFromUtf8(env->isolate(), info.machine) + .ToLocal(&osInformation[3])) { + args.GetReturnValue().Set( + Array::New(env->isolate(), osInformation, arraysize(osInformation))); + } } static void GetCPUInfo(const FunctionCallbackInfo& args) { @@ -204,7 +210,10 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo& args) { // to assume UTF8 as the default as well. It’s what people will expect if // they name the interface from any input that uses UTF-8, which should be // the most frequent case by far these days.) - name = String::NewFromUtf8(isolate, raw_name).ToLocalChecked(); + if (!String::NewFromUtf8(isolate, raw_name).ToLocal(&name)) { + // Error occurred creating the string. + return; + } snprintf(mac.data(), mac.size(), @@ -262,11 +271,11 @@ static void GetHomeDirectory(const FunctionCallbackInfo& args) { return args.GetReturnValue().SetUndefined(); } - Local home = String::NewFromUtf8(env->isolate(), - buf, - NewStringType::kNormal, - len).ToLocalChecked(); - args.GetReturnValue().Set(home); + Local home; + if (String::NewFromUtf8(env->isolate(), buf, NewStringType::kNormal, len) + .ToLocal(&home)) { + args.GetReturnValue().Set(home); + } } @@ -311,43 +320,41 @@ static void GetUserInfo(const FunctionCallbackInfo& args) { Local gid = Number::New(env->isolate(), pwd.gid); #endif - MaybeLocal username = StringBytes::Encode(env->isolate(), - pwd.username, - encoding, - &error); - MaybeLocal homedir = StringBytes::Encode(env->isolate(), - pwd.homedir, - encoding, - &error); - MaybeLocal shell; - - if (pwd.shell == nullptr) - shell = Null(env->isolate()); - else - shell = StringBytes::Encode(env->isolate(), pwd.shell, encoding, &error); - - if (username.IsEmpty() || homedir.IsEmpty() || shell.IsEmpty()) { + Local username; + if (!StringBytes::Encode(env->isolate(), pwd.username, encoding, &error) + .ToLocal(&username)) { + CHECK(!error.IsEmpty()); + env->isolate()->ThrowException(error); + return; + } + + Local homedir; + if (!StringBytes::Encode(env->isolate(), pwd.homedir, encoding, &error) + .ToLocal(&homedir)) { + CHECK(!error.IsEmpty()); + env->isolate()->ThrowException(error); + return; + } + + Local shell = Null(env->isolate()); + if (pwd.shell != nullptr && + !StringBytes::Encode(env->isolate(), pwd.shell, encoding, &error) + .ToLocal(&shell)) { CHECK(!error.IsEmpty()); env->isolate()->ThrowException(error); return; } constexpr size_t kRetLength = 5; - std::array, kRetLength> names = {env->uid_string(), - env->gid_string(), - env->username_string(), - env->homedir_string(), - env->shell_string()}; - std::array values = {uid, - gid, - username.ToLocalChecked(), - homedir.ToLocalChecked(), - shell.ToLocalChecked()}; - args.GetReturnValue().Set(Object::New(env->isolate(), - Null(env->isolate()), - names.data(), - values.data(), - kRetLength)); + Local names[kRetLength] = {env->uid_string(), + env->gid_string(), + env->username_string(), + env->homedir_string(), + env->shell_string()}; + Local values[kRetLength] = {uid, gid, username, homedir, shell}; + + args.GetReturnValue().Set(Object::New( + env->isolate(), Null(env->isolate()), &names[0], &values[0], kRetLength)); }