Skip to content

Commit 51dad0a

Browse files
koh110BethGriggs
authored andcommitted
doc: fix default maxBuffer size
Correctly document the default maxBuffer size for execSync, execFileSync, and spawnSync. It is 200 * 1024, not Infinity. Add tests to verify behaviour is as documented. PR-URL: #22894 Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Signed-off-by: Beth Griggs <Bethany.Griggs@uk.ibm.com>
1 parent ab5dbf9 commit 51dad0a

6 files changed

+211
-3
lines changed

doc/api/child_process.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ changes:
721721
process will be killed. **Default:** `'SIGTERM'`.
722722
* `maxBuffer` {number} Largest amount of data in bytes allowed on stdout or
723723
stderr. If exceeded, the child process is terminated. See caveat at
724-
[`maxBuffer` and Unicode][]. **Default:** `200 * 1024`.
724+
[`maxBuffer` and Unicode][]. **Default:** `Infinity`.
725725
* `encoding` {string} The encoding used for all stdio inputs and outputs.
726726
**Default:** `'buffer'`.
727727
* `windowsHide` {boolean} Hide the subprocess console window that would
@@ -788,7 +788,7 @@ changes:
788788
* `maxBuffer` {number} Largest amount of data in bytes allowed on stdout or
789789
stderr. If exceeded, the child process is terminated and any output is
790790
truncated. See caveat at [`maxBuffer` and Unicode][].
791-
**Default:** `200 * 1024`.
791+
**Default:** `Infinity`.
792792
* `encoding` {string} The encoding used for all stdio inputs and outputs.
793793
**Default:** `'buffer'`.
794794
* `windowsHide` {boolean} Hide the subprocess console window that would
@@ -852,7 +852,7 @@ changes:
852852
* `maxBuffer` {number} Largest amount of data in bytes allowed on stdout or
853853
stderr. If exceeded, the child process is terminated and any output is
854854
truncated. See caveat at [`maxBuffer` and Unicode][].
855-
**Default:** `200 * 1024`.
855+
**Default:** `Infinity`.
856856
* `encoding` {string} The encoding used for all stdio inputs and outputs.
857857
**Default:** `'buffer'`.
858858
* `shell` {boolean|string} If `true`, runs `command` inside of a shell. Uses

test/parallel/test-child-process-exec-maxBuffer.js test/parallel/test-child-process-exec-maxbuf.js

+23
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,29 @@ function runChecks(err, stdio, streamName, expected) {
1010
assert.deepStrictEqual(stdio[streamName], expected);
1111
}
1212

