From b7ecb0a390894736937343e7b08808e3b5ff2bd9 Mon Sep 17 00:00:00 2001 From: Khafra Date: Fri, 27 Oct 2023 17:37:32 -0400 Subject: [PATCH] src: readiterable entries may be empty MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixup PR-URL: https://github.com/nodejs/node/pull/50398 Reviewed-By: Yagiz Nizipli Reviewed-By: Vinícius Lourenço Claro Cardoso Reviewed-By: Debadree Chatterjee --- src/node_messaging.cc | 7 +++++-- test/parallel/test-messagechannel.js | 12 ++++++++++++ test/parallel/test-structuredClone-global.js | 11 +++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-messagechannel.js diff --git a/src/node_messaging.cc b/src/node_messaging.cc index 1a5ca8efd0beb3..04b998bb28ce4b 100644 --- a/src/node_messaging.cc +++ b/src/node_messaging.cc @@ -994,8 +994,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..7a4d85b6c177f0 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.deepStrictEqual(cloned, {}); +}