Skip to content

Commit

Permalink
child_process: add safety checks on stdio access
Browse files Browse the repository at this point in the history
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: #3799
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
cjihrig authored and rvagg committed Nov 13, 2015
1 parent 09e524d commit 84bb745
Showing 1 changed file with 47 additions and 34 deletions.
81 changes: 47 additions & 34 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand Down

0 comments on commit 84bb745

Please sign in to comment.