src: make more error handling improvements · nodejs/node@b6091a8 · GitHub
Skip to content

Commit b6091a8

Browse files
jasnellRafaelGSS
authored andcommitted
src: make more error handling improvements
PR-URL: #57262 Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
1 parent 4fe88f8 commit b6091a8

8 files changed

Lines changed: 131 additions & 85 deletions

File tree

src/env.cc

Lines changed: 49 additions & 35 deletions

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
@@ -683,8 +683,9 @@ void GetLinkedBinding(const FunctionCallbackInfo<Value>& args) {
683683

684684
Local<Object> module = Object::New(env->isolate());
685685
Local<Object> exports = Object::New(env->isolate());
686-
Local<String> exports_prop = env->exports_string();
687-
module->Set(env->context(), exports_prop, exports).Check();
686+
if (module->Set(env->context(), env->exports_string(), exports).IsNothing()) {
687+
return;
688+
}
688689

689690
if (mod->nm_context_register_func != nullptr) {
690691
mod->nm_context_register_func(
@@ -696,10 +697,11 @@ void GetLinkedBinding(const FunctionCallbackInfo<Value>& args) {
696697
env, "Linked binding has no declared entry point.");
697698
}
698699

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

705707
// 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

0 commit comments

Comments
 (0)