Skip to content

Commit

Permalink
worker: fix type check in receiveMessageOnPort
Browse files Browse the repository at this point in the history
Use the same type check we use in `MoveToContext()` in
`ReceiveMessage()`.

Fixes: #32742

PR-URL: #32745
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Gerhard Stöbich <[email protected]>
Reviewed-By: Yongsheng Zhang <[email protected]>
Reviewed-By: Zeyu Yang <[email protected]>
  • Loading branch information
addaleax authored and BethGriggs committed Apr 14, 2020
1 parent 0538dd1 commit 7147df5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/node_messaging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,12 @@ void MessagePort::Drain(const FunctionCallbackInfo<Value>& args) {
}

void MessagePort::ReceiveMessage(const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsObject());
Environment* env = Environment::GetCurrent(args);
if (!args[0]->IsObject() ||
!env->message_port_constructor_template()->HasInstance(args[0])) {
return THROW_ERR_INVALID_ARG_TYPE(env,
"First argument needs to be a MessagePort instance");
}
MessagePort* port = Unwrap<MessagePort>(args[0].As<Object>());
if (port == nullptr) {
// Return 'no messages' for a closed port.
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-worker-message-port-receive-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,11 @@ port2.on('message', common.mustNotCall());
port1.postMessage(message1);
assert.deepStrictEqual(receiveMessageOnPort(port2), { message: message1 });
port1.close();

for (const value of [null, 0, -1, {}, []]) {
assert.throws(() => receiveMessageOnPort(value), {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message: 'First argument needs to be a MessagePort instance'
});
}

0 comments on commit 7147df5

Please sign in to comment.