From 646a211196b03ccdc2460968788bdd73ad6c2ee4 Mon Sep 17 00:00:00 2001 From: Robert Chiras Date: Thu, 19 May 2016 17:13:20 +0300 Subject: [PATCH 1/2] 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. Signed-off-by: Robert Chiras --- lib/child_process.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/child_process.js b/lib/child_process.js index 003f6569fa0792..b6768c654ad592 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); From 91ff704484873390bcebabacaa97ecf16d09f84a Mon Sep 17 00:00:00 2001 From: Robert Chiras Date: Tue, 19 Jul 2016 14:00:52 +0300 Subject: [PATCH 2/2] test: Additional test for execSync Exercise the call to execSync with a bad shell passed through options. Verify that the right exception is thrown. Signed-off-by: Robert Chiras --- test/sequential/test-child-process-execsync.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/sequential/test-child-process-execsync.js b/test/sequential/test-child-process-execsync.js index fcb923a38fcfd0..9c70e2f84180f0 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});