Skip to content

Commit 4eb74a2

Browse files
KhafraDevtargos
authored andcommitted
src: readiterable entries may be empty
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]>
1 parent bd5b61f commit 4eb74a2

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/node_messaging.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,8 +1003,11 @@ static Maybe<bool> ReadIterable(Environment* env,
10031003
entries.push_back(val);
10041004
}
10051005

1006-
transfer_list.AllocateSufficientStorage(entries.size());
1007-
std::copy(entries.begin(), entries.end(), &transfer_list[0]);
1006+
if (!entries.empty()) {
1007+
transfer_list.AllocateSufficientStorage(entries.size());
1008+
std::copy(entries.begin(), entries.end(), &transfer_list[0]);
1009+
}
1010+
10081011
return Just(true);
10091012
}
10101013

test/parallel/test-messagechannel.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
// See: https://github.com/nodejs/node/issues/49940
6+
(async () => {
7+
new MessageChannel().port1.postMessage({}, {
8+
transfer: {
9+
*[Symbol.iterator]() {}
10+
}
11+
});
12+
})().then(common.mustCall());

test/parallel/test-structuredClone-global.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,14 @@ assert.strictEqual(structuredClone(undefined, null), undefined);
1515
// Transfer can be null or undefined.
1616
assert.strictEqual(structuredClone(undefined, { transfer: null }), undefined);
1717
assert.strictEqual(structuredClone(undefined, { }), undefined);
18+
19+
{
20+
// See: https://github.com/nodejs/node/issues/49940
21+
const cloned = structuredClone({}, {
22+
transfer: {
23+
*[Symbol.iterator]() {}
24+
}
25+
});
26+
27+
assert.deepStrictEqual(cloned, {});
28+
}

0 commit comments

Comments
 (0)