Skip to content

Commit 2c52bbc

Browse files
cjihrigrvagg
authored andcommitted
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: #3799 Reviewed-By: Brian White <[email protected]> Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 16ebaf6 commit 2c52bbc

File tree

1 file changed

+47
-34
lines changed

1 file changed

+47
-34
lines changed

lib/child_process.js

+47-34
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,22 @@ exports.execFile = function(file /*, args, options, callback*/) {
222222

223223
function errorhandler(e) {
224224
ex = e;
225-
child.stdout.destroy();
226-
child.stderr.destroy();
225+
226+
if (child.stdout)
227+
child.stdout.destroy();
228+
229+
if (child.stderr)
230+
child.stderr.destroy();
231+
227232
exithandler();
228233
}
229234

230235
function kill() {
231-
child.stdout.destroy();
232-
child.stderr.destroy();
236+
if (child.stdout)
237+
child.stdout.destroy();
238+
239+
if (child.stderr)
240+
child.stderr.destroy();
233241

234242
killed = true;
235243
try {
@@ -247,37 +255,42 @@ exports.execFile = function(file /*, args, options, callback*/) {
247255
}, options.timeout);
248256
}
249257

250-
child.stdout.addListener('data', function(chunk) {
251-
stdoutLen += chunk.length;
252-
253-
if (stdoutLen > options.maxBuffer) {
254-
ex = new Error('stdout maxBuffer exceeded.');
255-
kill();
256-
} else {
257-
if (!encoding)
258-
_stdout.push(chunk);
259-
else
260-
_stdout += chunk;
261-
}
262-
});
263-
264-
child.stderr.addListener('data', function(chunk) {
265-
stderrLen += chunk.length;
266-
267-
if (stderrLen > options.maxBuffer) {
268-
ex = new Error('stderr maxBuffer exceeded.');
269-
kill();
270-
} else {
271-
if (!encoding)
272-
_stderr.push(chunk);
273-
else
274-
_stderr += chunk;
275-
}
276-
});
258+
if (child.stdout) {
259+
if (encoding)
260+
child.stdout.setEncoding(encoding);
261+
262+
child.stdout.addListener('data', function(chunk) {
263+
stdoutLen += chunk.length;
264+
265+
if (stdoutLen > options.maxBuffer) {
266+
ex = new Error('stdout maxBuffer exceeded');
267+
kill();
268+
} else {
269+
if (!encoding)
270+
_stdout.push(chunk);
271+
else
272+
_stdout += chunk;
273+
}
274+
});
275+
}
277276

278-
if (encoding) {
279-
child.stderr.setEncoding(encoding);
280-
child.stdout.setEncoding(encoding);
277+
if (child.stderr) {
278+
if (encoding)
279+
child.stderr.setEncoding(encoding);
280+
281+
child.stderr.addListener('data', function(chunk) {
282+
stderrLen += chunk.length;
283+
284+
if (stderrLen > options.maxBuffer) {
285+
ex = new Error('stderr maxBuffer exceeded');
286+
kill();
287+
} else {
288+
if (!encoding)
289+
_stderr.push(chunk);
290+
else
291+
_stderr += chunk;
292+
}
293+
});
281294
}
282295

283296
child.addListener('close', exithandler);

0 commit comments

Comments
 (0)