From 4ab7c8a12520e07cffe95b7d0bd91ddb14a2eba9 Mon Sep 17 00:00:00 2001 From: Khafra Date: Wed, 25 Oct 2023 17:24:45 -0400 Subject: [PATCH] src: readiterable entries may be empty --- 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 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, {}); +}