Browse Source

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: https://github.com/nodejs/node/issues/3574
PR-URL: https://github.com/nodejs/node/pull/3575
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
process-exit-stdio-flushing
Ben Noordhuis 9 years ago
parent
commit
57bce60da3
  1. 10
      lib/child_process.js
  2. 9
      test/parallel/test-cli-eval.js

10
lib/child_process.js

@ -32,6 +32,16 @@ exports.fork = function(modulePath /*, args, options*/) {
// Prepare arguments for fork: // Prepare arguments for fork:
execArgv = options.execArgv || process.execArgv; 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); args = execArgv.concat([modulePath], args);
// Leave stdin open for the IPC channel. stdout and stderr should be the // Leave stdin open for the IPC channel. stdout and stderr should be the

9
test/parallel/test-cli-eval.js

@ -8,6 +8,7 @@ if (module.parent) {
var common = require('../common'), var common = require('../common'),
assert = require('assert'), assert = require('assert'),
child = require('child_process'), child = require('child_process'),
path = require('path'),
nodejs = '"' + process.execPath + '"'; nodejs = '"' + process.execPath + '"';
@ -75,3 +76,11 @@ child.exec(nodejs + ' --use-strict -p process.execArgv',
function(status, stdout, stderr) { function(status, stdout, stderr) {
assert.equal(stdout, "[ '--use-strict', '-p', 'process.execArgv' ]\n"); 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, '');
});

Loading…
Cancel
Save