Skip to content

Commit 4af63ee

Browse files
shobhitchittoratargos
authored andcommitted
child_process: handle undefined/null for fork() args
PR-URL: #22416Fixes: #20749 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: Matheus Marchini <[email protected]>
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‎

Lines changed: 5 additions & 0 deletions
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(typeofarguments[pos]!=='object'){
7479
thrownewERR_INVALID_ARG_VALUE(`arguments[${pos}]`,arguments[pos]);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
process.send({env: process.env});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
constfixtures=require('../common/fixtures');
4+
5+
// This test ensures that fork should parse options
6+
// correctly if args is undefined or null
7+
8+
constassert=require('assert');
9+
const{ fork }=require('child_process');
10+
11+
constexpectedEnv={foo: 'bar'};
12+
13+
{
14+
constcp=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+
constcp=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)