Browse Source

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>
process-exit-stdio-flushing
cjihrig 9 years ago
committed by James M Snell
parent
commit
7b355c5bb3
  1. 21
      lib/child_process.js

21
lib/child_process.js

@ -222,13 +222,21 @@ exports.execFile = function(file /*, args, options, callback*/) {
function errorhandler(e) {
ex = e;
if (child.stdout)
child.stdout.destroy();
if (child.stderr)
child.stderr.destroy();
exithandler();
}
function kill() {
if (child.stdout)
child.stdout.destroy();
if (child.stderr)
child.stderr.destroy();
killed = true;
@ -247,6 +255,10 @@ exports.execFile = function(file /*, args, options, callback*/) {
}, options.timeout);
}
if (child.stdout) {
if (encoding)
child.stdout.setEncoding(encoding);
child.stdout.addListener('data', function(chunk) {
stdoutLen += chunk.length;
@ -260,6 +272,11 @@ exports.execFile = function(file /*, args, options, callback*/) {
_stdout += chunk;
}
});
}
if (child.stderr) {
if (encoding)
child.stderr.setEncoding(encoding);
child.stderr.addListener('data', function(chunk) {
stderrLen += chunk.length;
@ -274,10 +291,6 @@ exports.execFile = function(file /*, args, options, callback*/) {
_stderr += chunk;
}
});
if (encoding) {
child.stderr.setEncoding(encoding);
child.stdout.setEncoding(encoding);
}
child.addListener('close', exithandler);

Loading…
Cancel
Save