Skip to content

Commit

Permalink
child_process: handle undefined/null for fork() args
Browse files Browse the repository at this point in the history
PR-URL: #22416
Fixes: #20749
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Matheus Marchini <mat@mmarchini.me>
  • Loading branch information
shobhitchittora authored and MylesBorins committed Nov 27, 2018
1 parent b1a2960 commit 072d5fd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ exports.fork = function(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 TypeError('Incorrect value of args option');
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/child-process-echo-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
process.send({ env: process.env });
37 changes: 37 additions & 0 deletions test/parallel/test-child-process-fork-options.js
Original file line number Diff line number Diff line change
@@ -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);
}));
}

0 comments on commit 072d5fd

Please sign in to comment.