Skip to content

Commit 22b4149

Browse files
shobhitchittorarvagg
authored andcommitted
child_process: handle undefined/null for fork() args
PR-URL: #22416 Fixes: #20749 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: Matheus Marchini <[email protected]>
1 parent e8dbd09 commit 22b4149

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

lib/child_process.js

+5
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ exports.fork = function(modulePath /*, args, options*/) {
6565
args = arguments[pos++];
6666
}
6767

68+
if (pos < arguments.length &&
69+
(arguments[pos] === undefined || arguments[pos] === null)) {
70+
pos++;
71+
}
72+
6873
if (pos < arguments.length && arguments[pos] != null) {
6974
if (typeof arguments[pos] !== 'object') {
7075
throw new TypeError('Incorrect value of args option');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
process.send({ env: process.env });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
const common = require('../common');
3+
const fixtures = require('../common/fixtures');
4+
5+
// This test ensures that fork should parse options
6+
// correctly if args is undefined or null
7+
8+
const assert = require('assert');
9+
const { fork } = require('child_process');
10+
11+
const expectedEnv = { foo: 'bar' };
12+
13+
{
14+
const cp = fork(fixtures.path('child-process-echo-options.js'), undefined,
15+
{ env: Object.assign({}, process.env, expectedEnv) });
16+
17+
cp.on('message', common.mustCall(({ env }) => {
18+
assert.strictEqual(env.foo, expectedEnv.foo);
19+
}));
20+
21+
cp.on('exit', common.mustCall((code) => {
22+
assert.strictEqual(code, 0);
23+
}));
24+
}
25+
26+
{
27+
const cp = fork(fixtures.path('child-process-echo-options.js'), null,
28+
{ env: Object.assign({}, process.env, expectedEnv) });
29+
30+
cp.on('message', common.mustCall(({ env }) => {
31+
assert.strictEqual(env.foo, expectedEnv.foo);
32+
}));
33+
34+
cp.on('exit', common.mustCall((code) => {
35+
assert.strictEqual(code, 0);
36+
}));
37+
}

0 commit comments

Comments
 (0)