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 <cjihrig@gmail.com> Reviewed-By: Robert Jefe Lindstädt <robert.lindstaedt@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
1 parent 72fc4eb commit 9bb1024

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

‎lib/child_process.js‎

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

490490
varret=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
varerr=checkExecSyncError(ret);
@@ -509,7 +509,7 @@ function execSync(command /*, options*/) {
509509
varret=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
varerr=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
varerr;
1313
varcaught=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+
/spawnSyncbad_shellENOENT/,
19+
'execSync did not throw the expected exception!'
20+
);
21+
assert.throws(
22+
function(){execFileSync('exit -1',{shell: 'bad_shell'});},
23+
/spawnSyncbad_shellENOENT/,
24+
'execFileSync did not throw the expected exception!'
25+
);
26+
1527
try{
1628
varcmd=`"${process.execPath}" -e "setTimeout(function(){}, ${SLEEP});"`;
1729
varret=execSync(cmd,{timeout: TIMER});

0 commit comments

Comments
 (0)