diff --git a/src/node_messaging.cc b/src/node_messaging.cc index 571b271b69605a..47cc80cf551f74 100644 --- a/src/node_messaging.cc +++ b/src/node_messaging.cc @@ -1003,8 +1003,11 @@ static Maybe ReadIterable(Environment* env, entries.push_back(val); } - transfer_list.AllocateSufficientStorage(entries.size()); - std::copy(entries.begin(), entries.end(), &transfer_list[0]); + if (!entries.empty()) { + transfer_list.AllocateSufficientStorage(entries.size()); + std::copy(entries.begin(), entries.end(), &transfer_list[0]); + } + return Just(true); } diff --git a/test/parallel/test-messagechannel.js b/test/parallel/test-messagechannel.js new file mode 100644 index 00000000000000..4f92924daa5048 --- /dev/null +++ b/test/parallel/test-messagechannel.js @@ -0,0 +1,12 @@ +'use strict'; + +const common = require('../common'); + +// See: https://github.com/nodejs/node/issues/49940 +(async () => { + new MessageChannel().port1.postMessage({}, { + transfer: { + *[Symbol.iterator]() {} + } + }); +})().then(common.mustCall()); diff --git a/test/parallel/test-structuredClone-global.js b/test/parallel/test-structuredClone-global.js index 2b49a7a2e70696..ac5fc370c7845b 100644 --- a/test/parallel/test-structuredClone-global.js +++ b/test/parallel/test-structuredClone-global.js @@ -15,3 +15,14 @@ assert.strictEqual(structuredClone(undefined, null), undefined); // Transfer can be null or undefined. assert.strictEqual(structuredClone(undefined, { transfer: null }), undefined); assert.strictEqual(structuredClone(undefined, { }), undefined); + +{ + // See: https://github.com/nodejs/node/issues/49940 + const cloned = structuredClone({}, { + transfer: { + *[Symbol.iterator]() {} + } + }); + + assert.strictEqual(cloned, {}); +}