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 <cjihrig@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
Reviewed-By: Zeyu Yang <himself65@outlook.com>
  • Loading branch information
addaleax authored and targos committed Apr 28, 2020
1 parent 26fee8b commit 57cd7d2
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
Expand Up @@ -895,7 +895,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
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 57cd7d2

Please sign in to comment.