Skip to content

Commit 937452c

Browse files
committed
child_process: make the maxBuffer correct with unicode
The maxLen just caculate the string length, but to unicode string, the string size is less than buffer size.
1 parent 83d2b77 commit 937452c

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

lib/child_process.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,7 @@ exports.execFile = function(file /*, args, options, callback*/) {
168168
_stderr = [];
169169
encoding = null;
170170
}
171-
var stdoutLen = 0;
172-
var stderrLen = 0;
171+
173172
var killed = false;
174173
var exited = false;
175174
var timeoutId;
@@ -260,9 +259,7 @@ exports.execFile = function(file /*, args, options, callback*/) {
260259
child.stdout.setEncoding(encoding);
261260

262261
child.stdout.addListener('data', function(chunk) {
263-
stdoutLen += chunk.length;
264-
265-
if (stdoutLen > options.maxBuffer) {
262+
if (child.stdout.bytesRead > options.maxBuffer) {
266263
ex = new Error('stdout maxBuffer exceeded');
267264
kill();
268265
} else {
@@ -279,9 +276,7 @@ exports.execFile = function(file /*, args, options, callback*/) {
279276
child.stderr.setEncoding(encoding);
280277

281278
child.stderr.addListener('data', function(chunk) {
282-
stderrLen += chunk.length;
283-
284-
if (stderrLen > options.maxBuffer) {
279+
if (child.stderr.bytesRead > options.maxBuffer) {
285280
ex = new Error('stderr maxBuffer exceeded');
286281
kill();
287282
} else {
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
'use strict';
2-
require('../common');
3-
var exec = require('child_process').exec;
4-
var assert = require('assert');
52

6-
var cmd = 'echo "hello world"';
3+
const common = require('../common');
4+
const exec = require('child_process').exec;
5+
const assert = require('assert');
6+
7+
const cmd = 'echo "hello world"';
78

89
exec(cmd, { maxBuffer: 5 }, function(err, stdout, stderr) {
910
assert.ok(err);
1011
assert.ok(/maxBuffer/.test(err.message));
1112
});
13+
14+
if (!common.isWindows) {
15+
const unicode = '中文测试'; // length: 12
16+
exec('echo ' + unicode, {
17+
encoding: 'utf8',
18+
maxBuffer: 10
19+
}, common.mustCall(function(err, stdout, stderr) {
20+
assert.strictEqual(err.message, 'stdout maxBuffer exceeded');
21+
}));
22+
}

0 commit comments

Comments
 (0)