diff --git a/lib/child_process.js b/lib/child_process.js index 003f6569fa..b6768c654a 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -494,7 +494,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); @@ -514,7 +514,7 @@ function execSync(command /*, options*/) { var ret = spawnSync(opts.file, opts.options); ret.cmd = command; - if (inheritStderr) + if (inheritStderr && ret.stderr) process.stderr.write(ret.stderr); var err = checkExecSyncError(ret); diff --git a/test/sequential/test-child-process-execsync.js b/test/sequential/test-child-process-execsync.js index fc3c0494dc..e16fa32a40 100644 --- a/test/sequential/test-child-process-execsync.js +++ b/test/sequential/test-child-process-execsync.js @@ -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'}); }, + /spawnSync bad_shell ENOENT/, + 'execSync did not throw the expected exception!' +); +assert.throws( + function() { execFileSync('exit -1', {shell: 'bad_shell'}); }, + /spawnSync 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});