Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,29 @@ function stdioStringToArray(option) {
}

exports.fork = function fork(modulePath /* , args, options */) {

// Get options and args arguments.
var execArgv;
var options = {};
var args = [];
var pos = 1;
if (pos < arguments.length && Array.isArray(arguments[pos])) {
args = arguments[pos++];
}

if (pos < arguments.length && arguments[pos] != null) {
if (typeof arguments[pos] !== 'object') {
throw new ERR_INVALID_ARG_VALUE(`arguments[${pos}]`, arguments[pos]);
}
var args = arguments[1];
var options = arguments[2];

@BridgeAR BridgeAR May 25, 2018 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accessing arguments without checking the length can lead to OOB accesses. That is not that bad anymore but that requires quite some work for the compiler and it still slows things down. Therefore this way of doing it is not really good.

@shobhitchittora shobhitchittora May 25, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would this be better -

var pos = 1;

var args = pos < arguments.length ? arguments[pos++] : [];
var options = pos < arguments.length ? arguments[pos++] : {};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likely. Just try to make sure each code path is not hit more than absolutely necessary.


if (Array.isArray(args)) {
args = args.slice(0);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the slice needed? Please add a test case for it and remove it if there is no need for it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure. But for consistency I took inspiration from current implementation at -

args = args.slice(0);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is not a strong argument. Please just verify this by adding a test as suggested.

} else if (options !== undefined &&
(args === undefined ||
args === null ||
typeof args !== 'object')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If args is not an object and it is not undefined or null, it should actually throw an error. Just ignoring the argument does not help anyone.

@shobhitchittora shobhitchittora May 25, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a

throw new ERR_INVALID_ARG_VALUE('arg', 'object', options);

for the code you mentioned breaks this line in test -




Should we update this test and add assert.thorws for these cases.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the test cases would continue to work but the code has to be rewritten further.

) {
args = [];
} else {
options = args;
args = [];
}

options = util._extend({}, arguments[pos++]);
if (options === undefined || options === null) {
options = {};
} else if (typeof options !== 'object') {
throw new ERR_INVALID_ARG_VALUE('options', 'object', options);
}

// Prepare arguments for fork:
Expand Down