Skip to content

Commit

Permalink
src: ReadIterable entries may be empty
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev committed Oct 25, 2023
1 parent c3a41d8 commit ee40112
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
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
@@ -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
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.strictEqual(cloned, {});
}

0 comments on commit ee40112

Please sign in to comment.