Skip to content

Commit 680b434

Browse files
jasnelladuh95
authored andcommitted
src: improve error handing in node_messaging
PR-URL: #57760 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
1 parent 1ded5f2 commit 680b434

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
@@ -488,8 +488,12 @@ Maybe<bool> Message::Serialize(Environment* env,
488488
Local<Object> entry = entry_val.As<Object>();
489489
// See https://github.com/nodejs/node/pull/30339#issuecomment-552225353
490490
// for details.
491-
if (entry->HasPrivate(context, env->untransferable_object_private_symbol())
492-
.ToChecked()) {
491+
bool ans;
492+
if (!entry->HasPrivate(context, env->untransferable_object_private_symbol())
493+
.To(&ans)) {
494+
return Nothing<bool>();
495+
}
496+
if (ans) {
493497
ThrowDataCloneException(context, env->transfer_unsupported_type_str());
494498
return Nothing<bool>();
495499
}
@@ -587,7 +591,9 @@ Maybe<bool> Message::Serialize(Environment* env,
587591
for (Local<ArrayBuffer> ab : array_buffers) {
588592
// If serialization succeeded, we render it inaccessible in this Isolate.
589593
std::shared_ptr<BackingStore> backing_store = ab->GetBackingStore();
590-
ab->Detach(Local<Value>()).Check();
594+
if (ab->Detach(Local<Value>()).IsNothing()) {
595+
return Nothing<bool>();
596+
}
591597

592598
array_buffers_.emplace_back(std::move(backing_store));
593599
}
@@ -1068,7 +1074,10 @@ bool GetTransferList(Environment* env,
10681074
void MessagePort::PostMessage(const FunctionCallbackInfo<Value>& args) {
10691075
Environment* env = Environment::GetCurrent(args);
10701076
Local<Object> obj = args.This();
1071-
Local<Context> context = obj->GetCreationContextChecked();
1077+
Local<Context> context;
1078+
if (!obj->GetCreationContext().ToLocal(&context)) {
1079+
return;
1080+
}
10721081

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

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

11651178
void MessagePort::MoveToContext(const FunctionCallbackInfo<Value>& args) {
@@ -1615,7 +1628,10 @@ static void MessageChannel(const FunctionCallbackInfo<Value>& args) {
16151628
return;
16161629
}
16171630

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

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

0 commit comments

Comments
 (0)