Skip to content

Commit 9bb1024

Browse files
robertchirasevanlucas
authored andcommitted
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 <[email protected]> Reviewed-By: Robert Jefe Lindstädt <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]>
1 parent 72fc4eb commit 9bb1024

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/child_process.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ function execFileSync(/*command, args, options*/) {
489489

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

492-
if (inheritStderr)
492+
if (inheritStderr && ret.stderr)
493493
process.stderr.write(ret.stderr);
494494

495495
var err = checkExecSyncError(ret);
@@ -509,7 +509,7 @@ function execSync(command /*, options*/) {
509509
var ret = spawnSync(opts.file, opts.options);
510510
ret.cmd = command;
511511

512-
if (inheritStderr)
512+
if (inheritStderr && ret.stderr)
513513
process.stderr.write(ret.stderr);
514514

515515
var err = checkExecSyncError(ret);

test/sequential/test-child-process-execsync.js

+12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ var start = Date.now();
1212
var err;
1313
var caught = false;
1414

15+
// Verify that stderr is not accessed when a bad shell is used
16+
assert.throws(
17+
function() { execSync('exit -1', {shell: 'bad_shell'}); },
18+
/spawnSync bad_shell ENOENT/,
19+
'execSync did not throw the expected exception!'
20+
);
21+
assert.throws(
22+
function() { execFileSync('exit -1', {shell: 'bad_shell'}); },
23+
/spawnSync bad_shell ENOENT/,
24+
'execFileSync did not throw the expected exception!'
25+
);
26+
1527
try {
1628
var cmd = `"${process.execPath}" -e "setTimeout(function(){}, ${SLEEP});"`;
1729
var ret = execSync(cmd, {timeout: TIMER});

0 commit comments

Comments
 (0)