Skip to content
Closed
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
71 changes: 30 additions & 41 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,15 @@ exports.execFile = function(file /*, args, options, callback*/) {
});

var encoding;
var stdoutState;
var stderrState;
var _stdout = [];
var _stderr = [];
var _stdout;
var _stderr;
if (options.encoding !== 'buffer' && Buffer.isEncoding(options.encoding)) {
encoding = options.encoding;
_stdout = '';
_stderr = '';
} else {
_stdout = [];
_stderr = [];
encoding = null;
}
var stdoutLen = 0;
Expand All @@ -174,23 +176,16 @@ exports.execFile = function(file /*, args, options, callback*/) {

if (!callback) return;

var stdout = Buffer.concat(_stdout, stdoutLen);
var stderr = Buffer.concat(_stderr, stderrLen);

var stdoutEncoding = encoding;
var stderrEncoding = encoding;

if (stdoutState && stdoutState.decoder)
stdoutEncoding = stdoutState.decoder.encoding;

if (stderrState && stderrState.decoder)
stderrEncoding = stderrState.decoder.encoding;

if (stdoutEncoding)
stdout = stdout.toString(stdoutEncoding);

if (stderrEncoding)
stderr = stderr.toString(stderrEncoding);
// merge chunks
var stdout;
var stderr;
if (encoding) {
stdout = _stdout;
stderr = _stderr;
} else {
stdout = Buffer.concat(_stdout);
stderr = Buffer.concat(_stderr);
}

if (ex) {
// Will be handled later
Expand Down Expand Up @@ -250,45 +245,39 @@ exports.execFile = function(file /*, args, options, callback*/) {
}

if (child.stdout) {
stdoutState = child.stdout._readableState;
if (encoding)
child.stdout.setEncoding(encoding);

child.stdout.addListener('data', function(chunk) {
// If `child.stdout.setEncoding()` happened in userland, convert string to
// Buffer.
if (stdoutState.decoder) {
chunk = Buffer.from(chunk, stdoutState.decoder.encoding);
}

stdoutLen += chunk.byteLength;
stdoutLen += encoding ? Buffer.byteLength(chunk, encoding) : chunk.length;

if (stdoutLen > options.maxBuffer) {
ex = new Error('stdout maxBuffer exceeded');
stdoutLen -= chunk.byteLength;
kill();
} else {
_stdout.push(chunk);
if (encoding)
_stdout += chunk;
else
_stdout.push(chunk);
}
});
}

if (child.stderr) {
stderrState = child.stderr._readableState;
if (encoding)
child.stderr.setEncoding(encoding);

child.stderr.addListener('data', function(chunk) {
// If `child.stderr.setEncoding()` happened in userland, convert string to
// Buffer.
if (stderrState.decoder) {
chunk = Buffer.from(chunk, stderrState.decoder.encoding);
}

stderrLen += chunk.byteLength;
stderrLen += encoding ? Buffer.byteLength(chunk, encoding) : chunk.length;

if (stderrLen > options.maxBuffer) {
ex = new Error('stderr maxBuffer exceeded');
stderrLen -= chunk.byteLength;
kill();
} else {
_stderr.push(chunk);
if (encoding)
_stderr += chunk;
else
_stderr.push(chunk);
}
});
}
Expand Down
31 changes: 31 additions & 0 deletions test/parallel/test-child-process-exec-maxBuffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const cp = require('child_process');

function checkFactory(streamName) {
return common.mustCall((err) => {
const message = `${streamName} maxBuffer exceeded`;
assert.strictEqual(err.message, message);
});
}

{
const cmd = 'echo "hello world"';

cp.exec(cmd, { maxBuffer: 5 }, checkFactory('stdout'));
}

const unicode = '中文测试'; // length = 4, byte length = 12

{
const cmd = `"${process.execPath}" -e "console.log('${unicode}');"`;

cp.exec(cmd, {maxBuffer: 10}, checkFactory('stdout'));
}

{
const cmd = `"${process.execPath}" -e "console.('${unicode}');"`;

cp.exec(cmd, {maxBuffer: 10}, checkFactory('stderr'));
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@ const common = require('../common');
const assert = require('assert');
const exec = require('child_process').exec;

const expectedCalls = 2;

const cb = common.mustCall((data) => {
assert.strictEqual(typeof data, 'string');
}, expectedCalls);
var stdoutCalls = 0;
var stderrCalls = 0;

const command = common.isWindows ? 'dir' : 'ls';
exec(command).stdout.on('data', cb);
exec(command).stdout.on('data', (data) => {
stdoutCalls += 1;
});

exec('fhqwhgads').stderr.on('data', (data) => {
assert.strictEqual(typeof data, 'string');
stderrCalls += 1;
});

exec('fhqwhgads').stderr.on('data', cb);
process.on('exit', () => {
assert(stdoutCalls > 0);
assert(stderrCalls > 0);
});
15 changes: 0 additions & 15 deletions test/parallel/test-child-process-maxBuffer-stderr.js

This file was deleted.

15 changes: 0 additions & 15 deletions test/parallel/test-child-process-maxBuffer-stdout.js

This file was deleted.

11 changes: 0 additions & 11 deletions test/parallel/test-exec-max-buffer.js

This file was deleted.