From df5a19f4688f5146d662014ce8354b14f71cef85 Mon Sep 17 00:00:00 2001 From: Shobhit Chittora Date: Mon, 20 Aug 2018 21:32:31 +0530 Subject: [PATCH] child_process: handle undefined/null for fork() args PR-URL: https://github.com/nodejs/node/pull/22416 Fixes: https://github.com/nodejs/node/issues/20749 Reviewed-By: James M Snell Reviewed-By: Denys Otrishko Reviewed-By: Matheus Marchini --- lib/child_process.js | 5 +++ test/fixtures/child-process-echo-options.js | 1 + .../test-child-process-fork-options.js | 37 +++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 test/fixtures/child-process-echo-options.js create mode 100644 test/parallel/test-child-process-fork-options.js diff --git a/lib/child_process.js b/lib/child_process.js index bc86ab3797ad17..f08acd76c7953b 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -69,6 +69,11 @@ exports.fork = function fork(modulePath /* , args, options */) { args = arguments[pos++]; } + if (pos < arguments.length && + (arguments[pos] === undefined || arguments[pos] === null)) { + pos++; + } + if (pos < arguments.length && arguments[pos] != null) { if (typeof arguments[pos] !== 'object') { throw new ERR_INVALID_ARG_VALUE(`arguments[${pos}]`, arguments[pos]); diff --git a/test/fixtures/child-process-echo-options.js b/test/fixtures/child-process-echo-options.js new file mode 100644 index 00000000000000..04f3f8dcffe6a9 --- /dev/null +++ b/test/fixtures/child-process-echo-options.js @@ -0,0 +1 @@ +process.send({ env: process.env }); diff --git a/test/parallel/test-child-process-fork-options.js b/test/parallel/test-child-process-fork-options.js new file mode 100644 index 00000000000000..5efb9bdbb49735 --- /dev/null +++ b/test/parallel/test-child-process-fork-options.js @@ -0,0 +1,37 @@ +'use strict'; +const common = require('../common'); +const fixtures = require('../common/fixtures'); + +// This test ensures that fork should parse options +// correctly if args is undefined or null + +const assert = require('assert'); +const { fork } = require('child_process'); + +const expectedEnv = { foo: 'bar' }; + +{ + const cp = fork(fixtures.path('child-process-echo-options.js'), undefined, + { env: Object.assign({}, process.env, expectedEnv) }); + + cp.on('message', common.mustCall(({ env }) => { + assert.strictEqual(env.foo, expectedEnv.foo); + })); + + cp.on('exit', common.mustCall((code) => { + assert.strictEqual(code, 0); + })); +} + +{ + const cp = fork(fixtures.path('child-process-echo-options.js'), null, + { env: Object.assign({}, process.env, expectedEnv) }); + + cp.on('message', common.mustCall(({ env }) => { + assert.strictEqual(env.foo, expectedEnv.foo); + })); + + cp.on('exit', common.mustCall((code) => { + assert.strictEqual(code, 0); + })); +}