Skip to content

Commit 4af63ee

Browse files
shobhitchittoratargos
authored andcommitted
child_process: handle undefined/null for fork() args
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>
1 parent 1dd8191 commit 4af63ee

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
@@ -69,6 +69,11 @@ exports.fork = function fork(modulePath /* , args, options */) {
6969
args = arguments[pos++];
7070
}
7171

72+
if (pos < arguments.length &&
73+
(arguments[pos] === undefined || arguments[pos] === null)) {
74+
pos++;
75+
}
76+
7277
if (pos < arguments.length && arguments[pos] != null) {
7378
if (typeof arguments[pos] !== 'object') {
7479
throw new ERR_INVALID_ARG_VALUE(`arguments[${pos}]`, arguments[pos]);
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)