Skip to content

Commit 765de1a

Browse files
robertchirasFishrock123
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 257a866 commit 765de1a

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/child_process.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ function execFileSync(/*command, args, options*/) {
494494

495495
var ret = spawnSync(opts.file, opts.args.slice(1), opts.options);
496496

497-
if (inheritStderr)
497+
if (inheritStderr && ret.stderr)
498498
process.stderr.write(ret.stderr);
499499

500500
var err = checkExecSyncError(ret);
@@ -514,7 +514,7 @@ function execSync(command /*, options*/) {
514514
var ret = spawnSync(opts.file, opts.options);
515515
ret.cmd = command;
516516

517-
if (inheritStderr)
517+
if (inheritStderr && ret.stderr)
518518
process.stderr.write(ret.stderr);
519519

520520
var err = checkExecSyncError(ret);

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

Lines changed: 12 additions & 0 deletions
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)