From 542ac7f3d204be52c829f25270e840933e397bc0 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Wed, 17 Sep 2014 17:26:46 -0400 Subject: child_process: properly support optional args Currently, a TypeError is incorrectly thrown if the second argument is an object. This commit allows the args argument to be properly omitted. Fixes: https://github.com/joyent/node/issues/6068 Reviewed-by: Trevor Norris --- lib/child_process.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/child_process.js') diff --git a/lib/child_process.js b/lib/child_process.js index e19f4ff89..0c1b4c99c 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -711,7 +711,7 @@ var spawn = exports.spawn = function(file /*, args, options*/) { if (Array.isArray(arguments[1])) { args = arguments[1].slice(0); options = arguments[2]; - } else if (arguments[1] && !Array.isArray(arguments[1])) { + } else if (arguments[1] && typeof arguments[1] !== 'object') { throw new TypeError('Incorrect value of args option'); } else { args = []; -- cgit v1.2.1 From e17c5a72b23f920f291d61f2780068c18768cb92 Mon Sep 17 00:00:00 2001 From: Sam Roberts Date: Thu, 25 Sep 2014 22:28:33 -0700 Subject: 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: https://github.com/joyent/node/pull/8454 Reviewed-by: Trevor Norris --- lib/child_process.js | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) (limited to 'lib/child_process.js') diff --git a/lib/child_process.js b/lib/child_process.js index 0c1b4c99c..53e0bb24f 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -591,7 +591,7 @@ exports.exec = function(command /*, options, callback */) { exports.execFile = function(file /* args, options, callback */) { - var args, optionArg, callback; + var args = [], optionArg, callback; var options = { encoding: 'utf8', timeout: 0, @@ -601,18 +601,28 @@ exports.execFile = function(file /* args, options, callback */) { env: null }; - // Parse the parameters. + // Parse the optional positional parameters. + var pos = 1; + if (pos < arguments.length && Array.isArray(arguments[pos])) { + args = arguments[pos++]; + } + else if(pos < arguments.length && arguments[pos] == null) { + pos++; + } - if (typeof arguments[arguments.length - 1] === 'function') { - callback = arguments[arguments.length - 1]; + if (pos < arguments.length && typeof arguments[pos] === 'object') { + options = util._extend(options, arguments[pos++]); + } + else if(pos < arguments.length && arguments[pos] == null) { + pos++; } - if (Array.isArray(arguments[1])) { - args = arguments[1]; - options = util._extend(options, arguments[2]); - } else { - args = []; - options = util._extend(options, arguments[1]); + if (pos < arguments.length && typeof arguments[pos] === 'function') { + callback = arguments[pos++]; + } + + if (pos === 1 && arguments.length > 1) { + throw new TypeError('Incorrect value of args option'); } var child = spawn(file, args, { -- cgit v1.2.1 From 70dafa7b624abd43432e03304d65cc527fbecc11 Mon Sep 17 00:00:00 2001 From: Sam Roberts Date: Mon, 29 Sep 2014 11:26:43 -0700 Subject: child_process: check fork args is an array Optional fork args should be type-checked with same behaviour as the equivalent argument to spawn. PR-URL: https://github.com/joyent/node/pull/8454 Reviewed-by: Trevor Norris --- lib/child_process.js | 2 ++ 1 file changed, 2 insertions(+) (limited to 'lib/child_process.js') diff --git a/lib/child_process.js b/lib/child_process.js index 53e0bb24f..640f68e61 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -525,6 +525,8 @@ exports.fork = function(modulePath /*, args, options*/) { if (Array.isArray(arguments[1])) { args = arguments[1]; options = util._extend({}, arguments[2]); + } else if (arguments[1] && typeof arguments[1] !== 'object') { + throw new TypeError('Incorrect value of args option'); } else { args = []; options = util._extend({}, arguments[1]); -- cgit v1.2.1 From a1b2875afd9affd3e0147a10f04a2daf8c598761 Mon Sep 17 00:00:00 2001 From: Trevor Norris Date: Tue, 18 Nov 2014 16:42:10 -0800 Subject: lint: fix lint issues Forgot to fix these before landing the patch. Fixes: e17c5a72 --- lib/child_process.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'lib/child_process.js') diff --git a/lib/child_process.js b/lib/child_process.js index 640f68e61..1d2afb754 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -607,15 +607,13 @@ exports.execFile = function(file /* args, options, callback */) { var pos = 1; if (pos < arguments.length && Array.isArray(arguments[pos])) { args = arguments[pos++]; - } - else if(pos < arguments.length && arguments[pos] == null) { + } else if (pos < arguments.length && arguments[pos] == null) { pos++; } if (pos < arguments.length && typeof arguments[pos] === 'object') { options = util._extend(options, arguments[pos++]); - } - else if(pos < arguments.length && arguments[pos] == null) { + } else if (pos < arguments.length && arguments[pos] == null) { pos++; } -- cgit v1.2.1