Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
child_process: Check stderr before accessing it
If something bad happens in spawnSync, stderr might be null. Therefore,
we have to check it before using it, so we won't mask the actual
exception.

PR-URL: #6877
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Robert Jefe Lindstädt <robert.lindstaedt@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
  • Loading branch information
robertchiras committed Oct 26, 2016
commit fdb2408db602aaf21dd036688c5e6c1e49ad184b
4 changes: 2 additions & 2 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ function execFileSync(/*command, args, options*/) {

var ret = spawnSync(opts.file, opts.args.slice(1), opts.options);

if (inheritStderr)
if (inheritStderr && ret.stderr)
process.stderr.write(ret.stderr);

var err = checkExecSyncError(ret);
Expand All @@ -499,7 +499,7 @@ function execSync(/*command, options*/) {
var ret = spawnSync(opts.file, opts.args, opts.options);
ret.cmd = opts.cmd;

if (inheritStderr)
if (inheritStderr && ret.stderr)
process.stderr.write(ret.stderr);

var err = checkExecSyncError(ret);
Expand Down
12 changes: 12 additions & 0 deletions test/sequential/test-child-process-execsync.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ var start = Date.now();
var err;
var caught = false;

// Verify that stderr is not accessed when a bad shell is used
assert.throws(
function() { execSync('exit -1', {shell: 'bad_shell'}); },
/ENOENT/,
'execSync did not throw the expected exception!'
);
assert.throws(
function() { execFileSync('exit -1', {shell: 'bad_shell'}); },
/ENOENT/,
'execFileSync did not throw the expected exception!'
);

try {
var cmd = `"${process.execPath}" -e "setTimeout(function(){}, ${SLEEP});"`;
var ret = execSync(cmd, {timeout: TIMER});
Expand Down