Skip to content

Commit

Permalink
src: readiterable entries may be empty
Browse files Browse the repository at this point in the history
fixup

PR-URL: #50398
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Debadree Chatterjee <debadree333@gmail.com>
  • Loading branch information
KhafraDev authored and UlisesGascon committed Dec 11, 2023
1 parent 7a661d7 commit b7ecb0a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/node_messaging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -994,8 +994,11 @@ static Maybe<bool> 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);
}

Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-messagechannel.js
Original file line number Diff line number Diff line change
@@ -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());
11 changes: 11 additions & 0 deletions test/parallel/test-structuredClone-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {});
}

0 comments on commit b7ecb0a

Please sign in to comment.