Skip to content

Commit 6ca5223

Browse files
jasnelladuh95
authored andcommitted
src: make more error handling improvements
PR-URL: #57262 Reviewed-By: Daeyeon Jeong <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]>
1 parent 523fe76 commit 6ca5223

File tree

8 files changed

+131
-85
lines changed

8 files changed

+131
-85
lines changed

src/env.cc

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ using v8::HeapProfiler;
5050
using v8::HeapSpaceStatistics;
5151
using v8::Integer;
5252
using v8::Isolate;
53+
using v8::JustVoid;
5354
using v8::Local;
5455
using v8::Maybe;
5556
using v8::MaybeLocal;
5657
using v8::NewStringType;
58+
using v8::Nothing;
5759
using v8::Number;
5860
using v8::Object;
5961
using v8::ObjectTemplate;
@@ -1592,54 +1594,66 @@ Local<Value> Environment::GetNow() {
15921594
return Number::New(isolate(), static_cast<double>(now));
15931595
}
15941596

1595-
void CollectExceptionInfo(Environment* env,
1596-
Local<Object> obj,
1597-
int errorno,
1598-
const char* err_string,
1599-
const char* syscall,
1600-
const char* message,
1601-
const char* path,
1602-
const char* dest) {
1603-
obj->Set(env->context(),
1604-
env->errno_string(),
1605-
Integer::New(env->isolate(), errorno)).Check();
1606-
1607-
obj->Set(env->context(), env->code_string(),
1608-
OneByteString(env->isolate(), err_string)).Check();
1609-
1610-
if (message != nullptr) {
1611-
obj->Set(env->context(), env->message_string(),
1612-
OneByteString(env->isolate(), message)).Check();
1597+
Maybe<void> CollectExceptionInfo(Environment* env,
1598+
Local<Object> obj,
1599+
int errorno,
1600+
const char* err_string,
1601+
const char* syscall,
1602+
const char* message,
1603+
const char* path,
1604+
const char* dest) {
1605+
if (obj->Set(env->context(),
1606+
env->errno_string(),
1607+
Integer::New(env->isolate(), errorno))
1608+
.IsNothing() ||
1609+
obj->Set(env->context(),
1610+
env->code_string(),
1611+
OneByteString(env->isolate(), err_string))
1612+
.IsNothing() ||
1613+
(message != nullptr && obj->Set(env->context(),
1614+
env->message_string(),
1615+
OneByteString(env->isolate(), message))
1616+
.IsNothing())) {
1617+
return Nothing<void>();
16131618
}
16141619

16151620
Local<Value> path_buffer;
16161621
if (path != nullptr) {
1617-
path_buffer =
1618-
Buffer::Copy(env->isolate(), path, strlen(path)).ToLocalChecked();
1619-
obj->Set(env->context(), env->path_string(), path_buffer).Check();
1622+
if (!Buffer::Copy(env->isolate(), path, strlen(path))
1623+
.ToLocal(&path_buffer) ||
1624+
obj->Set(env->context(), env->path_string(), path_buffer).IsNothing()) {
1625+
return Nothing<void>();
1626+
}
16201627
}
16211628

16221629
Local<Value> dest_buffer;
16231630
if (dest != nullptr) {
1624-
dest_buffer =
1625-
Buffer::Copy(env->isolate(), dest, strlen(dest)).ToLocalChecked();
1626-
obj->Set(env->context(), env->dest_string(), dest_buffer).Check();
1631+
if (!Buffer::Copy(env->isolate(), dest, strlen(dest))
1632+
.ToLocal(&dest_buffer) ||
1633+
obj->Set(env->context(), env->dest_string(), dest_buffer).IsNothing()) {
1634+
return Nothing<void>();
1635+
}
16271636
}
16281637

16291638
if (syscall != nullptr) {
1630-
obj->Set(env->context(), env->syscall_string(),
1631-
OneByteString(env->isolate(), syscall)).Check();
1639+
if (obj->Set(env->context(),
1640+
env->syscall_string(),
1641+
OneByteString(env->isolate(), syscall))
1642+
.IsNothing()) {
1643+
return Nothing<void>();
1644+
}
16321645
}
1646+
1647+
return JustVoid();
16331648
}
16341649

1635-
void Environment::CollectUVExceptionInfo(Local<Value> object,
1636-
int errorno,
1637-
const char* syscall,
1638-
const char* message,
1639-
const char* path,
1640-
const char* dest) {
1641-
if (!object->IsObject() || errorno == 0)
1642-
return;
1650+
Maybe<void> Environment::CollectUVExceptionInfo(Local<Value> object,
1651+
int errorno,
1652+
const char* syscall,
1653+
const char* message,
1654+
const char* path,
1655+
const char* dest) {
1656+
if (!object->IsObject() || errorno == 0) return JustVoid();
16431657

16441658
Local<Object> obj = object.As<Object>();
16451659
const char* err_string = uv_err_name(errorno);
@@ -1648,7 +1662,7 @@ void Environment::CollectUVExceptionInfo(Local<Value> object,
16481662
message = uv_strerror(errorno);
16491663
}
16501664

1651-
CollectExceptionInfo(
1665+
return CollectExceptionInfo(
16521666
this, obj, errorno, err_string, syscall, message, path, dest);
16531667
}
16541668

src/env.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -784,12 +784,12 @@ class Environment final : public MemoryRetainer {
784784

785785
inline performance::PerformanceState* performance_state();
786786

787-
void CollectUVExceptionInfo(v8::Local<v8::Value> context,
788-
int errorno,
789-
const char* syscall = nullptr,
790-
const char* message = nullptr,
791-
const char* path = nullptr,
792-
const char* dest = nullptr);
787+
v8::Maybe<void> CollectUVExceptionInfo(v8::Local<v8::Value> context,
788+
int errorno,
789+
const char* syscall = nullptr,
790+
const char* message = nullptr,
791+
const char* path = nullptr,
792+
const char* dest = nullptr);
793793

794794
// If this flag is set, calls into JS (if they would be observable
795795
// from userland) must be avoided. This flag does not indicate whether

src/node_binding.cc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -685,8 +685,9 @@ void GetLinkedBinding(const FunctionCallbackInfo<Value>& args) {
685685

686686
Local<Object> module = Object::New(env->isolate());
687687
Local<Object> exports = Object::New(env->isolate());
688-
Local<String> exports_prop = env->exports_string();
689-
module->Set(env->context(), exports_prop, exports).Check();
688+
if (module->Set(env->context(), env->exports_string(), exports).IsNothing()) {
689+
return;
690+
}
690691

691692
if (mod->nm_context_register_func != nullptr) {
692693
mod->nm_context_register_func(
@@ -698,10 +699,11 @@ void GetLinkedBinding(const FunctionCallbackInfo<Value>& args) {
698699
env, "Linked binding has no declared entry point.");
699700
}
700701

701-
auto effective_exports =
702-
module->Get(env->context(), exports_prop).ToLocalChecked();
703-
704-
args.GetReturnValue().Set(effective_exports);
702+
Local<Value> effective_exports;
703+
if (module->Get(env->context(), env->exports_string())
704+
.ToLocal(&effective_exports)) {
705+
args.GetReturnValue().Set(effective_exports);
706+
}
705707
}
706708

707709
// Call built-in bindings' _register_<module name> function to

src/node_errors.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,9 +1215,12 @@ void TriggerUncaughtException(Isolate* isolate,
12151215
// monkey-patchable.
12161216
Local<Object> process_object = env->process_object();
12171217
Local<String> fatal_exception_string = env->fatal_exception_string();
1218-
Local<Value> fatal_exception_function =
1219-
process_object->Get(env->context(),
1220-
fatal_exception_string).ToLocalChecked();
1218+
Local<Value> fatal_exception_function;
1219+
if (!process_object->Get(env->context(), fatal_exception_string)
1220+
.ToLocal(&fatal_exception_function)) {
1221+
// V8 will have scheduled a superseding error to throw
1222+
return;
1223+
}
12211224
// If the exception happens before process._fatalException is attached
12221225
// during bootstrap, or if the user has patched it incorrectly, exit
12231226
// the current Node.js instance.

src/node_os.cc

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ static void GetHostname(const FunctionCallbackInfo<Value>& args) {
6767

6868
if (r != 0) {
6969
CHECK_GE(args.Length(), 1);
70-
env->CollectUVExceptionInfo(args[args.Length() - 1], r,
71-
"uv_os_gethostname");
72-
return args.GetReturnValue().SetUndefined();
70+
USE(env->CollectUVExceptionInfo(
71+
args[args.Length() - 1], r, "uv_os_gethostname"));
72+
return;
7373
}
7474

7575
Local<Value> ret;
@@ -85,8 +85,9 @@ static void GetOSInformation(const FunctionCallbackInfo<Value>& args) {
8585

8686
if (err != 0) {
8787
CHECK_GE(args.Length(), 1);
88-
env->CollectUVExceptionInfo(args[args.Length() - 1], err, "uv_os_uname");
89-
return args.GetReturnValue().SetUndefined();
88+
USE(env->CollectUVExceptionInfo(
89+
args[args.Length() - 1], err, "uv_os_uname"));
90+
return;
9091
}
9192

9293
// [sysname, version, release, machine]
@@ -159,8 +160,8 @@ static void GetUptime(const FunctionCallbackInfo<Value>& args) {
159160
double uptime;
160161
int err = uv_uptime(&uptime);
161162
if (err != 0) {
162-
env->CollectUVExceptionInfo(args[args.Length() - 1], err, "uv_uptime");
163-
return args.GetReturnValue().SetUndefined();
163+
USE(env->CollectUVExceptionInfo(args[args.Length() - 1], err, "uv_uptime"));
164+
return;
164165
}
165166

166167
args.GetReturnValue().Set(uptime);
@@ -189,14 +190,13 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) {
189190

190191
int err = uv_interface_addresses(&interfaces, &count);
191192

192-
if (err == UV_ENOSYS)
193-
return args.GetReturnValue().SetUndefined();
193+
if (err == UV_ENOSYS) return;
194194

195195
if (err) {
196196
CHECK_GE(args.Length(), 1);
197-
env->CollectUVExceptionInfo(args[args.Length() - 1], errno,
198-
"uv_interface_addresses");
199-
return args.GetReturnValue().SetUndefined();
197+
USE(env->CollectUVExceptionInfo(
198+
args[args.Length() - 1], errno, "uv_interface_addresses"));
199+
return;
200200
}
201201

202202
Local<Value> no_scope_id = Integer::New(isolate, -1);
@@ -267,8 +267,9 @@ static void GetHomeDirectory(const FunctionCallbackInfo<Value>& args) {
267267

268268
if (err) {
269269
CHECK_GE(args.Length(), 1);
270-
env->CollectUVExceptionInfo(args[args.Length() - 1], err, "uv_os_homedir");
271-
return args.GetReturnValue().SetUndefined();
270+
USE(env->CollectUVExceptionInfo(
271+
args[args.Length() - 1], err, "uv_os_homedir"));
272+
return;
272273
}
273274

274275
Local<String> home;
@@ -299,9 +300,9 @@ static void GetUserInfo(const FunctionCallbackInfo<Value>& args) {
299300

300301
if (const int err = uv_os_get_passwd(&pwd)) {
301302
CHECK_GE(args.Length(), 2);
302-
env->CollectUVExceptionInfo(args[args.Length() - 1], err,
303-
"uv_os_get_passwd");
304-
return args.GetReturnValue().SetUndefined();
303+
USE(env->CollectUVExceptionInfo(
304+
args[args.Length() - 1], err, "uv_os_get_passwd"));
305+
return;
305306
}
306307

307308
auto free_passwd = OnScopeLeave([&] { uv_os_free_passwd(&pwd); });
@@ -371,7 +372,10 @@ static void SetPriority(const FunctionCallbackInfo<Value>& args) {
371372

372373
if (err) {
373374
CHECK(args[2]->IsObject());
374-
env->CollectUVExceptionInfo(args[2], err, "uv_os_setpriority");
375+
if (env->CollectUVExceptionInfo(args[2], err, "uv_os_setpriority")
376+
.IsNothing()) {
377+
return;
378+
}
375379
}
376380

377381
args.GetReturnValue().Set(err);
@@ -390,7 +394,7 @@ static void GetPriority(const FunctionCallbackInfo<Value>& args) {
390394

391395
if (err) {
392396
CHECK(args[1]->IsObject());
393-
env->CollectUVExceptionInfo(args[1], err, "uv_os_getpriority");
397+
USE(env->CollectUVExceptionInfo(args[1], err, "uv_os_getpriority"));
394398
return;
395399
}
396400

src/node_wasi.cc

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,37 @@ void WASI::New(const FunctionCallbackInfo<Value>& args) {
133133

134134
Local<Array> stdio = args[3].As<Array>();
135135
CHECK_EQ(stdio->Length(), 3);
136-
options.in = stdio->Get(context, 0).ToLocalChecked()->
137-
Int32Value(context).FromJust();
138-
options.out = stdio->Get(context, 1).ToLocalChecked()->
139-
Int32Value(context).FromJust();
140-
options.err = stdio->Get(context, 2).ToLocalChecked()->
141-
Int32Value(context).FromJust();
136+
137+
Local<Value> val;
138+
int32_t tmp;
139+
if (!stdio->Get(context, 0).ToLocal(&val) ||
140+
!val->Int32Value(context).To(&tmp)) {
141+
return;
142+
}
143+
options.in = tmp;
144+
145+
if (!stdio->Get(context, 1).ToLocal(&val) ||
146+
!val->Int32Value(context).To(&tmp)) {
147+
return;
148+
}
149+
options.out = tmp;
150+
151+
if (!stdio->Get(context, 2).ToLocal(&val) ||
152+
!val->Int32Value(context).To(&tmp)) {
153+
return;
154+
}
155+
options.err = tmp;
142156

143157
options.fd_table_size = 3;
144158
options.argc = argc;
145159
options.argv =
146160
const_cast<const char**>(argc == 0 ? nullptr : new char*[argc]);
147161

148162
for (uint32_t i = 0; i < argc; i++) {
149-
auto arg = argv->Get(context, i).ToLocalChecked();
163+
Local<Value> arg;
164+
if (!argv->Get(context, i).ToLocal(&arg)) {
165+
return;
166+
}
150167
CHECK(arg->IsString());
151168
node::Utf8Value str(env->isolate(), arg);
152169
options.argv[i] = strdup(*str);
@@ -157,7 +174,10 @@ void WASI::New(const FunctionCallbackInfo<Value>& args) {
157174
const uint32_t envc = env_pairs->Length();
158175
options.envp = const_cast<const char**>(new char*[envc + 1]);
159176
for (uint32_t i = 0; i < envc; i++) {
160-
auto pair = env_pairs->Get(context, i).ToLocalChecked();
177+
Local<Value> pair;
178+
if (!env_pairs->Get(context, i).ToLocal(&pair)) {
179+
return;
180+
}
161181
CHECK(pair->IsString());
162182
node::Utf8Value str(env->isolate(), pair);
163183
options.envp[i] = strdup(*str);
@@ -171,8 +191,12 @@ void WASI::New(const FunctionCallbackInfo<Value>& args) {
171191
options.preopens = Calloc<uvwasi_preopen_t>(options.preopenc);
172192
int index = 0;
173193
for (uint32_t i = 0; i < preopens->Length(); i += 2) {
174-
auto mapped = preopens->Get(context, i).ToLocalChecked();
175-
auto real = preopens->Get(context, i + 1).ToLocalChecked();
194+
Local<Value> mapped;
195+
Local<Value> real;
196+
if (!preopens->Get(context, i).ToLocal(&mapped) ||
197+
!preopens->Get(context, i + 1).ToLocal(&real)) {
198+
return;
199+
}
176200
CHECK(mapped->IsString());
177201
CHECK(real->IsString());
178202
node::Utf8Value mapped_path(env->isolate(), mapped);

src/tty_wrap.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ void TTYWrap::New(const FunctionCallbackInfo<Value>& args) {
136136
int err = 0;
137137
new TTYWrap(env, args.This(), fd, &err);
138138
if (err != 0) {
139-
env->CollectUVExceptionInfo(args[1], err, "uv_tty_init");
140-
args.GetReturnValue().SetUndefined();
139+
USE(env->CollectUVExceptionInfo(args[1], err, "uv_tty_init"));
141140
}
142141
}
143142

src/udp_wrap.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,8 @@ void UDPWrap::BufferSize(const FunctionCallbackInfo<Value>& args) {
384384
"uv_send_buffer_size";
385385

386386
if (!args[0]->IsInt32()) {
387-
env->CollectUVExceptionInfo(args[2], UV_EINVAL, uv_func_name);
388-
return args.GetReturnValue().SetUndefined();
387+
USE(env->CollectUVExceptionInfo(args[2], UV_EINVAL, uv_func_name));
388+
return;
389389
}
390390

391391
uv_handle_t* handle = reinterpret_cast<uv_handle_t*>(&wrap->handle_);
@@ -398,8 +398,8 @@ void UDPWrap::BufferSize(const FunctionCallbackInfo<Value>& args) {
398398
err = uv_send_buffer_size(handle, &size);
399399

400400
if (err != 0) {
401-
env->CollectUVExceptionInfo(args[2], err, uv_func_name);
402-
return args.GetReturnValue().SetUndefined();
401+
USE(env->CollectUVExceptionInfo(args[2], err, uv_func_name));
402+
return;
403403
}
404404

405405
args.GetReturnValue().Set(size);

0 commit comments

Comments
 (0)