Skip to content

src: make even more improvements to error handling #57264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/crypto/crypto_tls.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 8 additions & 3 deletions src/inspector_js_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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());
Expand Down
5 changes: 4 additions & 1 deletion src/module_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 3 additions & 1 deletion src/node_builtins.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
5 changes: 4 additions & 1 deletion src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
18 changes: 14 additions & 4 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
7 changes: 4 additions & 3 deletions src/node_messaging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
76 changes: 43 additions & 33 deletions src/node_serdes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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())
uint32_t hi;
uint32_t lo;

if (!args[0]->Uint32Value(ctx->env()->context()).To(&hi) ||
!args[1]->Uint32Value(ctx->env()->context()).To(&lo)) {
return;
}

uint64_t hi = arg0.FromJust();
uint64_t lo = arg1.FromJust();
ctx->serializer_.WriteUint64((hi << 32) | lo);
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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down
19 changes: 15 additions & 4 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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)) {
Expand Down
7 changes: 5 additions & 2 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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>());

Expand Down
5 changes: 4 additions & 1 deletion src/process_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 12 additions & 5 deletions src/spawn_sync.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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;
Expand Down Expand Up @@ -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*));
}

Expand Down
5 changes: 4 additions & 1 deletion src/tcp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading
Loading