Skip to content

Commit d2ae6ae

Browse files
jasnellRafaelGSS
authored andcommitted
src: make even more improvements to error handling
PR-URL: #57264 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
1 parent 5f396ac commit d2ae6ae

15 files changed

+134
-69
lines changed

src/crypto/crypto_tls.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,10 +2171,11 @@ void TLSWrap::SetMaxSendFragment(const FunctionCallbackInfo<Value>& args) {
21712171
Environment* env = Environment::GetCurrent(args);
21722172
TLSWrap* w;
21732173
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
2174-
int rv = SSL_set_max_send_fragment(
2175-
w->ssl_.get(),
2176-
args[0]->Int32Value(env->context()).FromJust());
2177-
args.GetReturnValue().Set(rv);
2174+
int val;
2175+
if (args[0]->Int32Value(env->context()).To(&val)) {
2176+
int32_t ret = SSL_set_max_send_fragment(w->ssl_.get(), val);
2177+
args.GetReturnValue().Set(ret);
2178+
}
21782179
}
21792180
#endif // SSL_set_max_send_fragment
21802181

src/inspector_js_api.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,10 @@ template <void (Agent::*asyncTaskFn)(void*)>
231231
static void InvokeAsyncTaskFnWithId(const FunctionCallbackInfo<Value>& args) {
232232
Environment* env = Environment::GetCurrent(args);
233233
CHECK(args[0]->IsNumber());
234-
int64_t task_id = args[0]->IntegerValue(env->context()).FromJust();
235-
(env->inspector_agent()->*asyncTaskFn)(GetAsyncTask(task_id));
234+
int64_t task_id;
235+
if (args[0]->IntegerValue(env->context()).To(&task_id)) {
236+
(env->inspector_agent()->*asyncTaskFn)(GetAsyncTask(task_id));
237+
}
236238
}
237239

