Skip to content

Commit 41ea5a2

Browse files
jasnelladuh95
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 906f23d commit 41ea5a2

15 files changed

+134
-69
lines changed

src/crypto/crypto_tls.cc

+5-4
Original file line numberDiff line numberDiff line change
@@ -2157,10 +2157,11 @@ void TLSWrap::SetMaxSendFragment(const FunctionCallbackInfo<Value>& args) {
21572157
Environment* env = Environment::GetCurrent(args);
21582158
TLSWrap* w;
21592159
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
2160-
int rv = SSL_set_max_send_fragment(
2161-
w->ssl_.get(),
2162-
args[0]->Int32Value(env->context()).FromJust());
2163-
args.GetReturnValue().Set(rv);
2160+
int val;
2161+
if (args[0]->Int32Value(env->context()).To(&val)) {
2162+
int32_t ret = SSL_set_max_send_fragment(w->ssl_.get(), val);
2163+
args.GetReturnValue().Set(ret);
2164+
}
21642165
}
21652166
#endif // SSL_set_max_send_fragment
21662167

src/inspector_js_api.cc

+8-3
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

+4-1
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

+3-3
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,10 @@ MaybeLocal<Uint8Array> New(Environment* env,
278278
size_t length) {
279279
CHECK(!env->buffer_prototype_object().IsEmpty());
280280
Local<Uint8Array> ui = Uint8Array::New(ab, byte_offset, length);
281-
Maybe<bool> mb =
282-
ui->SetPrototypeV2(env->context(), env->buffer_prototype_object());
283-
if (mb.IsNothing())
281+
if (ui->SetPrototypeV2(env->context(), env->buffer_prototype_object())
282+
.IsNothing()) {
284283
return MaybeLocal<Uint8Array>();
284+
}
285285
return ui;
286286
}
287287

src/node_builtins.cc

+3-1
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

+4-1
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,10 @@ void ContextifyScript::RunInContext(const FunctionCallbackInfo<Value>& args) {
12371237
TRACE_EVENT0(TRACING_CATEGORY_NODE2(vm, script), "RunInContext");
12381238

12391239
CHECK(args[1]->IsNumber());
1240-
int64_t timeout = args[1]->IntegerValue(env->context()).FromJust();
1240+
int64_t timeout;
1241+
if (!args[1]->IntegerValue(env->context()).To(&timeout)) {
1242+
return;
1243+
}
12411244

12421245
CHECK(args[2]->IsBoolean());
12431246
bool display_errors = args[2]->IsTrue();

src/node_file.cc

+14-4
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,20 @@ void FileHandle::New(const FunctionCallbackInfo<Value>& args) {
254254

255255
std::optional<int64_t> maybeOffset = std::nullopt;
256256
std::optional<int64_t> maybeLength = std::nullopt;
257-
if (args[1]->IsNumber())
258-
maybeOffset = args[1]->IntegerValue(realm->context()).FromJust();
259-
if (args[2]->IsNumber())
260-
maybeLength = args[2]->IntegerValue(realm->context()).FromJust();
257+
if (args[1]->IsNumber()) {
258+
int64_t val;
259+
if (!args[1]->IntegerValue(realm->context()).To(&val)) {
260+
return;
261+
}
262+
maybeOffset = val;
263+
}
264+
if (args[2]->IsNumber()) {
265+
int64_t val;
266+
if (!args[2]->IntegerValue(realm->context()).To(&val)) {
267+
return;
268+
}
269+
maybeLength = val;
270+
}
261271

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

src/node_messaging.cc

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

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

10981099
void MessagePort::Start() {

src/node_serdes.cc

+43-33
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,10 @@ void SerializerContext::WriteHeader(const FunctionCallbackInfo<Value>& args) {
189189
void SerializerContext::WriteValue(const FunctionCallbackInfo<Value>& args) {
190190
SerializerContext* ctx;
191191
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This());
192-
Maybe<bool> ret =
193-
ctx->serializer_.WriteValue(ctx->env()->context(), args[0]);
194-
195-
if (ret.IsJust()) args.GetReturnValue().Set(ret.FromJust());
192+
bool ret;
193+
if (ctx->serializer_.WriteValue(ctx->env()->context(), args[0]).To(&ret)) {
194+
args.GetReturnValue().Set(ret);
195+
}
196196
}
197197

198198
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) {
@@ -361,18 +367,20 @@ void DeserializerContext::TransferArrayBuffer(
361367
DeserializerContext* ctx;
362368
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This());
363369

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

367375
if (args[1]->IsArrayBuffer()) {
368376
Local<ArrayBuffer> ab = args[1].As<ArrayBuffer>();
369-
ctx->deserializer_.TransferArrayBuffer(id.FromJust(), ab);
377+
ctx->deserializer_.TransferArrayBuffer(id, ab);
370378
return;
371379
}
372380

373381
if (args[1]->IsSharedArrayBuffer()) {
374382
Local<SharedArrayBuffer> sab = args[1].As<SharedArrayBuffer>();
375-
ctx->deserializer_.TransferSharedArrayBuffer(id.FromJust(), sab);
383+
ctx->deserializer_.TransferSharedArrayBuffer(id, sab);
376384
return;
377385
}
378386

@@ -433,9 +441,11 @@ void DeserializerContext::ReadRawBytes(
433441
DeserializerContext* ctx;
434442
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This());
435443

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

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

src/node_sqlite.cc

+15-4
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,11 @@ void DatabaseSync::CreateSession(const FunctionCallbackInfo<Value>& args) {
968968
Local<Object> options = args[0].As<Object>();
969969

970970
Local<String> table_key = FIXED_ONE_BYTE_STRING(env->isolate(), "table");
971-
if (options->HasOwnProperty(env->context(), table_key).FromJust()) {
971+
bool hasIt;
972+
if (!options->HasOwnProperty(env->context(), table_key).To(&hasIt)) {
973+
return;
974+
}
975+
if (hasIt) {
972976
Local<Value> table_value;
973977
if (!options->Get(env->context(), table_key).ToLocal(&table_value)) {
974978
return;
@@ -986,7 +990,10 @@ void DatabaseSync::CreateSession(const FunctionCallbackInfo<Value>& args) {
986990

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

989-
if (options->HasOwnProperty(env->context(), db_key).FromJust()) {
993+
if (!options->HasOwnProperty(env->context(), db_key).To(&hasIt)) {
994+
return;
995+
}
996+
if (hasIt) {
990997
Local<Value> db_value;
991998
if (!options->Get(env->context(), db_key).ToLocal(&db_value)) {
992999
// An error will have been scheduled.
@@ -1205,8 +1212,12 @@ void DatabaseSync::ApplyChangeset(const FunctionCallbackInfo<Value>& args) {
12051212
};
12061213
}
12071214

1208-
if (options->HasOwnProperty(env->context(), env->filter_string())
1209-
.FromJust()) {
1215+
bool hasIt;
1216+
if (!options->HasOwnProperty(env->context(), env->filter_string())
1217+
.To(&hasIt)) {
1218+
return;
1219+
}
1220+
if (hasIt) {
12101221
Local<Value> filterValue;
12111222
if (!options->Get(env->context(), env->filter_string())
12121223
.ToLocal(&filterValue)) {

src/node_url.cc

+5-2
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

+4-1
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

+12-5
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

+4-1
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)