Skip to content

Commit 02ea6f4

Browse files
bnoordhuisjasnell
authored andcommitted
child_process: don't fork bomb ourselves from -e
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]>
1 parent b10433b commit 02ea6f4

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/child_process.js

+10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ exports.fork = function(modulePath /*, args, options*/) {
3232

3333
// Prepare arguments for fork:
3434
execArgv = options.execArgv || process.execArgv;
35+
36+
if (execArgv === process.execArgv && process._eval != null) {
37+
const index = execArgv.lastIndexOf(process._eval);
38+
if (index > 0) {
39+
// Remove the -e switch to avoid fork bombing ourselves.
40+
execArgv = execArgv.slice();
41+
execArgv.splice(index - 1, 2);
42+
}
43+
}
44+
3545
args = execArgv.concat([modulePath], args);
3646

3747
// Leave stdin open for the IPC channel. stdout and stderr should be the

test/parallel/test-cli-eval.js

+9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ if (module.parent) {
88
var common = require('../common'),
99
assert = require('assert'),
1010
child = require('child_process'),
11+
path = require('path'),
1112
nodejs = '"' + process.execPath + '"';
1213

1314

@@ -75,3 +76,11 @@ child.exec(nodejs + ' --use-strict -p process.execArgv',
7576
function(status, stdout, stderr) {
7677
assert.equal(stdout, "[ '--use-strict', '-p', 'process.execArgv' ]\n");
7778
});
79+
80+
// Regression test for https://github.com/nodejs/node/issues/3574
81+
const emptyFile = path.join(common.fixturesDir, 'empty.js');
82+
child.exec(nodejs + ` -e 'require("child_process").fork("${emptyFile}")'`,
83+
function(status, stdout, stderr) {
84+
assert.equal(stdout, '');
85+
assert.equal(stderr, '');
86+
});

0 commit comments

Comments
 (0)