Skip to content

Commit 612dfeb

Browse files
robertchirasMylesBorins
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. Ref: #9152 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 408a585 commit 612dfeb

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
@@ -479,7 +479,7 @@ function execFileSync(/*command, args, options*/) {
479479

480480
var ret = spawnSync(opts.file, opts.args.slice(1), opts.options);
481481

482-
if (inheritStderr)
482+
if (inheritStderr && ret.stderr)
483483
process.stderr.write(ret.stderr);
484484

485485
var err = checkExecSyncError(ret);
@@ -499,7 +499,7 @@ function execSync(/*command, options*/) {
499499
var ret = spawnSync(opts.file, opts.args, opts.options);
500500
ret.cmd = opts.cmd;
501501

502-
if (inheritStderr)
502+
if (inheritStderr && ret.stderr)
503503
process.stderr.write(ret.stderr);
504504

505505
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+
/ENOENT/,
19+
'execSync did not throw the expected exception!'
20+
);
21+
assert.throws(
22+
function() { execFileSync('exit -1', {shell: 'bad_shell'}); },
23+
/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)