diff options
author | cjihrig <cjihrig@gmail.com> | 2015-11-12 14:30:20 -0500 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2015-11-12 17:52:09 -0800 |
commit | 7b355c5bb30d7f9b38654fdb50e58dbd1dcd990a (patch) | |
tree | cb20d4916fd6b386046a797b5bb709d5af9628ff /lib/child_process.js | |
parent | f8390fdd7528e4c1863ee1e40eda8f9b1dbd23e6 (diff) | |
download | android-node-v8-7b355c5bb30d7f9b38654fdb50e58dbd1dcd990a.tar.gz android-node-v8-7b355c5bb30d7f9b38654fdb50e58dbd1dcd990a.tar.bz2 android-node-v8-7b355c5bb30d7f9b38654fdb50e58dbd1dcd990a.zip |
child_process: add safety checks on stdio access
When a child process is spawned, there is no guarantee that stdout
and stderr will be created successfully. This commit adds checks
before attempting to access the streams.
PR-URL: https://github.com/nodejs/node/pull/3799
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib/child_process.js')
-rw-r--r-- | lib/child_process.js | 81 |
1 files changed, 47 insertions, 34 deletions
diff --git a/lib/child_process.js b/lib/child_process.js index 5abba8a534..ee73562d24 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -222,14 +222,22 @@ exports.execFile = function(file /*, args, options, callback*/) { function errorhandler(e) { ex = e; - child.stdout.destroy(); - child.stderr.destroy(); + + if (child.stdout) + child.stdout.destroy(); + + if (child.stderr) + child.stderr.destroy(); + exithandler(); } function kill() { - child.stdout.destroy(); - child.stderr.destroy(); + if (child.stdout) + child.stdout.destroy(); + + if (child.stderr) + child.stderr.destroy(); killed = true; try { @@ -247,37 +255,42 @@ exports.execFile = function(file /*, args, options, callback*/) { }, options.timeout); } - child.stdout.addListener('data', function(chunk) { - stdoutLen += chunk.length; - - if (stdoutLen > options.maxBuffer) { - ex = new Error('stdout maxBuffer exceeded'); - kill(); - } else { - if (!encoding) - _stdout.push(chunk); - else - _stdout += chunk; - } - }); - - child.stderr.addListener('data', function(chunk) { - stderrLen += chunk.length; - - if (stderrLen > options.maxBuffer) { - ex = new Error('stderr maxBuffer exceeded'); - kill(); - } else { - if (!encoding) - _stderr.push(chunk); - else - _stderr += chunk; - } - }); + if (child.stdout) { + if (encoding) + child.stdout.setEncoding(encoding); + + child.stdout.addListener('data', function(chunk) { + stdoutLen += chunk.length; + + if (stdoutLen > options.maxBuffer) { + ex = new Error('stdout maxBuffer exceeded'); + kill(); + } else { + if (!encoding) + _stdout.push(chunk); + else + _stdout += chunk; + } + }); + } - if (encoding) { - child.stderr.setEncoding(encoding); - child.stdout.setEncoding(encoding); + if (child.stderr) { + if (encoding) + child.stderr.setEncoding(encoding); + + child.stderr.addListener('data', function(chunk) { + stderrLen += chunk.length; + + if (stderrLen > options.maxBuffer) { + ex = new Error('stderr maxBuffer exceeded'); + kill(); + } else { + if (!encoding) + _stderr.push(chunk); + else + _stderr += chunk; + } + }); } child.addListener('close', exithandler); |