src: make even more improvements to error handling

PR-URL: https://github.com/nodejs/node/pull/57264
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
This commit is contained in:
James M Snell 2025-03-01 12:59:20 -08:00
parent b4c8440109
commit 157b36b91b
15 changed files with 135 additions and 70 deletions

View File

@ -2157,10 +2157,11 @@ void TLSWrap::SetMaxSendFragment(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
TLSWrap* w;
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
int rv = SSL_set_max_send_fragment(
w->ssl_.get(),
args[0]->Int32Value(env->context()).FromJust());
args.GetReturnValue().Set(rv);
int val;
if (args[0]->Int32Value(env->context()).To(&val)) {
int32_t ret = SSL_set_max_send_fragment(w->ssl_.get(), val);
args.GetReturnValue().Set(ret);
}
}
#endif // SSL_set_max_send_fragment

View File

@ -231,8 +231,10 @@ template <void (Agent::*asyncTaskFn)(void*)>
static void InvokeAsyncTaskFnWithId(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
CHECK(args[0]->IsNumber());
int64_t task_id = args[0]->IntegerValue(env->context()).FromJust();
(env->inspector_agent()->*asyncTaskFn)(GetAsyncTask(task_id));
int64_t task_id;
if (args[0]->IntegerValue(env->context()).To(&task_id)) {
(env->inspector_agent()->*asyncTaskFn)(GetAsyncTask(task_id));
}
}
static void AsyncTaskScheduledWrapper(const FunctionCallbackInfo<Value>& args) {
@ -244,7 +246,10 @@ static void AsyncTaskScheduledWrapper(const FunctionCallbackInfo<Value>& args) {
StringView task_name_view(*task_name_value, task_name_value.length());
CHECK(args[1]->IsNumber());
int64_t task_id = args[1]->IntegerValue(env->context()).FromJust();
int64_t task_id;
if (!args[1]->IntegerValue(env->context()).To(&task_id)) {
return;
}
void* task = GetAsyncTask(task_id);
CHECK(args[2]->IsBoolean());

View File

@ -578,7 +578,10 @@ void ModuleWrap::Evaluate(const FunctionCallbackInfo<Value>& args) {
CHECK_EQ(args.Length(), 2);
CHECK(args[0]->IsNumber());
int64_t timeout = args[0]->IntegerValue(realm->context()).FromJust();
int64_t timeout;
if (!args[0]->IntegerValue(realm->context()).To(&timeout)) {
return;
}
CHECK(args[1]->IsBoolean());
bool break_on_sigint = args[1]->IsTrue();

View File

@ -278,10 +278,10 @@ MaybeLocal<Uint8Array> New(Environment* env,
size_t length) {
CHECK(!env->buffer_prototype_object().IsEmpty());
Local<Uint8Array> ui = Uint8Array::New(ab, byte_offset, length);
Maybe<bool> mb =
ui->SetPrototypeV2(env->context(), env->buffer_prototype_object());
if (mb.IsNothing())
if (ui->SetPrototypeV2(env->context(), env->buffer_prototype_object())
.IsNothing()) {
return MaybeLocal<Uint8Array>();
}
return ui;
}

View File

@ -79,7 +79,9 @@ void BuiltinLoader::GetNatives(Local<Name> property,
auto source = env->builtin_loader()->source_.read();
for (auto const& x : *source) {
Local<String> key = OneByteString(isolate, x.first);
out->Set(context, key, x.second.ToStringChecked(isolate)).FromJust();
if (out->Set(context, key, x.second.ToStringChecked(isolate)).IsNothing()) {
return;
}
}
info.GetReturnValue().Set(out);
}

View File

@ -1237,7 +1237,10 @@ void ContextifyScript::RunInContext(const FunctionCallbackInfo<Value>& args) {
TRACE_EVENT0(TRACING_CATEGORY_NODE2(vm, script), "RunInContext");
CHECK(args[1]->IsNumber());
int64_t timeout = args[1]->IntegerValue(env->context()).FromJust();
int64_t timeout;
if (!args[1]->IntegerValue(env->context()).To(&timeout)) {
return;
}
CHECK(args[2]->IsBoolean());
bool display_errors = args[2]->IsTrue();

View File

@ -254,10 +254,20 @@ void FileHandle::New(const FunctionCallbackInfo<Value>& args) {
std::optional<int64_t> maybeOffset = std::nullopt;
std::optional<int64_t> maybeLength = std::nullopt;
if (args[1]->IsNumber())
maybeOffset = args[1]->IntegerValue(realm->context()).FromJust();
if (args[2]->IsNumber())
maybeLength = args[2]->IntegerValue(realm->context()).FromJust();
if (args[1]->IsNumber()) {
int64_t val;
if (!args[1]->IntegerValue(realm->context()).To(&val)) {
return;
}
maybeOffset = val;
}
if (args[2]->IsNumber()) {
int64_t val;
if (!args[2]->IntegerValue(realm->context()).To(&val)) {
return;
}
maybeLength = val;
}
FileHandle::New(binding_data,
args[0].As<Int32>()->Value(),

View File

@ -1090,9 +1090,10 @@ void MessagePort::PostMessage(const FunctionCallbackInfo<Value>& args) {
return;
}
Maybe<bool> res = port->PostMessage(env, context, args[0], transfer_list);
if (res.IsJust())
args.GetReturnValue().Set(res.FromJust());
bool res;
if (port->PostMessage(env, context, args[0], transfer_list).To(&res)) {
args.GetReturnValue().Set(res);
}
}
void MessagePort::Start() {

View File

@ -189,10 +189,10 @@ void SerializerContext::WriteHeader(const FunctionCallbackInfo<Value>& args) {
void SerializerContext::WriteValue(const FunctionCallbackInfo<Value>& args) {
SerializerContext* ctx;
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This());
Maybe<bool> ret =
ctx->serializer_.WriteValue(ctx->env()->context(), args[0]);
if (ret.IsJust()) args.GetReturnValue().Set(ret.FromJust());
bool ret;
if (ctx->serializer_.WriteValue(ctx->env()->context(), args[0]).To(&ret)) {
args.GetReturnValue().Set(ret);
}
}
void SerializerContext::SetTreatArrayBufferViewsAsHostObjects(
@ -223,50 +223,55 @@ void SerializerContext::TransferArrayBuffer(
SerializerContext* ctx;
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This());
Maybe<uint32_t> id = args[0]->Uint32Value(ctx->env()->context());
if (id.IsNothing()) return;
uint32_t id;
if (!args[0]->Uint32Value(ctx->env()->context()).To(&id)) {
return;
}
if (!args[1]->IsArrayBuffer())
if (!args[1]->IsArrayBuffer()) {
return node::THROW_ERR_INVALID_ARG_TYPE(
ctx->env(), "arrayBuffer must be an ArrayBuffer");
}
Local<ArrayBuffer> ab = args[1].As<ArrayBuffer>();
ctx->serializer_.TransferArrayBuffer(id.FromJust(), ab);
return;
ctx->serializer_.TransferArrayBuffer(id, ab);
}
void SerializerContext::WriteUint32(const FunctionCallbackInfo<Value>& args) {
SerializerContext* ctx;
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This());
Maybe<uint32_t> value = args[0]->Uint32Value(ctx->env()->context());
if (value.IsNothing()) return;
ctx->serializer_.WriteUint32(value.FromJust());
uint32_t value;
if (args[0]->Uint32Value(ctx->env()->context()).To(&value)) {
ctx->serializer_.WriteUint32(value);
}
}
void SerializerContext::WriteUint64(const FunctionCallbackInfo<Value>& args) {
SerializerContext* ctx;
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This());
Maybe<uint32_t> arg0 = args[0]->Uint32Value(ctx->env()->context());
Maybe<uint32_t> arg1 = args[1]->Uint32Value(ctx->env()->context());
if (arg0.IsNothing() || arg1.IsNothing())
return;
uint32_t hi;
uint32_t lo;
uint64_t hi = arg0.FromJust();
uint64_t lo = arg1.FromJust();
ctx->serializer_.WriteUint64((hi << 32) | lo);
if (!args[0]->Uint32Value(ctx->env()->context()).To(&hi) ||
!args[1]->Uint32Value(ctx->env()->context()).To(&lo)) {
return;
}
uint64_t hiu64 = hi;
uint64_t lou64 = lo;
ctx->serializer_.WriteUint64((hiu64 << 32) | lou64);
}
void SerializerContext::WriteDouble(const FunctionCallbackInfo<Value>& args) {
SerializerContext* ctx;
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This());
Maybe<double> value = args[0]->NumberValue(ctx->env()->context());
if (value.IsNothing()) return;
ctx->serializer_.WriteDouble(value.FromJust());
double value;
if (args[0]->NumberValue(ctx->env()->context()).To(&value)) {
ctx->serializer_.WriteDouble(value);
}
}
void SerializerContext::WriteRawBytes(const FunctionCallbackInfo<Value>& args) {
@ -341,9 +346,10 @@ void DeserializerContext::ReadHeader(const FunctionCallbackInfo<Value>& args) {
DeserializerContext* ctx;
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This());
Maybe<bool> ret = ctx->deserializer_.ReadHeader(ctx->env()->context());
if (ret.IsJust()) args.GetReturnValue().Set(ret.FromJust());
bool ret;
if (ctx->deserializer_.ReadHeader(ctx->env()->context()).To(&ret)) {
args.GetReturnValue().Set(ret);
}
}
void DeserializerContext::ReadValue(const FunctionCallbackInfo<Value>& args) {
@ -361,18 +367,20 @@ void DeserializerContext::TransferArrayBuffer(
DeserializerContext* ctx;
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This());
Maybe<uint32_t> id = args[0]->Uint32Value(ctx->env()->context());
if (id.IsNothing()) return;
uint32_t id;
if (!args[0]->Uint32Value(ctx->env()->context()).To(&id)) {
return;
}
if (args[1]->IsArrayBuffer()) {
Local<ArrayBuffer> ab = args[1].As<ArrayBuffer>();
ctx->deserializer_.TransferArrayBuffer(id.FromJust(), ab);
ctx->deserializer_.TransferArrayBuffer(id, ab);
return;
}
if (args[1]->IsSharedArrayBuffer()) {
Local<SharedArrayBuffer> sab = args[1].As<SharedArrayBuffer>();
ctx->deserializer_.TransferSharedArrayBuffer(id.FromJust(), sab);
ctx->deserializer_.TransferSharedArrayBuffer(id, sab);
return;
}
@ -433,9 +441,11 @@ void DeserializerContext::ReadRawBytes(
DeserializerContext* ctx;
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This());
Maybe<int64_t> length_arg = args[0]->IntegerValue(ctx->env()->context());
if (length_arg.IsNothing()) return;
size_t length = length_arg.FromJust();
int64_t length_arg;
if (!args[0]->IntegerValue(ctx->env()->context()).To(&length_arg)) {
return;
}
size_t length = length_arg;
const void* data;
bool ok = ctx->deserializer_.ReadRawBytes(length, &data);

View File

@ -968,7 +968,11 @@ void DatabaseSync::CreateSession(const FunctionCallbackInfo<Value>& args) {
Local<Object> options = args[0].As<Object>();
Local<String> table_key = FIXED_ONE_BYTE_STRING(env->isolate(), "table");
if (options->HasOwnProperty(env->context(), table_key).FromJust()) {
bool hasIt;
if (!options->HasOwnProperty(env->context(), table_key).To(&hasIt)) {
return;
}
if (hasIt) {
Local<Value> table_value;
if (!options->Get(env->context(), table_key).ToLocal(&table_value)) {
return;
@ -986,7 +990,10 @@ void DatabaseSync::CreateSession(const FunctionCallbackInfo<Value>& args) {
Local<String> db_key = FIXED_ONE_BYTE_STRING(env->isolate(), "db");
if (options->HasOwnProperty(env->context(), db_key).FromJust()) {
if (!options->HasOwnProperty(env->context(), db_key).To(&hasIt)) {
return;
}
if (hasIt) {
Local<Value> db_value;
if (!options->Get(env->context(), db_key).ToLocal(&db_value)) {
// An error will have been scheduled.
@ -1205,8 +1212,12 @@ void DatabaseSync::ApplyChangeset(const FunctionCallbackInfo<Value>& args) {
};
}
if (options->HasOwnProperty(env->context(), env->filter_string())
.FromJust()) {
bool hasIt;
if (!options->HasOwnProperty(env->context(), env->filter_string())
.To(&hasIt)) {
return;
}
if (hasIt) {
Local<Value> filterValue;
if (!options->Get(env->context(), env->filter_string())
.ToLocal(&filterValue)) {

View File

@ -397,8 +397,11 @@ void BindingData::Update(const FunctionCallbackInfo<Value>& args) {
BindingData* binding_data = realm->GetBindingData<BindingData>();
Isolate* isolate = realm->isolate();
enum url_update_action action = static_cast<enum url_update_action>(
args[1]->Uint32Value(realm->context()).FromJust());
uint32_t val;
if (!args[1]->Uint32Value(realm->context()).To(&val)) {
return;
}
enum url_update_action action = static_cast<enum url_update_action>(val);
Utf8Value input(isolate, args[0].As<String>());
Utf8Value new_value(isolate, args[2].As<String>());

View File

@ -379,7 +379,10 @@ class ProcessWrap : public HandleWrap {
Environment* env = Environment::GetCurrent(args);
ProcessWrap* wrap;
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This());
int signal = args[0]->Int32Value(env->context()).FromJust();
int signal;
if (!args[0]->Int32Value(env->context()).To(&signal)) {
return;
}
#ifdef _WIN32
if (signal != SIGKILL && signal != SIGTERM && signal != SIGINT &&
signal != SIGQUIT) {

View File

@ -912,7 +912,10 @@ Maybe<int> SyncProcessRunner::ParseOptions(Local<Value> js_value) {
THROW_ERR_INVALID_ARG_TYPE(env(), "options.timeout must be a number");
return Nothing<int>();
}
int64_t timeout = js_timeout->IntegerValue(context).FromJust();
int64_t timeout;
if (!js_timeout->IntegerValue(context).To(&timeout)) {
return Nothing<int>();
}
timeout_ = static_cast<uint64_t>(timeout);
}
@ -926,7 +929,9 @@ Maybe<int> SyncProcessRunner::ParseOptions(Local<Value> js_value) {
THROW_ERR_INVALID_ARG_TYPE(env(), "options.maxBuffer must be a number");
return Nothing<int>();
}
max_buffer_ = js_max_buffer->NumberValue(context).FromJust();
if (!js_max_buffer->NumberValue(context).To(&max_buffer_)) {
return Nothing<int>();
}
}
Local<Value> js_kill_signal;
@ -1164,9 +1169,11 @@ Maybe<int> SyncProcessRunner::CopyJsStringArray(Local<Value> js_value,
}
}
Maybe<size_t> maybe_size = StringBytes::StorageSize(isolate, value, UTF8);
if (maybe_size.IsNothing()) return Nothing<int>();
data_size += maybe_size.FromJust() + 1;
size_t maybe_size;
if (!StringBytes::StorageSize(isolate, value, UTF8).To(&maybe_size)) {
return Nothing<int>();
}
data_size += maybe_size + 1;
data_size = nbytes::RoundUp(data_size, sizeof(void*));
}

View File

@ -342,7 +342,10 @@ void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args,
delete req_wrap;
} else {
CHECK(args[2]->Uint32Value(env->context()).IsJust());
int port = args[2]->Uint32Value(env->context()).FromJust();
uint32_t port;
if (!args[2]->Uint32Value(env->context()).To(&port)) {
return;
}
TRACE_EVENT_NESTABLE_ASYNC_BEGIN2(TRACING_CATEGORY_NODE2(net, native),
"connect",
req_wrap,

View File

@ -43,9 +43,12 @@ double BindingData::GetLibuvNowImpl(BindingData* data) {
}
void BindingData::SlowScheduleTimer(const FunctionCallbackInfo<Value>& args) {
int64_t duration =
args[0]->IntegerValue(args.GetIsolate()->GetCurrentContext()).FromJust();
ScheduleTimerImpl(Realm::GetBindingData<BindingData>(args), duration);
int64_t duration;
if (args[0]
->IntegerValue(args.GetIsolate()->GetCurrentContext())
.To(&duration)) {
ScheduleTimerImpl(Realm::GetBindingData<BindingData>(args), duration);
}
}
void BindingData::FastScheduleTimer(Local<Object> unused,