From 446cf6adabec7b7d600d84a3c757ed46829f2be5 Mon Sep 17 00:00:00 2001 From: Giovanni Date: Thu, 9 Jan 2025 09:43:21 +0100 Subject: [PATCH] child_process: refactor string validation in exec and spawn --- lib/child_process.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/child_process.js b/lib/child_process.js index 3fb21f755be3d7..99e929857fb180 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -186,9 +186,6 @@ function _forkChild(fd, serializationMode) { } function normalizeExecArgs(command, options, callback) { - validateString(command, 'command'); - validateArgumentNullCheck(command, 'command'); - if (typeof options === 'function') { callback = options; options = undefined; @@ -535,12 +532,17 @@ function copyProcessEnvToEnv(env, name, optionEnv) { } } -function normalizeSpawnArguments(file, args, options) { - validateString(file, 'file'); - validateArgumentNullCheck(file, 'file'); +function validateStringParam(param, paramName) { + validateString(param, paramName); + validateArgumentNullCheck(param, paramName); + + if (param.length === 0) { + throw new ERR_INVALID_ARG_VALUE(paramName, param, 'cannot be empty'); + } +} - if (file.length === 0) - throw new ERR_INVALID_ARG_VALUE('file', file, 'cannot be empty'); +function normalizeSpawnArguments(file, args, options) { + validateStringParam(file, 'file'); if (ArrayIsArray(args)) { args = ArrayPrototypeSlice(args);