Skip to content

Commit 2e619fa

Browse files
jasnellRafaelGSS
authored andcommitted
src: improve error handing in node_messaging
PR-URL: #57760 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]>
1 parent 61ce1c7 commit 2e619fa

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

src/node_messaging.cc

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -489,8 +489,12 @@ Maybe<bool> Message::Serialize(Environment* env,
489489
Local<Object> entry = entry_val.As<Object>();
490490
// See https://github.com/nodejs/node/pull/30339#issuecomment-552225353
491491
// for details.
492-
if (entry->HasPrivate(context, env->untransferable_object_private_symbol())
493-
.ToChecked()) {
492+
bool ans;
493+
if (!entry->HasPrivate(context, env->untransferable_object_private_symbol())
494+
.To(&ans)) {
495+
return Nothing<bool>();
496+
}
497+
if (ans) {
494498
ThrowDataCloneException(context, env->transfer_unsupported_type_str());
495499
return Nothing<bool>();
496500
}
@@ -588,7 +592,9 @@ Maybe<bool> Message::Serialize(Environment* env,
588592
for (Local<ArrayBuffer> ab : array_buffers) {
589593
// If serialization succeeded, we render it inaccessible in this Isolate.
590594
std::shared_ptr<BackingStore> backing_store = ab->GetBackingStore();
591-
ab->Detach(Local<Value>()).Check();
595+
if (ab->Detach(Local<Value>()).IsNothing()) {
596+
return Nothing<bool>();
597+
}
592598

593599
array_buffers_.emplace_back(std::move(backing_store));
594600
}
@@ -1069,7 +1075,10 @@ bool GetTransferList(Environment* env,
10691075
void MessagePort::PostMessage(const FunctionCallbackInfo<Value>& args) {
10701076
Environment* env = Environment::GetCurrent(args);
10711077
Local<Object> obj = args.This();
1072-
Local<Context> context = obj->GetCreationContextChecked();
1078+
Local<Context> context;
1079+
if (!obj->GetCreationContext().ToLocal(&context)) {
1080+
return;
1081+
}
10731082

10741083
if (args.Length() == 0) {
10751084
return THROW_ERR_MISSING_ARGS(env, "Not enough arguments to "
@@ -1156,11 +1165,15 @@ void MessagePort::ReceiveMessage(const FunctionCallbackInfo<Value>& args) {
11561165
return;
11571166
}
11581167

1159-
MaybeLocal<Value> payload =
1160-
port->ReceiveMessage(port->object()->GetCreationContextChecked(),
1161-
MessageProcessingMode::kForceReadMessages);
1162-
if (!payload.IsEmpty())
1163-
args.GetReturnValue().Set(payload.ToLocalChecked());
1168+
Local<Value> payload;
1169+
Local<Context> context;
1170+
if (!port->object()->GetCreationContext().ToLocal(&context)) {
1171+
return;
1172+
}
1173+
if (port->ReceiveMessage(context, MessageProcessingMode::kForceReadMessages)
1174+
.ToLocal(&payload)) {
1175+
args.GetReturnValue().Set(payload);
1176+
}
11641177
}
11651178

11661179
void MessagePort::MoveToContext(const FunctionCallbackInfo<Value>& args) {
@@ -1616,7 +1629,10 @@ static void MessageChannel(const FunctionCallbackInfo<Value>& args) {
16161629
return;
16171630
}
16181631

1619-
Local<Context> context = args.This()->GetCreationContextChecked();
1632+
Local<Context> context;
1633+
if (!args.This()->GetCreationContext().ToLocal(&context)) {
1634+
return;
1635+
}
16201636
Context::Scope context_scope(context);
16211637

16221638
MessagePort* port1 = MessagePort::New(env, context);

0 commit comments

Comments
 (0)