Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit e17c5a7

Browse files
sam-githubtrevnorris
authored andcommitted
child_process: check execFile args is an array
execFile and spawn have same API signature with respect to optional arg array and optional options object, they should have same behaviour with respect to argument validation. PR-URL: #8454 Reviewed-by: Trevor Norris <[email protected]>
1 parent 2ff29cc commit e17c5a7

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

lib/child_process.js

+20-10
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ exports.exec = function(command /*, options, callback */) {
591591

592592

593593
exports.execFile = function(file /* args, options, callback */) {
594-
var args, optionArg, callback;
594+
var args = [], optionArg, callback;
595595
var options = {
596596
encoding: 'utf8',
597597
timeout: 0,
@@ -601,18 +601,28 @@ exports.execFile = function(file /* args, options, callback */) {
601601
env: null
602602
};
603603

604-
// Parse the parameters.
604+
// Parse the optional positional parameters.
605+
var pos = 1;
606+
if (pos < arguments.length && Array.isArray(arguments[pos])) {
607+
args = arguments[pos++];
608+
}
609+
else if(pos < arguments.length && arguments[pos] == null) {
610+
pos++;
611+
}
605612

606-
if (typeof arguments[arguments.length - 1] === 'function') {
607-
callback = arguments[arguments.length - 1];
613+
if (pos < arguments.length && typeof arguments[pos] === 'object') {
614+
options = util._extend(options, arguments[pos++]);
615+
}
616+
else if(pos < arguments.length && arguments[pos] == null) {
617+
pos++;
608618
}
609619

610-
if (Array.isArray(arguments[1])) {
611-
args = arguments[1];
612-
options = util._extend(options, arguments[2]);
613-
} else {
614-
args = [];
615-
options = util._extend(options, arguments[1]);
620+
if (pos < arguments.length && typeof arguments[pos] === 'function') {
621+
callback = arguments[pos++];
622+
}
623+
624+
if (pos === 1 && arguments.length > 1) {
625+
throw new TypeError('Incorrect value of args option');
616626
}
617627

618628
var child = spawn(file, args, {

test/simple/test-child-process-spawn-typeerror.js

+11
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
var assert = require('assert');
2323
var child_process = require('child_process');
2424
var spawn = child_process.spawn;
25+
var execFile = child_process.execFile;
2526
var cmd = (process.platform === 'win32') ? 'dir' : 'ls';
2627

2728

@@ -34,3 +35,13 @@ assert.throws(function() {
3435
assert.doesNotThrow(function() {
3536
spawn(cmd, {});
3637
});
38+
39+
40+
// verify that execFile has same argument parsing behaviour as spawn
41+
assert.throws(function() {
42+
execFile(cmd, 'this is not an array');
43+
}, TypeError);
44+
45+
assert.doesNotThrow(function() {
46+
execFile(cmd, {});
47+
});

0 commit comments

Comments
 (0)