From fdb2408db602aaf21dd036688c5e6c1e49ad184b Mon Sep 17 00:00:00 2001 From: Robert Chiras Date: Thu, 19 May 2016 17:13:20 +0300 Subject: [PATCH] child_process: Check stderr before accessing it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: https://github.com/nodejs/node/pull/6877 Reviewed-By: Colin Ihrig Reviewed-By: Robert Jefe Lindstädt Reviewed-By: James M Snell Reviewed-By: Ben Noordhuis Reviewed-By: Jeremiah Senkpiel --- lib/child_process.js | 4 ++-- test/sequential/test-child-process-execsync.js | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/child_process.js b/lib/child_process.js index 02d7c2344798f0..727ac230ab18d8 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -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); @@ -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); diff --git a/test/sequential/test-child-process-execsync.js b/test/sequential/test-child-process-execsync.js index 359799dd459e98..76da39c0e6b12d 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'}); }, + /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});