Skip to content

Commit

Permalink
lib: refactor to avoid unsafe array iteration
Browse files Browse the repository at this point in the history
PR-URL: #37029
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
Reviewed-By: Zijian Liu <lxxyxzj@gmail.com>
  • Loading branch information
aduh95 authored and RaisinTen committed Jan 27, 2021
1 parent da3a948 commit 2e1e02a
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions lib/internal/process/per_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// thread and the worker threads.

const {
ArrayPrototypeEvery,
ArrayPrototypeMap,
ArrayPrototypePush,
ArrayPrototypeSplice,
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 2e1e02a

Please sign in to comment.