From 937452cd4ce5e864d333488c648daed21f5cd223 Mon Sep 17 00:00:00 2001 From: Jackson Tian Date: Sat, 6 Jun 2015 14:03:37 +0800 Subject: [PATCH] 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. --- lib/child_process.js | 11 +++-------- test/parallel/test-exec-max-buffer.js | 19 +++++++++++++++---- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/child_process.js b/lib/child_process.js index ee73562d24e80f..615bbc61f0a002 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -168,8 +168,7 @@ exports.execFile = function(file /*, args, options, callback*/) { _stderr = []; encoding = null; } - var stdoutLen = 0; - var stderrLen = 0; + var killed = false; var exited = false; var timeoutId; @@ -260,9 +259,7 @@ exports.execFile = function(file /*, args, options, callback*/) { child.stdout.setEncoding(encoding); child.stdout.addListener('data', function(chunk) { - stdoutLen += chunk.length; - - if (stdoutLen > options.maxBuffer) { + if (child.stdout.bytesRead > options.maxBuffer) { ex = new Error('stdout maxBuffer exceeded'); kill(); } else { @@ -279,9 +276,7 @@ exports.execFile = function(file /*, args, options, callback*/) { child.stderr.setEncoding(encoding); child.stderr.addListener('data', function(chunk) { - stderrLen += chunk.length; - - if (stderrLen > options.maxBuffer) { + if (child.stderr.bytesRead > options.maxBuffer) { ex = new Error('stderr maxBuffer exceeded'); kill(); } else { diff --git a/test/parallel/test-exec-max-buffer.js b/test/parallel/test-exec-max-buffer.js index 15c6e7025978e2..71bc45458431fb 100644 --- a/test/parallel/test-exec-max-buffer.js +++ b/test/parallel/test-exec-max-buffer.js @@ -1,11 +1,22 @@ 'use strict'; -require('../common'); -var exec = require('child_process').exec; -var assert = require('assert'); -var cmd = 'echo "hello world"'; +const common = require('../common'); +const exec = require('child_process').exec; +const assert = require('assert'); + +const cmd = 'echo "hello world"'; exec(cmd, { maxBuffer: 5 }, function(err, stdout, stderr) { assert.ok(err); assert.ok(/maxBuffer/.test(err.message)); }); + +if (!common.isWindows) { + const unicode = '中文测试'; // length: 12 + exec('echo ' + unicode, { + encoding: 'utf8', + maxBuffer: 10 + }, common.mustCall(function(err, stdout, stderr) { + assert.strictEqual(err.message, 'stdout maxBuffer exceeded'); + })); +}