Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 521fed1

Browse filesBrowse files
jasnelltargos
authored andcommitted
src: improve error handling in node_os by removing ToLocalChecked
PR-URL: #56888 Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
1 parent c9a99df commit 521fed1
Copy full SHA for 521fed1

File tree

Expand file treeCollapse file tree

1 file changed

+55
-48
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+55
-48
lines changed
Open diff view settings
Collapse file

‎src/node_os.cc‎

Copy file name to clipboardExpand all lines: src/node_os.cc
+55-48Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ using v8::Isolate;
5050
using v8::Local;
5151
using v8::LocalVector;
5252
using v8::MaybeLocal;
53+
using v8::Name;
5354
using v8::NewStringType;
5455
using v8::Null;
5556
using v8::Number;
@@ -71,8 +72,10 @@ static void GetHostname(const FunctionCallbackInfo<Value>& args) {
7172
return args.GetReturnValue().SetUndefined();
7273
}
7374

74-
args.GetReturnValue().Set(
75-
String::NewFromUtf8(env->isolate(), buf).ToLocalChecked());
75+
Local<Value> ret;
76+
if (String::NewFromUtf8(env->isolate(), buf).ToLocal(&ret)) {
77+
args.GetReturnValue().Set(ret);
78+
}
7679
}
7780

7881
static void GetOSInformation(const FunctionCallbackInfo<Value>& args) {
@@ -87,15 +90,18 @@ static void GetOSInformation(const FunctionCallbackInfo<Value>& args) {
8790
}
8891

8992
// [sysname, version, release, machine]
90-
Local<Value> osInformation[] = {
91-
String::NewFromUtf8(env->isolate(), info.sysname).ToLocalChecked(),
92-
String::NewFromUtf8(env->isolate(), info.version).ToLocalChecked(),
93-
String::NewFromUtf8(env->isolate(), info.release).ToLocalChecked(),
94-
String::NewFromUtf8(env->isolate(), info.machine).ToLocalChecked()};
95-
96-
args.GetReturnValue().Set(Array::New(env->isolate(),
97-
osInformation,
98-
arraysize(osInformation)));
93+
Local<Value> osInformation[4];
94+
if (String::NewFromUtf8(env->isolate(), info.sysname)
95+
.ToLocal(&osInformation[0]) &&
96+
String::NewFromUtf8(env->isolate(), info.version)
97+
.ToLocal(&osInformation[1]) &&
98+
String::NewFromUtf8(env->isolate(), info.release)
99+
.ToLocal(&osInformation[2]) &&
100+
String::NewFromUtf8(env->isolate(), info.machine)
101+
.ToLocal(&osInformation[3])) {
102+
args.GetReturnValue().Set(
103+
Array::New(env->isolate(), osInformation, arraysize(osInformation)));
104+
}
99105
}
100106

101107
static void GetCPUInfo(const FunctionCallbackInfo<Value>& args) {
@@ -204,7 +210,10 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) {
204210
// to assume UTF8 as the default as well. It’s what people will expect if
205211
// they name the interface from any input that uses UTF-8, which should be
206212
// the most frequent case by far these days.)
207-
name = String::NewFromUtf8(isolate, raw_name).ToLocalChecked();
213+
if (!String::NewFromUtf8(isolate, raw_name).ToLocal(&name)) {
214+
// Error occurred creating the string.
215+
return;
216+
}
208217

209218
snprintf(mac.data(),
210219
mac.size(),
@@ -262,11 +271,11 @@ static void GetHomeDirectory(const FunctionCallbackInfo<Value>& args) {
262271
return args.GetReturnValue().SetUndefined();
263272
}
264273

265-
Local<String> home = String::NewFromUtf8(env->isolate(),
266-
buf,
267-
NewStringType::kNormal,
268-
len).ToLocalChecked();
269-
args.GetReturnValue().Set(home);
274+
Local<String> home;
275+
if (String::NewFromUtf8(env->isolate(), buf, NewStringType::kNormal, len)
276+
.ToLocal(&home)) {
277+
args.GetReturnValue().Set(home);
278+
}
270279
}
271280

272281

@@ -311,43 +320,41 @@ static void GetUserInfo(const FunctionCallbackInfo<Value>& args) {
311320
Local<Value> gid = Number::New(env->isolate(), pwd.gid);
312321
#endif
313322

314-
MaybeLocal<Value> username = StringBytes::Encode(env->isolate(),
315-
pwd.username,
316-
encoding,
317-
&error);
318-
MaybeLocal<Value> homedir = StringBytes::Encode(env->isolate(),
319-
pwd.homedir,
320-
encoding,
321-
&error);
322-
MaybeLocal<Value> shell;
323-
324-
if (pwd.shell == nullptr)
325-
shell = Null(env->isolate());
326-
else
327-
shell = StringBytes::Encode(env->isolate(), pwd.shell, encoding, &error);
328-
329-
if (username.IsEmpty() || homedir.IsEmpty() || shell.IsEmpty()) {
323+
Local<Value> username;
324+
if (!StringBytes::Encode(env->isolate(), pwd.username, encoding, &error)
325+
.ToLocal(&username)) {
326+
CHECK(!error.IsEmpty());
327+
env->isolate()->ThrowException(error);
328+
return;
329+
}
330+
331+
Local<Value> homedir;
332+
if (!StringBytes::Encode(env->isolate(), pwd.homedir, encoding, &error)
333+
.ToLocal(&homedir)) {
334+
CHECK(!error.IsEmpty());
335+
env->isolate()->ThrowException(error);
336+
return;
337+
}
338+
339+
Local<Value> shell = Null(env->isolate());
340+
if (pwd.shell != nullptr &&
341+
!StringBytes::Encode(env->isolate(), pwd.shell, encoding, &error)
342+
.ToLocal(&shell)) {
330343
CHECK(!error.IsEmpty());
331344
env->isolate()->ThrowException(error);
332345
return;
333346
}
334347

335348
constexpr size_t kRetLength = 5;
336-
std::array<Local<v8::Name>, kRetLength> names = {env->uid_string(),
337-
env->gid_string(),
338-
env->username_string(),
339-
env->homedir_string(),
340-
env->shell_string()};
341-
std::array values = {uid,
342-
gid,
343-
username.ToLocalChecked(),
344-
homedir.ToLocalChecked(),
345-
shell.ToLocalChecked()};
346-
args.GetReturnValue().Set(Object::New(env->isolate(),
347-
Null(env->isolate()),
348-
names.data(),
349-
values.data(),
350-
kRetLength));
349+
Local<Name> names[kRetLength] = {env->uid_string(),
350+
env->gid_string(),
351+
env->username_string(),
352+
env->homedir_string(),
353+
env->shell_string()};
354+
Local<Value> values[kRetLength] = {uid, gid, username, homedir, shell};
355+
356+
args.GetReturnValue().Set(Object::New(
357+
env->isolate(), Null(env->isolate()), &names[0], &values[0], kRetLength));
351358
}
352359

353360

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.