Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

child_process: fix parsing messages with splitted length field #56106

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/internal/child_process/serialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ const advanced = {
*parseChannelMessages(channel, readData) {
if (readData.length === 0) return;

ArrayPrototypePush(channel[kMessageBuffer], readData);
if (channel[kMessageBufferSize] && channel[kMessageBuffer][0].length < 4) {
// Message length split into two buffers, so let's concatenate it.
channel[kMessageBuffer][0] = Buffer.concat([channel[kMessageBuffer][0], readData]);
} else {
ArrayPrototypePush(channel[kMessageBuffer], readData);
}
channel[kMessageBufferSize] += readData.length;

// Index 0 should always be present because we just pushed data into it.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';
const common = require('../common');
const child_process = require('child_process');

// Regression test for https://github.com/nodejs/node/issues/55834
const msgLen = 65521;
let cnt = 10;

if (process.argv[2] === 'child') {
const msg = Buffer.allocUnsafe(msgLen);
(function send() {
if (cnt--) {
process.send(msg, send);
} else {
process.disconnect();
}
})();
} else {
const child = child_process.spawn(process.execPath, [__filename, 'child'], {
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
serialization: 'advanced'
});
child.on('message', common.mustCall(cnt));
}