child_process: check execFile and fork args
[platform/upstream/nodejs.git] / lib / child_process.js
index a923477..0fe9ca7 100644 (file)
@@ -23,6 +23,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]);
@@ -104,7 +106,7 @@ exports.exec = function(command /*, options, callback*/) {
 
 
 exports.execFile = function(file /*, args, options, callback*/) {
-  var args, callback;
+  var args = [], callback;
   var options = {
     encoding: 'utf8',
     timeout: 0,
@@ -114,18 +116,26 @@ 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, {