From 2e1e02a77aa6e86473ac9900b52eab1fbf247044 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sat, 23 Jan 2021 09:53:03 +0100 Subject: [PATCH] lib: refactor to avoid unsafe array iteration PR-URL: https://github.com/nodejs/node/pull/37029 Reviewed-By: Darshan Sen Reviewed-By: Rich Trott Reviewed-By: Minwoo Jung Reviewed-By: Zijian Liu --- lib/internal/process/per_thread.js | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/lib/internal/process/per_thread.js b/lib/internal/process/per_thread.js index 4fdf0ba8764455..c5ec2609bcfa8b 100644 --- a/lib/internal/process/per_thread.js +++ b/lib/internal/process/per_thread.js @@ -5,6 +5,7 @@ // thread and the worker threads. const { + ArrayPrototypeEvery, ArrayPrototypeMap, ArrayPrototypePush, ArrayPrototypeSplice, @@ -258,27 +259,24 @@ function buildAllowedFlags() { const { options, aliases } = require('internal/options'); const allowedNodeEnvironmentFlags = []; - for (const [name, info] of options) { + for (const { 0: name, 1: info } of options) { if (info.envVarSettings === kAllowedInEnvironment) { ArrayPrototypePush(allowedNodeEnvironmentFlags, name); } } - for (const [ from, expansion ] of aliases) { - let isAccepted = true; - for (const to of expansion) { - if (!StringPrototypeStartsWith(to, '-') || to === '--') continue; - const recursiveExpansion = aliases.get(to); - if (recursiveExpansion) { - if (recursiveExpansion[0] === to) - ArrayPrototypeSplice(recursiveExpansion, 0, 1); - ArrayPrototypePush(expansion, ...recursiveExpansion); - continue; - } - isAccepted = options.get(to).envVarSettings === kAllowedInEnvironment; - if (!isAccepted) break; + function isAccepted(to) { + if (!StringPrototypeStartsWith(to, '-') || to === '--') return true; + const recursiveExpansion = aliases.get(to); + if (recursiveExpansion) { + if (recursiveExpansion[0] === to) + ArrayPrototypeSplice(recursiveExpansion, 0, 1); + return ArrayPrototypeEvery(recursiveExpansion, isAccepted); } - if (isAccepted) { + return options.get(to).envVarSettings === kAllowedInEnvironment; + } + for (const { 0: from, 1: expansion } of aliases) { + if (ArrayPrototypeEvery(expansion, isAccepted)) { let canonical = from; if (StringPrototypeEndsWith(canonical, '=')) canonical = StringPrototypeSlice(canonical, 0, canonical.length - 1);