Skip to content

Commit

Permalink
child_process: don't fork bomb ourselves from -e
Browse files Browse the repository at this point in the history
Remove the `-e` argument from process.execArgv in child_process.fork()
to keep `node -e 'require("child_process").fork("empty.js")'` from
spawning itself recursively.

Fixes: #3574
PR-URL: #3575
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
bnoordhuis authored and jasnell committed Dec 17, 2015
1 parent 4c382e7 commit 236ad90
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ exports.fork = function(modulePath /*, args, options*/) {

// Prepare arguments for fork:
execArgv = options.execArgv || process.execArgv;

if (execArgv === process.execArgv && process._eval != null) {
const index = execArgv.lastIndexOf(process._eval);
if (index > 0) {
// Remove the -e switch to avoid fork bombing ourselves.
execArgv = execArgv.slice();
execArgv.splice(index - 1, 2);
}
}

args = execArgv.concat([modulePath], args);

// Leave stdin open for the IPC channel. stdout and stderr should be the
Expand Down
9 changes: 9 additions & 0 deletions test/parallel/test-cli-eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ if (module.parent) {
var common = require('../common'),
assert = require('assert'),
child = require('child_process'),
path = require('path'),
nodejs = '"' + process.execPath + '"';


Expand Down Expand Up @@ -75,3 +76,11 @@ child.exec(nodejs + ' --use-strict -p process.execArgv',
function(status, stdout, stderr) {
assert.equal(stdout, "[ '--use-strict', '-p', 'process.execArgv' ]\n");
});

// Regression test for https://github.com/nodejs/node/issues/3574
const emptyFile = path.join(common.fixturesDir, 'empty.js');
child.exec(nodejs + ` -e 'require("child_process").fork("${emptyFile}")'`,
function(status, stdout, stderr) {
assert.equal(stdout, '');
assert.equal(stderr, '');
});

0 comments on commit 236ad90

Please sign in to comment.