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 <[email protected]>
Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]>
Reviewed-By: Debadree Chatterjee <[email protected]>
  • Loading branch information
KhafraDev authored Oct 27, 2023
1 parent 8eea2d3 commit e2cf2f0
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 @@ -1003,8 1003,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 e2cf2f0

Please sign in to comment.