13+
// default value
14+
{
15+
const cmd = `"${process.execPath}" -e "console.log('a'.repeat(200 * 1024))"`;
16+
17+
cp.exec(cmd, common.mustCall((err) => {
18+
assert(err instanceof RangeError);
19+
assert.strictEqual(err.message, 'stdout maxBuffer length exceeded');
20+
assert.strictEqual(err.code, 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER');
21+
}));
22+
}
23+
24+
// default value
25+
{
26+
const cmd =
27+
`${process.execPath} -e "console.log('a'.repeat(200 * 1024 - 1))"`;
28+
29+
cp.exec(cmd, common.mustCall((err, stdout, stderr) => {
30+
assert.ifError(err);
31+
assert.strictEqual(stdout.trim(), 'a'.repeat(200 * 1024 - 1));
32+
assert.strictEqual(stderr, '');
33+
}));
34+
}
35+
1336
{
1437
const cmd = `"${process.execPath}" -e "console.log('hello world');"`;
1538
const options = { maxBuffer: Infinity };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const { execFile } = require('child_process');
5+
6+
function checkFactory(streamName) {
7+
return common.mustCall((err) => {
8+
assert(err instanceof RangeError);
9+
assert.strictEqual(err.message, `${streamName} maxBuffer length exceeded`);
10+
assert.strictEqual(err.code, 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER');
11+
});
12+
}
13+
14+
// default value
15+
{
16+
execFile(
17+
process.execPath,
18+
['-e', 'console.log("a".repeat(200 * 1024))'],
19+
checkFactory('stdout')
20+
);
21+
}
22+
23+
// default value
24+
{
25+
execFile(
26+
process.execPath,
27+
['-e', 'console.log("a".repeat(200 * 1024 - 1))'],
28+
common.mustCall((err, stdout, stderr) => {
29+
assert.ifError(err);
30+
assert.strictEqual(stdout.trim(), 'a'.repeat(200 * 1024 - 1));
31+
assert.strictEqual(stderr, '');
32+
})
33+
);
34+
}
35+
36+
{
37+
const options = { maxBuffer: Infinity };
38+
39+
execFile(
40+
process.execPath,
41+
['-e', 'console.log("hello world");'],
42+
options,
43+
common.mustCall((err, stdout, stderr) => {
44+
assert.ifError(err);
45+
assert.strictEqual(stdout.trim(), 'hello world');
46+
assert.strictEqual(stderr, '');
47+
})
48+
);
49+
}
50+
51+
{
52+
execFile('echo', ['hello world'], { maxBuffer: 5 }, checkFactory('stdout'));
53+
}
54+
55+
const unicode = '中文测试'; // length = 4, byte length = 12
56+
57+
{
58+
execFile(
59+
process.execPath,
60+
['-e', `console.log('${unicode}');`],
61+
{ maxBuffer: 10 },
62+
checkFactory('stdout'));
63+
}
64+
65+
{
66+
execFile(
67+
process.execPath,
68+
['-e', `console.error('${unicode}');`],
69+
{ maxBuffer: 10 },
70+
checkFactory('stderr')
71+
);
72+
}
73+
74+
{
75+
const child = execFile(
76+
process.execPath,
77+
['-e', `console.log('${unicode}');`],
78+
{ encoding: null, maxBuffer: 10 },
79+
checkFactory('stdout')
80+
);
81+
82+
child.stdout.setEncoding('utf-8');
83+
}
84+
85+
{
86+
const child = execFile(
87+
process.execPath,
88+
['-e', `console.error('${unicode}');`],
89+
{ encoding: null, maxBuffer: 10 },
90+
checkFactory('stderr')
91+
);
92+
93+
child.stderr.setEncoding('utf-8');
94+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
require('../common');
3+
4+
// This test checks that the maxBuffer option for child_process.spawnSync()
5+
// works as expected.
6+
7+
const assert = require('assert');
8+
const execFileSync = require('child_process').execFileSync;
9+
const msgOut = 'this is stdout';
10+
const msgOutBuf = Buffer.from(`${msgOut}\n`);
11+
12+
const args = [
13+
'-e',
14+
`console.log("${msgOut}");`
15+
];
16+
17+
// Verify that an error is returned if maxBuffer is surpassed.
18+
{
19+
assert.throws(
20+
() => execFileSync(process.execPath, args, { maxBuffer: 1 }),
21+
(e) => {
22+
assert.ok(e, 'maxBuffer should error');
23+
assert.strictEqual(e.errno, 'ENOBUFS');
24+
assert.deepStrictEqual(e.stdout, msgOutBuf);
25+
return true;
26+
}
27+
);
28+
}
29+
30+
// Verify that a maxBuffer size of Infinity works.
31+
{
32+
const ret = execFileSync(process.execPath, args, { maxBuffer: Infinity });
33+
34+
assert.deepStrictEqual(ret, msgOutBuf);
35+
}
36+
37+
// maxBuffer size is Infinity at default.
38+
{
39+
const ret = execFileSync(
40+
process.execPath,
41+
['-e', "console.log('a'.repeat(200 * 1024))"],
42+
{ encoding: 'utf-8' }
43+
);
44+
45+
assert.ifError(ret.error);
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
require('../common');
3+
4+
// This test checks that the maxBuffer option for child_process.spawnSync()
5+
// works as expected.
6+
7+
const assert = require('assert');
8+
const { execSync } = require('child_process');
9+
const msgOut = 'this is stdout';
10+
const msgOutBuf = Buffer.from(`${msgOut}\n`);
11+
12+
const args = [
13+
'-e',
14+
`"console.log('${msgOut}')";`
15+
];
16+
17+
// Verify that an error is returned if maxBuffer is surpassed.
18+
{
19+
assert.throws(() => {
20+
execSync(`"${process.execPath}" ${args.join(' ')}`, { maxBuffer: 1 });
21+
}, (e) => {
22+
assert.ok(e, 'maxBuffer should error');
23+
assert.strictEqual(e.errno, 'ENOBUFS');
24+
assert.deepStrictEqual(e.stdout, msgOutBuf);
25+
return true;
26+
});
27+
}
28+
29+
// Verify that a maxBuffer size of Infinity works.
30+
{
31+
const ret = execSync(
32+
`"${process.execPath}" ${args.join(' ')}`,
33+
{ maxBuffer: Infinity }
34+
);
35+
36+
assert.deepStrictEqual(ret, msgOutBuf);
37+
}

test/parallel/test-child-process-spawnsync-maxbuf.js

+8
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,11 @@ const args = [
3232
assert.ifError(ret.error);
3333
assert.deepStrictEqual(ret.stdout, msgOutBuf);
3434
}
35+
36+
// maxBuffer size is Infinity at default.
37+
{
38+
const args = ['-e', "console.log('a'.repeat(200 * 1024))"];
39+
const ret = spawnSync(process.execPath, args, { encoding: 'utf-8' });
40+
41+
assert.ifError(ret.error);
42+
}

0 commit comments

Comments
 (0)