Skip to content

Commit 5414eb4

Browse files
jasnelltargos
authored andcommitted
src: improve error handling in multiple files
PR-URL: #56962 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent 4b02fdc commit 5414eb4

11 files changed

+212
-178
lines changed

src/node_i18n.cc

+22-12
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,15 @@ namespace {
104104

105105
template <typename T>
106106
MaybeLocal<Object> ToBufferEndian(Environment* env, MaybeStackBuffer<T>* buf) {
107-
MaybeLocal<Object> ret = Buffer::New(env, buf);
108-
if (ret.IsEmpty())
109-
return ret;
107+
Local<Object> ret;
108+
if (!Buffer::New(env, buf).ToLocal(&ret)) {
109+
return {};
110+
}
110111

111112
static_assert(sizeof(T) == 1 || sizeof(T) == 2,
112113
"Currently only one- or two-byte buffers are supported");
113114
if constexpr (sizeof(T) > 1 && IsBigEndian()) {
114-
SPREAD_BUFFER_ARG(ret.ToLocalChecked(), retbuf);
115+
SPREAD_BUFFER_ARG(ret, retbuf);
115116
CHECK(nbytes::SwapBytes16(retbuf_data, retbuf_length));
116117
}
117118

@@ -317,19 +318,22 @@ void Transcode(const FunctionCallbackInfo<Value>&args) {
317318
status = U_ILLEGAL_ARGUMENT_ERROR;
318319
}
319320

320-
if (result.IsEmpty())
321-
return args.GetReturnValue().Set(status);
321+
Local<Object> res;
322+
if (result.ToLocal(&res)) {
323+
return args.GetReturnValue().Set(res);
324+
}
322325

323-
return args.GetReturnValue().Set(result.ToLocalChecked());
326+
return args.GetReturnValue().Set(status);
324327
}
325328

326329
void ICUErrorName(const FunctionCallbackInfo<Value>& args) {
327330
Environment* env = Environment::GetCurrent(args);
328331
CHECK(args[0]->IsInt32());
329332
UErrorCode status = static_cast<UErrorCode>(args[0].As<Int32>()->Value());
330-
args.GetReturnValue().Set(
331-
String::NewFromUtf8(env->isolate(),
332-
u_errorName(status)).ToLocalChecked());
333+
Local<Value> res;
334+
if (String::NewFromUtf8(env->isolate(), u_errorName(status)).ToLocal(&res)) {
335+
args.GetReturnValue().Set(res);
336+
}
333337
}
334338

335339
} // anonymous namespace
@@ -390,7 +394,10 @@ void ConverterObject::Create(const FunctionCallbackInfo<Value>& args) {
390394

391395
CHECK_GE(args.Length(), 2);
392396
Utf8Value label(env->isolate(), args[0]);
393-
int flags = args[1]->Uint32Value(env->context()).ToChecked();
397+
uint32_t flags;
398+
if (!args[1]->Uint32Value(env->context()).To(&flags)) {
399+
return;
400+
}
394401
bool fatal =
395402
(flags & CONVERTER_FLAGS_FATAL) == CONVERTER_FLAGS_FATAL;
396403

@@ -430,7 +437,10 @@ void ConverterObject::Decode(const FunctionCallbackInfo<Value>& args) {
430437
}
431438

432439
ArrayBufferViewContents<char> input(args[1]);
433-
int flags = args[2]->Uint32Value(env->context()).ToChecked();
440+
uint32_t flags;
441+
if (!args[2]->Uint32Value(env->context()).To(&flags)) {
442+
return;
443+
}
434444

435445
CHECK(args[3]->IsString());
436446
Local<String> from_encoding = args[3].As<String>();

src/node_messaging.cc

+7-7
Original file line numberDiff line numberDiff line change
@@ -1150,16 +1150,16 @@ void MessagePort::ReceiveMessage(const FunctionCallbackInfo<Value>& args) {
11501150
MessagePort* port = Unwrap<MessagePort>(args[0].As<Object>());
11511151
if (port == nullptr) {
11521152
// Return 'no messages' for a closed port.
1153-
args.GetReturnValue().Set(
1154-
Environment::GetCurrent(args)->no_message_symbol());
1153+
args.GetReturnValue().Set(env->no_message_symbol());
11551154
return;
11561155
}
11571156

1158-
MaybeLocal<Value> payload =
1159-
port->ReceiveMessage(port->object()->GetCreationContextChecked(),
1160-
MessageProcessingMode::kForceReadMessages);
1161-
if (!payload.IsEmpty())
1162-
args.GetReturnValue().Set(payload.ToLocalChecked());
1157+
Local<Value> payload;
1158+
if (port->ReceiveMessage(port->object()->GetCreationContextChecked(),
1159+
MessageProcessingMode::kForceReadMessages)
1160+
.ToLocal(&payload)) {
1161+
args.GetReturnValue().Set(payload);
1162+
}
11631163
}
11641164

11651165
void MessagePort::MoveToContext(const FunctionCallbackInfo<Value>& args) {

src/node_modules.cc

+22-17
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,10 @@ void BindingData::GetNearestParentPackageJSONType(
398398
return;
399399
}
400400

401-
Local<Value> value =
402-
ToV8Value(realm->context(), package_json->type).ToLocalChecked();
403-
args.GetReturnValue().Set(value);
401+
Local<Value> value;
402+
if (ToV8Value(realm->context(), package_json->type).ToLocal(&value)) {
403+
args.GetReturnValue().Set(value);
404+
}
404405
}
405406

406407
void BindingData::GetPackageScopeConfig(
@@ -462,12 +463,11 @@ void BindingData::GetPackageScopeConfig(
462463
auto package_json_url_as_path =
463464
url::FileURLToPath(realm->env(), *package_json_url);
464465
CHECK(package_json_url_as_path);
465-
return args.GetReturnValue().Set(
466-
String::NewFromUtf8(realm->isolate(),
467-
package_json_url_as_path->c_str(),
468-
NewStringType::kNormal,
469-
package_json_url_as_path->size())
470-
.ToLocalChecked());
466+
Local<Value> ret;
467+
if (ToV8Value(realm->context(), *package_json_url_as_path, realm->isolate())
468+
.ToLocal(&ret)) {
469+
args.GetReturnValue().Set(ret);
470+
}
471471
}
472472

473473
void FlushCompileCache(const FunctionCallbackInfo<Value>& args) {
@@ -499,11 +499,13 @@ void EnableCompileCache(const FunctionCallbackInfo<Value>& args) {
499499
}
500500
Utf8Value value(isolate, args[0]);
501501
CompileCacheEnableResult result = env->EnableCompileCache(*value);
502-
Local<Value> values[] = {
503-
v8::Integer::New(isolate, static_cast<uint8_t>(result.status)),
504-
ToV8Value(context, result.message).ToLocalChecked(),
505-
ToV8Value(context, result.cache_directory).ToLocalChecked()};
506-
args.GetReturnValue().Set(Array::New(isolate, &values[0], arraysize(values)));
502+
Local<Value> values[3];
503+
values[0] = v8::Integer::New(isolate, static_cast<uint8_t>(result.status));
504+
if (ToV8Value(context, result.message).ToLocal(&values[1]) &&
505+
ToV8Value(context, result.cache_directory).ToLocal(&values[2])) {
506+
args.GetReturnValue().Set(
507+
Array::New(isolate, &values[0], arraysize(values)));
508+
}
507509
}
508510

509511
void GetCompileCacheDir(const FunctionCallbackInfo<Value>& args) {
@@ -514,9 +516,12 @@ void GetCompileCacheDir(const FunctionCallbackInfo<Value>& args) {
514516
args.GetReturnValue().Set(v8::String::Empty(isolate));
515517
return;
516518
}
517-
args.GetReturnValue().Set(
518-
ToV8Value(context, env->compile_cache_handler()->cache_dir())
519-
.ToLocalChecked());
519+
520+
Local<Value> ret;
521+
if (ToV8Value(context, env->compile_cache_handler()->cache_dir())
522+
.ToLocal(&ret)) {
523+
args.GetReturnValue().Set(ret);
524+
}
520525
}
521526

522527
void GetCompileCacheEntry(const FunctionCallbackInfo<Value>& args) {

src/node_options.cc

+13-9
Original file line numberDiff line numberDiff line change
@@ -1364,9 +1364,11 @@ void GetCLIOptionsValues(const FunctionCallbackInfo<Value>& args) {
13641364
std::string negated_name =
13651365
"--no" + item.first.substr(1, item.first.size());
13661366
Local<Value> negated_value = Boolean::New(isolate, !original_value);
1367-
Local<Name> negated_name_v8 =
1368-
ToV8Value(context, negated_name).ToLocalChecked().As<Name>();
1369-
option_names.push_back(negated_name_v8);
1367+
Local<Value> negated_name_v8;
1368+
if (!ToV8Value(context, negated_name).ToLocal(&negated_name_v8)) {
1369+
return;
1370+
}
1371+
option_names.push_back(negated_name_v8.As<Name>());
13701372
option_values.push_back(negated_value);
13711373
break;
13721374
}
@@ -1414,9 +1416,11 @@ void GetCLIOptionsValues(const FunctionCallbackInfo<Value>& args) {
14141416
UNREACHABLE();
14151417
}
14161418
CHECK(!value.IsEmpty());
1417-
Local<Name> name =
1418-
ToV8Value(context, item.first).ToLocalChecked().As<Name>();
1419-
option_names.push_back(name);
1419+
Local<Value> name;
1420+
if (!ToV8Value(context, item.first).ToLocal(&name)) {
1421+
return;
1422+
}
1423+
option_names.push_back(name.As<Name>());
14201424
option_values.push_back(value);
14211425
}
14221426

@@ -1455,10 +1459,10 @@ void GetCLIOptionsInfo(const FunctionCallbackInfo<Value>& args) {
14551459
const auto& option_info = item.second;
14561460
auto field = option_info.field;
14571461

1458-
Local<Name> name =
1459-
ToV8Value(context, item.first).ToLocalChecked().As<Name>();
1462+
Local<Value> name;
14601463
Local<Value> help_text;
1461-
if (!ToV8Value(context, option_info.help_text).ToLocal(&help_text)) {
1464+
if (!ToV8Value(context, item.first).ToLocal(&name) ||
1465+
!ToV8Value(context, option_info.help_text).ToLocal(&help_text)) {
14621466
return;
14631467
}
14641468
constexpr size_t kInfoSize = 4;

src/node_process_methods.cc

+7-6
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,15 @@ static void Cwd(const FunctionCallbackInfo<Value>& args) {
136136
char buf[PATH_MAX_BYTES];
137137
size_t cwd_len = sizeof(buf);
138138
int err = uv_cwd(buf, &cwd_len);
139-
if (err)
139+
if (err) {
140140
return env->ThrowUVException(err, "uv_cwd");
141+
}
141142

142-
Local<String> cwd = String::NewFromUtf8(env->isolate(),
143-
buf,
144-
NewStringType::kNormal,
145-
cwd_len).ToLocalChecked();
146-
args.GetReturnValue().Set(cwd);
143+
Local<String> cwd;
144+
if (String::NewFromUtf8(env->isolate(), buf, NewStringType::kNormal, cwd_len)
145+
.ToLocal(&cwd)) {
146+
args.GetReturnValue().Set(cwd);
147+
}
147148
}
148149

149150
static void Kill(const FunctionCallbackInfo<Value>& args) {

src/node_process_object.cc

+44-41
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,20 @@ using v8::Isolate;
2222
using v8::Local;
2323
using v8::MaybeLocal;
2424
using v8::Name;
25-
using v8::NewStringType;
2625
using v8::None;
2726
using v8::Object;
2827
using v8::PropertyCallbackInfo;
2928
using v8::SideEffectType;
30-
using v8::String;
3129
using v8::Value;
3230

3331
static void ProcessTitleGetter(Local<Name> property,
3432
const PropertyCallbackInfo<Value>& info) {
3533
std::string title = GetProcessTitle("node");
36-
info.GetReturnValue().Set(
37-
String::NewFromUtf8(info.GetIsolate(), title.data(),
38-
NewStringType::kNormal, title.size())
39-
.ToLocalChecked());
34+
Local<Value> ret;
35+
auto isolate = info.GetIsolate();
36+
if (ToV8Value(isolate->GetCurrentContext(), title, isolate).ToLocal(&ret)) {
37+
info.GetReturnValue().Set(ret);
38+
}
4039
}
4140

4241
static void ProcessTitleSetter(Local<Name> property,
@@ -196,28 +195,34 @@ void PatchProcessObject(const FunctionCallbackInfo<Value>& args) {
196195
.FromJust());
197196

198197
// process.argv
199-
process->Set(context,
200-
FIXED_ONE_BYTE_STRING(isolate, "argv"),
201-
ToV8Value(context, env->argv()).ToLocalChecked()).Check();
198+
Local<Value> val;
199+
if (!ToV8Value(context, env->argv()).ToLocal(&val) ||
200+
!process->Set(context, FIXED_ONE_BYTE_STRING(isolate, "argv"), val)
201+
.IsJust()) {
202+
return;
203+
}
202204

203205
// process.execArgv
204-
process->Set(context,
205-
FIXED_ONE_BYTE_STRING(isolate, "execArgv"),
206-
ToV8Value(context, env->exec_argv())
207-
.ToLocalChecked()).Check();
206+
if (!ToV8Value(context, env->exec_argv()).ToLocal(&val) ||
207+
!process->Set(context, FIXED_ONE_BYTE_STRING(isolate, "execArgv"), val)
208+
.IsJust()) {
209+
return;
210+
}
208211

209212
READONLY_PROPERTY(process, "pid",
210213
Integer::New(isolate, uv_os_getpid()));
211214

212-
CHECK(process
213-
->SetNativeDataProperty(context,
214-
FIXED_ONE_BYTE_STRING(isolate, "ppid"),
215-
GetParentProcessId,
216-
nullptr,
217-
Local<Value>(),
218-
None,
219-
SideEffectType::kHasNoSideEffect)
220-
.FromJust());
215+
if (!process
216+
->SetNativeDataProperty(context,
217+
FIXED_ONE_BYTE_STRING(isolate, "ppid"),
218+
GetParentProcessId,
219+
nullptr,
220+
Local<Value>(),
221+
None,
222+
SideEffectType::kHasNoSideEffect)
223+
.IsJust()) {
224+
return;
225+
}
221226

222227
// --security-revert flags
223228
#define V(code, _, __) \
@@ -230,27 +235,25 @@ void PatchProcessObject(const FunctionCallbackInfo<Value>& args) {
230235
#undef V
231236

232237
// process.execPath
233-
process
234-
->Set(context,
235-
FIXED_ONE_BYTE_STRING(isolate, "execPath"),
236-
String::NewFromUtf8(isolate,
237-
env->exec_path().c_str(),
238-
NewStringType::kInternalized,
239-
env->exec_path().size())
240-
.ToLocalChecked())
241-
.Check();
238+
if (!ToV8Value(context, env->exec_path(), isolate).ToLocal(&val) ||
239+
!process->Set(context, FIXED_ONE_BYTE_STRING(isolate, "execPath"), val)
240+
.IsJust()) {
241+
return;
242+
}
242243

243244
// process.debugPort
244-
CHECK(process
245-
->SetNativeDataProperty(
246-
context,
247-
FIXED_ONE_BYTE_STRING(isolate, "debugPort"),
248-
DebugPortGetter,
249-
env->owns_process_state() ? DebugPortSetter : nullptr,
250-
Local<Value>(),
251-
None,
252-
SideEffectType::kHasNoSideEffect)
253-
.FromJust());
245+
if (!process
246+
->SetNativeDataProperty(
247+
context,
248+
FIXED_ONE_BYTE_STRING(isolate, "debugPort"),
249+
DebugPortGetter,
250+
env->owns_process_state() ? DebugPortSetter : nullptr,
251+
Local<Value>(),
252+
None,
253+
SideEffectType::kHasNoSideEffect)
254+
.IsJust()) {
255+
return;
256+
}
254257

255258
// process.versions
256259
Local<Object> versions = Object::New(isolate);

src/node_report.cc

+8-4
Original file line numberDiff line numberDiff line change
@@ -439,10 +439,14 @@ static Maybe<std::string> ErrorToString(Isolate* isolate,
439439
} else if (!error->IsObject()) {
440440
maybe_str = error->ToString(context);
441441
} else if (error->IsObject()) {
442-
MaybeLocal<Value> stack = error.As<Object>()->Get(
443-
context, FIXED_ONE_BYTE_STRING(isolate, "stack"));
444-
if (!stack.IsEmpty() && stack.ToLocalChecked()->IsString()) {
445-
maybe_str = stack.ToLocalChecked().As<String>();
442+
Local<Value> stack;
443+
if (!error.As<Object>()
444+
->Get(context, FIXED_ONE_BYTE_STRING(isolate, "stack"))
445+
.ToLocal(&stack)) {
446+
return Nothing<std::string>();
447+
}
448+
if (stack->IsString()) {
449+
maybe_str = stack.As<String>();
446450
}
447451
}
448452

0 commit comments

Comments
 (0)