From ea89fdfec4f7a6ff180149a5eacc3c7d84b87e39 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sat, 19 Jul 2014 22:35:07 -0400 Subject: [PATCH] child_process: do not access stderr when stdio set to 'ignore' Currently, checkExecSyncError() attempts to access the contents of stderr. When stdio is set to 'ignore', this causes a crash. This commit adds a check on the access of stderr. Closes #7966. Signed-off-by: Fedor Indutny --- lib/child_process.js | 8 ++++---- test/simple/test-child-process-execsync.js | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/child_process.js b/lib/child_process.js index 7bcd6d95f8..aadac2ed28 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -1323,10 +1323,10 @@ function checkExecSyncError(ret) { ret.error = null; if (!err) { - var cmd = ret.cmd ? ret.cmd : ret.args.join(' '); - err = new Error(util.format('Command failed: %s\n%s', - cmd, - ret.stderr.toString())); + var msg = 'Command failed: ' + + (ret.cmd ? ret.cmd : ret.args.join(' ')) + + (ret.stderr ? '\n' + ret.stderr.toString() : ''); + err = new Error(msg); } util._extend(err, ret); diff --git a/test/simple/test-child-process-execsync.js b/test/simple/test-child-process-execsync.js index 6f17ab31e7..28fa0200f2 100644 --- a/test/simple/test-child-process-execsync.js +++ b/test/simple/test-child-process-execsync.js @@ -96,3 +96,10 @@ assert.strictEqual(ret, msg + '\n', 'execFileSync encoding result should match') assert.strictEqual(response.toString().trim(), cwd); })(); + +// Verify that stderr is not accessed when stdio = 'ignore' - GH #7966 +(function() { + assert.throws(function() { + execSync('exit -1', {stdio: 'ignore'}); + }, /Command failed: exit -1/); +})();