From a0d6b15f62e59634c28e880a9404f6c940813ea7 Mon Sep 17 00:00:00 2001 From: Shobhit Chittora Date: Tue, 22 May 2018 13:46:56 +0530 Subject: [PATCH 1/5] child_process: adds normalization for fork function arguments Closes: #20749 --- lib/child_process.js | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/lib/child_process.js b/lib/child_process.js index 6cce1b09e565..7b720e860c6e 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -58,24 +58,38 @@ function stdioStringToArray(option) { } } +function normalizeForkArguments(args, options) { + if (Array.isArray(args)) { + args = args.slice(0); + } else if (options !== undefined && + (args === undefined || + args === null || + typeof args !== 'object') + ) { + throw new ERR_INVALID_ARG_TYPE('args', 'array', args); + } else { + options = args; + args = []; + } + + if (options === undefined) + options = {}; + else if (options === null || typeof options !== 'object') + throw new ERR_INVALID_ARG_TYPE('options', 'object', options); + + return { + args, + options + }; +} + 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]); - } - - options = util._extend({}, arguments[pos++]); - } + var opts = normalizeForkArguments(arguments[1], arguments[2]); + var options = opts.options; + var args = opts.args; // Prepare arguments for fork: execArgv = options.execArgv || process.execArgv; From 7a31575617590a38c7033d77b67eb02825e26c40 Mon Sep 17 00:00:00 2001 From: Shobhit Chittora Date: Tue, 22 May 2018 14:41:28 +0530 Subject: [PATCH 2/5] child_process: handles options === null refs: #20749 --- lib/child_process.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/child_process.js b/lib/child_process.js index 7b720e860c6e..199d7669bcb4 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -66,16 +66,20 @@ function normalizeForkArguments(args, options) { args === null || typeof args !== 'object') ) { - throw new ERR_INVALID_ARG_TYPE('args', 'array', args); + // throw new ERR_INVALID_ARG_TYPE('args', 'object', args); + args = []; } else { options = args; args = []; } - if (options === undefined) + if (options === undefined) { options = {}; - else if (options === null || typeof options !== 'object') + } else if (typeof options !== 'object') { throw new ERR_INVALID_ARG_TYPE('options', 'object', options); + } else { + options = {}; + } return { args, From e1199c263e69d46ae5485ee5799323e36bd6b0b8 Mon Sep 17 00:00:00 2001 From: Shobhit Chittora Date: Thu, 24 May 2018 18:04:03 +0530 Subject: [PATCH 3/5] child_process: fixing indentation + refactored options check for null. refs: #20749 --- lib/child_process.js | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/lib/child_process.js b/lib/child_process.js index 199d7669bcb4..913a5c2cbc80 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -62,23 +62,20 @@ function normalizeForkArguments(args, options) { if (Array.isArray(args)) { args = args.slice(0); } else if (options !== undefined && - (args === undefined || - args === null || - typeof args !== 'object') + (args === undefined || + args === null || + typeof args !== 'object') ) { - // throw new ERR_INVALID_ARG_TYPE('args', 'object', args); args = []; } else { options = args; args = []; } - if (options === undefined) { + if (options === undefined || options === null) { options = {}; } else if (typeof options !== 'object') { - throw new ERR_INVALID_ARG_TYPE('options', 'object', options); - } else { - options = {}; + throw new ERR_INVALID_ARG_VALUE('options', 'object', options); } return { @@ -400,7 +397,7 @@ const _deprecatedCustomFds = deprecate( return fd === -1 ? 'pipe' : fd; }); }, 'child_process: options.customFds option is deprecated. ' + - 'Use options.stdio instead.', 'DEP0006'); + 'Use options.stdio instead.', 'DEP0006'); function _convertCustomFds(options) { if (options.customFds && !options.stdio) { @@ -417,7 +414,7 @@ function normalizeSpawnArguments(file, args, options) { if (Array.isArray(args)) { args = args.slice(0); } else if (args !== undefined && - (args === null || typeof args !== 'object')) { + (args === null || typeof args !== 'object')) { throw new ERR_INVALID_ARG_TYPE('args', 'object', args); } else { options = args; @@ -431,13 +428,13 @@ function normalizeSpawnArguments(file, args, options) { // Validate the cwd, if present. if (options.cwd != null && - typeof options.cwd !== 'string') { + typeof options.cwd !== 'string') { throw new ERR_INVALID_ARG_TYPE('options.cwd', 'string', options.cwd); } // Validate detached, if present. if (options.detached != null && - typeof options.detached !== 'boolean') { + typeof options.detached !== 'boolean') { throw new ERR_INVALID_ARG_TYPE('options.detached', 'boolean', options.detached); } @@ -454,28 +451,28 @@ function normalizeSpawnArguments(file, args, options) { // Validate the shell, if present. if (options.shell != null && - typeof options.shell !== 'boolean' && - typeof options.shell !== 'string') { + typeof options.shell !== 'boolean' && + typeof options.shell !== 'string') { throw new ERR_INVALID_ARG_TYPE('options.shell', ['boolean', 'string'], options.shell); } // Validate argv0, if present. if (options.argv0 != null && - typeof options.argv0 !== 'string') { + typeof options.argv0 !== 'string') { throw new ERR_INVALID_ARG_TYPE('options.argv0', 'string', options.argv0); } // Validate windowsHide, if present. if (options.windowsHide != null && - typeof options.windowsHide !== 'boolean') { + typeof options.windowsHide !== 'boolean') { throw new ERR_INVALID_ARG_TYPE('options.windowsHide', 'boolean', options.windowsHide); } // Validate windowsVerbatimArguments, if present. if (options.windowsVerbatimArguments != null && - typeof options.windowsVerbatimArguments !== 'boolean') { + typeof options.windowsVerbatimArguments !== 'boolean') { throw new ERR_INVALID_ARG_TYPE('options.windowsVerbatimArguments', 'boolean', options.windowsVerbatimArguments); From 37d601942f76a04101c55e1650389ce292bf7244 Mon Sep 17 00:00:00 2001 From: Shobhit Chittora Date: Thu, 24 May 2018 18:09:11 +0530 Subject: [PATCH 4/5] child_process: removes unrelated change ( due to lint auto fix ) refs: #20749 --- lib/child_process.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/child_process.js b/lib/child_process.js index 913a5c2cbc80..52d1943bdefc 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -397,7 +397,7 @@ const _deprecatedCustomFds = deprecate( return fd === -1 ? 'pipe' : fd; }); }, 'child_process: options.customFds option is deprecated. ' + - 'Use options.stdio instead.', 'DEP0006'); + 'Use options.stdio instead.', 'DEP0006'); function _convertCustomFds(options) { if (options.customFds && !options.stdio) { @@ -414,7 +414,7 @@ function normalizeSpawnArguments(file, args, options) { if (Array.isArray(args)) { args = args.slice(0); } else if (args !== undefined && - (args === null || typeof args !== 'object')) { + (args === null || typeof args !== 'object')) { throw new ERR_INVALID_ARG_TYPE('args', 'object', args); } else { options = args; @@ -428,13 +428,13 @@ function normalizeSpawnArguments(file, args, options) { // Validate the cwd, if present. if (options.cwd != null && - typeof options.cwd !== 'string') { + typeof options.cwd !== 'string') { throw new ERR_INVALID_ARG_TYPE('options.cwd', 'string', options.cwd); } // Validate detached, if present. if (options.detached != null && - typeof options.detached !== 'boolean') { + typeof options.detached !== 'boolean') { throw new ERR_INVALID_ARG_TYPE('options.detached', 'boolean', options.detached); } @@ -451,28 +451,28 @@ function normalizeSpawnArguments(file, args, options) { // Validate the shell, if present. if (options.shell != null && - typeof options.shell !== 'boolean' && - typeof options.shell !== 'string') { + typeof options.shell !== 'boolean' && + typeof options.shell !== 'string') { throw new ERR_INVALID_ARG_TYPE('options.shell', ['boolean', 'string'], options.shell); } // Validate argv0, if present. if (options.argv0 != null && - typeof options.argv0 !== 'string') { + typeof options.argv0 !== 'string') { throw new ERR_INVALID_ARG_TYPE('options.argv0', 'string', options.argv0); } // Validate windowsHide, if present. if (options.windowsHide != null && - typeof options.windowsHide !== 'boolean') { + typeof options.windowsHide !== 'boolean') { throw new ERR_INVALID_ARG_TYPE('options.windowsHide', 'boolean', options.windowsHide); } // Validate windowsVerbatimArguments, if present. if (options.windowsVerbatimArguments != null && - typeof options.windowsVerbatimArguments !== 'boolean') { + typeof options.windowsVerbatimArguments !== 'boolean') { throw new ERR_INVALID_ARG_TYPE('options.windowsVerbatimArguments', 'boolean', options.windowsVerbatimArguments); From 529480e76773b2264a98775ec85e113b6c5fec5a Mon Sep 17 00:00:00 2001 From: Shobhit Chittora Date: Fri, 25 May 2018 11:32:34 +0530 Subject: [PATCH 5/5] child_process: inlines arg normalization in the fork function itself. refs: #20749 --- lib/child_process.js | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/lib/child_process.js b/lib/child_process.js index 52d1943bdefc..a78664c4abd4 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -58,7 +58,13 @@ function stdioStringToArray(option) { } } -function normalizeForkArguments(args, options) { +exports.fork = function fork(modulePath /* , args, options */) { + // Get options and args arguments. + var execArgv; + + var args = arguments[1]; + var options = arguments[2]; + if (Array.isArray(args)) { args = args.slice(0); } else if (options !== undefined && @@ -78,20 +84,6 @@ function normalizeForkArguments(args, options) { throw new ERR_INVALID_ARG_VALUE('options', 'object', options); } - return { - args, - options - }; -} - -exports.fork = function fork(modulePath /* , args, options */) { - - // Get options and args arguments. - var execArgv; - var opts = normalizeForkArguments(arguments[1], arguments[2]); - var options = opts.options; - var args = opts.args; - // Prepare arguments for fork: execArgv = options.execArgv || process.execArgv;