238240
static void AsyncTaskScheduledWrapper(const FunctionCallbackInfo<Value>& args) {
@@ -244,7 +246,10 @@ static void AsyncTaskScheduledWrapper(const FunctionCallbackInfo<Value>& args) {
244246
StringView task_name_view(*task_name_value, task_name_value.length());
245247

246248
CHECK(args[1]->IsNumber());
247-
int64_t task_id = args[1]->IntegerValue(env->context()).FromJust();
249+
int64_t task_id;
250+
if (!args[1]->IntegerValue(env->context()).To(&task_id)) {
251+
return;
252+
}
248253
void* task = GetAsyncTask(task_id);
249254

250255
CHECK(args[2]->IsBoolean());

src/module_wrap.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,10 @@ void ModuleWrap::Evaluate(const FunctionCallbackInfo<Value>& args) {
561561
CHECK_EQ(args.Length(), 2);
562562

563563
CHECK(args[0]->IsNumber());
564-
int64_t timeout = args[0]->IntegerValue(realm->context()).FromJust();
564+
int64_t timeout;
565+
if (!args[0]->IntegerValue(realm->context()).To(&timeout)) {
566+
return;
567+
}
565568

566569
CHECK(args[1]->IsBoolean());
567570
bool break_on_sigint = args[1]->IsTrue();

src/node_buffer.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,10 @@ MaybeLocal<Uint8Array> New(Environment* env,
277277
size_t length) {
278278
CHECK(!env->buffer_prototype_object().IsEmpty());
279279
Local<Uint8Array> ui = Uint8Array::New(ab, byte_offset, length);
280-
Maybe<bool> mb =
281-
ui->SetPrototype(env->context(), env->buffer_prototype_object());
282-
if (mb.IsNothing())
280+
if (ui->SetPrototype(env->context(), env->buffer_prototype_object())
281+
.IsNothing()) {
283282
return MaybeLocal<Uint8Array>();
283+
}
284284
return ui;
285285
}
286286

src/node_builtins.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ void BuiltinLoader::GetNatives(Local<Name> property,
7979
auto source = env->builtin_loader()->source_.read();
8080
for (auto const& x : *source) {
8181
Local<String> key = OneByteString(isolate, x.first);
82-
out->Set(context, key, x.second.ToStringChecked(isolate)).FromJust();
82+
if (out->Set(context, key, x.second.ToStringChecked(isolate)).IsNothing()) {
83+
return;
84+
}
8385
}
8486
info.GetReturnValue().Set(out);
8587
}

src/node_contextify.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,10 @@ void ContextifyScript::RunInContext(const FunctionCallbackInfo<Value>& args) {
12391239
TRACE_EVENT0(TRACING_CATEGORY_NODE2(vm, script), "RunInContext");
12401240

12411241
CHECK(args[1]->IsNumber());
1242-
int64_t timeout = args[1]->IntegerValue(env->context()).FromJust();
1242+
int64_t timeout;
1243+
if (!args[1]->IntegerValue(env->context()).To(&timeout)) {
1244+
return;
1245+
}
12431246

12441247
CHECK(args[2]->IsBoolean());
12451248
bool display_errors = args[2]->IsTrue();

src/node_file.cc

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,20 @@ void FileHandle::New(const FunctionCallbackInfo<Value>& args) {
248248

249249
std::optional<int64_t> maybeOffset = std::nullopt;
250250
std::optional<int64_t> maybeLength = std::nullopt;
251-
if (args[1]->IsNumber())
252-
maybeOffset = args[1]->IntegerValue(realm->context()).FromJust();
253-
if (args[2]->IsNumber())
254-
maybeLength = args[2]->IntegerValue(realm->context()).FromJust();
251+
if (args[1]->IsNumber()) {
252+
int64_t val;
253+
if (!args[1]->IntegerValue(realm->context()).To(&val)) {
254+
return;
255+
}
256+
maybeOffset = val;
257+
}
258+
if (args[2]->IsNumber()) {
259+
int64_t val;
260+
if (!args[2]->IntegerValue(realm->context()).To(&val)) {
261+
return;
262+
}
263+
maybeLength = val;
264+
}
255265

256266
FileHandle::New(binding_data,
257267
args[0].As<Int32>()->Value(),

src/node_messaging.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,9 +1089,10 @@ void MessagePort::PostMessage(const FunctionCallbackInfo<Value>& args) {
10891089
return;
10901090
}
10911091

1092-
Maybe<bool> res = port->PostMessage(env, context, args[0], transfer_list);
1093-
if (res.IsJust())
1094-
args.GetReturnValue().Set(res.FromJust());
1092+
bool res;
1093+
if (port->PostMessage(env, context, args[0], transfer_list).To(&res)) {
1094+
args.GetReturnValue().Set(res);
1095+
}
10951096
}
10961097

10971098
void MessagePort::Start() {

src/node_serdes.cc

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,10 @@ void SerializerContext::WriteHeader(const FunctionCallbackInfo<Value>& args) {
187187
void SerializerContext::WriteValue(const FunctionCallbackInfo<Value>& args) {
188188
SerializerContext* ctx;
189189
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This());
190-
Maybe<bool> ret =
191-
ctx->serializer_.WriteValue(ctx->env()->context(), args[0]);
192-
193-
if (ret.IsJust()) args.GetReturnValue().Set(ret.FromJust());
190+
bool ret;
191+
if (ctx->serializer_.WriteValue(ctx->env()->context(), args[0]).To(&ret)) {
192+
args.GetReturnValue().Set(ret);
193+
}
194194
}
195195

196196
void SerializerContext::SetTreatArrayBufferViewsAsHostObjects(
@@ -223,50 +223,55 @@ void SerializerContext::TransferArrayBuffer(
223223
SerializerContext* ctx;
224224
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This());
225225

226-
Maybe<uint32_t> id = args[0]->Uint32Value(ctx->env()->context());
227-
if (id.IsNothing()) return;
226+
uint32_t id;
227+
if (!args[0]->Uint32Value(ctx->env()->context()).To(&id)) {
228+
return;
229+
}
228230

229-
if (!args[1]->IsArrayBuffer())
231+
if (!args[1]->IsArrayBuffer()) {
230232
return node::THROW_ERR_INVALID_ARG_TYPE(
231233
ctx->env(), "arrayBuffer must be an ArrayBuffer");
234+
}
232235

233236
Local<ArrayBuffer> ab = args[1].As<ArrayBuffer>();
234-
ctx->serializer_.TransferArrayBuffer(id.FromJust(), ab);
235-
return;
237+
ctx->serializer_.TransferArrayBuffer(id, ab);
236238
}
237239

238240
void SerializerContext::WriteUint32(const FunctionCallbackInfo<Value>& args) {
239241
SerializerContext* ctx;
240242
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This());
241243

242-
Maybe<uint32_t> value = args[0]->Uint32Value(ctx->env()->context());
243-
if (value.IsNothing()) return;
244-
245-
ctx->serializer_.WriteUint32(value.FromJust());
244+
uint32_t value;
245+
if (args[0]->Uint32Value(ctx->env()->context()).To(&value)) {
246+
ctx->serializer_.WriteUint32(value);
247+
}
246248
}
247249

248250
void SerializerContext::WriteUint64(const FunctionCallbackInfo<Value>& args) {
249251
SerializerContext* ctx;
250252
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This());
251253

252-
Maybe<uint32_t> arg0 = args[0]->Uint32Value(ctx->env()->context());
253-
Maybe<uint32_t> arg1 = args[1]->Uint32Value(ctx->env()->context());
254-
if (arg0.IsNothing() || arg1.IsNothing())
254+
uint32_t hi;
255+
uint32_t lo;
256+
257+
if (!args[0]->Uint32Value(ctx->env()->context()).To(&hi) ||
258+
!args[1]->Uint32Value(ctx->env()->context()).To(&lo)) {
255259
return;
260+
}
256261

257-
uint64_t hi = arg0.FromJust();
258-
uint64_t lo = arg1.FromJust();
259-
ctx->serializer_.WriteUint64((hi << 32) | lo);
262+
uint64_t hiu64 = hi;
263+
uint64_t lou64 = lo;
264+
ctx->serializer_.WriteUint64((hiu64 << 32) | lou64);
260265
}
261266

262267
void SerializerContext::WriteDouble(const FunctionCallbackInfo<Value>& args) {
263268
SerializerContext* ctx;
264269
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This());
265270

266-
Maybe<double> value = args[0]->NumberValue(ctx->env()->context());
267-
if (value.IsNothing()) return;
268-
269-
ctx->serializer_.WriteDouble(value.FromJust());
271+
double value;
272+
if (args[0]->NumberValue(ctx->env()->context()).To(&value)) {
273+
ctx->serializer_.WriteDouble(value);
274+
}
270275
}
271276

272277
void SerializerContext::WriteRawBytes(const FunctionCallbackInfo<Value>& args) {
@@ -341,9 +346,10 @@ void DeserializerContext::ReadHeader(const FunctionCallbackInfo<Value>& args) {
341346
DeserializerContext* ctx;
342347
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This());
343348

344-
Maybe<bool> ret = ctx->deserializer_.ReadHeader(ctx->env()->context());
345-
346-
if (ret.IsJust()) args.GetReturnValue().Set(ret.FromJust());
349+
bool ret;
350+
if (ctx->deserializer_.ReadHeader(ctx->env()->context()).To(&ret)) {
351+
args.GetReturnValue().Set(ret);
352+
}
347353
}
348354

349355
void DeserializerContext::ReadValue(const FunctionCallbackInfo<Value>& args) {
@@ -360,18 +366,20 @@ void DeserializerContext::TransferArrayBuffer(
360366
DeserializerContext* ctx;
361367
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This());
362368

363-
Maybe<uint32_t> id = args[0]->Uint32Value(ctx->env()->context());
364-
if (id.IsNothing()) return;
369+
uint32_t id;
370+
if (!args[0]->Uint32Value(ctx->env()->context()).To(&id)) {
371+
return;
372+
}
365373

366374
if (args[1]->IsArrayBuffer()) {
367375
Local<ArrayBuffer> ab = args[1].As<ArrayBuffer>();
368-
ctx->deserializer_.TransferArrayBuffer(id.FromJust(), ab);
376+
ctx->deserializer_.TransferArrayBuffer(id, ab);
369377
return;
370378
}
371379

372380
if (args[1]->IsSharedArrayBuffer()) {
373381
Local<SharedArrayBuffer> sab = args[1].As<SharedArrayBuffer>();
374-
ctx->deserializer_.TransferSharedArrayBuffer(id.FromJust(), sab);
382+
ctx->deserializer_.TransferSharedArrayBuffer(id, sab);
375383
return;
376384
}
377385

@@ -432,9 +440,11 @@ void DeserializerContext::ReadRawBytes(
432440
DeserializerContext* ctx;
433441
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This());
434442

435-
Maybe<int64_t> length_arg = args[0]->IntegerValue(ctx->env()->context());
436-
if (length_arg.IsNothing()) return;
437-
size_t length = length_arg.FromJust();
443+
int64_t length_arg;
444+
if (!args[0]->IntegerValue(ctx->env()->context()).To(&length_arg)) {
445+
return;
446+
}
447+
size_t length = length_arg;
438448

439449
const void* data;
440450
bool ok = ctx->deserializer_.ReadRawBytes(length, &data);

src/node_sqlite.cc

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,11 @@ void DatabaseSync::CreateSession(const FunctionCallbackInfo<Value>& args) {
741741
Local<Object> options = args[0].As<Object>();
742742

743743
Local<String> table_key = FIXED_ONE_BYTE_STRING(env->isolate(), "table");
744-
if (options->HasOwnProperty(env->context(), table_key).FromJust()) {
744+
bool hasIt;
745+
if (!options->HasOwnProperty(env->context(), table_key).To(&hasIt)) {
746+
return;
747+
}
748+
if (hasIt) {
745749
Local<Value> table_value;
746750
if (!options->Get(env->context(), table_key).ToLocal(&table_value)) {
747751
return;
@@ -759,7 +763,10 @@ void DatabaseSync::CreateSession(const FunctionCallbackInfo<Value>& args) {
759763

760764
Local<String> db_key = FIXED_ONE_BYTE_STRING(env->isolate(), "db");
761765

762-
if (options->HasOwnProperty(env->context(), db_key).FromJust()) {
766+
if (!options->HasOwnProperty(env->context(), db_key).To(&hasIt)) {
767+
return;
768+
}
769+
if (hasIt) {
763770
Local<Value> db_value;
764771
if (!options->Get(env->context(), db_key).ToLocal(&db_value)) {
765772
// An error will have been scheduled.
@@ -864,8 +871,12 @@ void DatabaseSync::ApplyChangeset(const FunctionCallbackInfo<Value>& args) {
864871
};
865872
}
866873

867-
if (options->HasOwnProperty(env->context(), env->filter_string())
868-
.FromJust()) {
874+
bool hasIt;
875+
if (!options->HasOwnProperty(env->context(), env->filter_string())
876+
.To(&hasIt)) {
877+
return;
878+
}
879+
if (hasIt) {
869880
Local<Value> filterValue;
870881
if (!options->Get(env->context(), env->filter_string())
871882
.ToLocal(&filterValue)) {

src/node_url.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,11 @@ void BindingData::Update(const FunctionCallbackInfo<Value>& args) {
397397
BindingData* binding_data = realm->GetBindingData<BindingData>();
398398
Isolate* isolate = realm->isolate();
399399

400-
enum url_update_action action = static_cast<enum url_update_action>(
401-
args[1]->Uint32Value(realm->context()).FromJust());
400+
uint32_t val;
401+
if (!args[1]->Uint32Value(realm->context()).To(&val)) {
402+
return;
403+
}
404+
enum url_update_action action = static_cast<enum url_update_action>(val);
402405
Utf8Value input(isolate, args[0].As<String>());
403406
Utf8Value new_value(isolate, args[2].As<String>());
404407

src/process_wrap.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,10 @@ class ProcessWrap : public HandleWrap {
379379
Environment* env = Environment::GetCurrent(args);
380380
ProcessWrap* wrap;
381381
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This());
382-
int signal = args[0]->Int32Value(env->context()).FromJust();
382+
int signal;
383+
if (!args[0]->Int32Value(env->context()).To(&signal)) {
384+
return;
385+
}
383386
#ifdef _WIN32
384387
if (signal != SIGKILL && signal != SIGTERM && signal != SIGINT &&
385388
signal != SIGQUIT) {

src/spawn_sync.cc

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,10 @@ Maybe<int> SyncProcessRunner::ParseOptions(Local<Value> js_value) {
912912
THROW_ERR_INVALID_ARG_TYPE(env(), "options.timeout must be a number");
913913
return Nothing<int>();
914914
}
915-
int64_t timeout = js_timeout->IntegerValue(context).FromJust();
915+
int64_t timeout;
916+
if (!js_timeout->IntegerValue(context).To(&timeout)) {
917+
return Nothing<int>();
918+
}
916919
timeout_ = static_cast<uint64_t>(timeout);
917920
}
918921

@@ -926,7 +929,9 @@ Maybe<int> SyncProcessRunner::ParseOptions(Local<Value> js_value) {
926929
THROW_ERR_INVALID_ARG_TYPE(env(), "options.maxBuffer must be a number");
927930
return Nothing<int>();
928931
}
929-
max_buffer_ = js_max_buffer->NumberValue(context).FromJust();
932+
if (!js_max_buffer->NumberValue(context).To(&max_buffer_)) {
933+
return Nothing<int>();
934+
}
930935
}
931936

932937
Local<Value> js_kill_signal;
@@ -1164,9 +1169,11 @@ Maybe<int> SyncProcessRunner::CopyJsStringArray(Local<Value> js_value,
11641169
}
11651170
}
11661171

1167-
Maybe<size_t> maybe_size = StringBytes::StorageSize(isolate, value, UTF8);
1168-
if (maybe_size.IsNothing()) return Nothing<int>();
1169-
data_size += maybe_size.FromJust() + 1;
1172+
size_t maybe_size;
1173+
if (!StringBytes::StorageSize(isolate, value, UTF8).To(&maybe_size)) {
1174+
return Nothing<int>();
1175+
}
1176+
data_size += maybe_size + 1;
11701177
data_size = nbytes::RoundUp(data_size, sizeof(void*));
11711178
}
11721179

src/tcp_wrap.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,10 @@ void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args,
342342
delete req_wrap;
343343
} else {
344344
CHECK(args[2]->Uint32Value(env->context()).IsJust());
345-
int port = args[2]->Uint32Value(env->context()).FromJust();
345+
uint32_t port;
346+
if (!args[2]->Uint32Value(env->context()).To(&port)) {
347+
return;
348+
}
346349
TRACE_EVENT_NESTABLE_ASYNC_BEGIN2(TRACING_CATEGORY_NODE2(net, native),
347350
"connect",
348351
req_wrap,

0 commit comments

Comments
 (0)