Skip to content

Commit 2a3009f

Browse files
committed
util: use primordials consistently in inspect.js
Replace native array methods (.push, .pop, .splice, .slice) with primordials (ArrayPrototypePush, ArrayPrototypePop, ArrayPrototypeSplice, ArrayPrototypeSlice) throughout lib/internal/util/inspect.js for consistency and security. This improves protection against prototype pollution and aligns with the primordials pattern used throughout the codebase.
1 parent 05d6b9b commit 2a3009f

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

lib/internal/util/inspect.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ inspect.styles = ObjectAssign({ __proto__: null }, {
521521
// Define the palette for RegExp group depth highlighting. Can be changed by users.
522522
inspect.styles.regexp.colors = ['green', 'red', 'yellow', 'cyan', 'magenta'];
523523

524-
const highlightRegExpColors = inspect.styles.regexp.colors.slice();
524+
const highlightRegExpColors = ArrayPrototypeSlice(inspect.styles.regexp.colors);
525525

526526
/**
527527
* Colorize a JavaScript RegExp pattern per ECMAScript grammar.
@@ -549,7 +549,7 @@ function highlightRegExp(regexpString) {
549549

550550
const palette = paletteNames.reduce((acc, name) => {
551551
const color = inspect.colors[name];
552-
if (color) acc.push([`\u001b[${color[0]}m`, `\u001b[${color[1]}m`]);
552+
if (color) ArrayPrototypePush(acc, [`\u001b[${color[0]}m`, `\u001b[${color[1]}m`]);
553553
return acc;
554554
}, []);
555555

@@ -1424,7 +1424,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
14241424
}
14251425
recurseTimes += 1;
14261426

1427-
ctx.seen.push(value);
1427+
ArrayPrototypePush(ctx.seen, value);
14281428
ctx.currentDepth = recurseTimes;
14291429
let output;
14301430
const indentationLvl = ctx.indentationLvl;
@@ -1468,7 +1468,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
14681468
}
14691469
}
14701470
}
1471-
ctx.seen.pop();
1471+
ArrayPrototypePop(ctx.seen);
14721472

14731473
if (ctx.sorted) {
14741474
const comparator = ctx.sorted === true ? undefined : ctx.sorted;
@@ -1520,7 +1520,7 @@ function getBoxedBase(value, ctx, keys, constructor, tag) {
15201520
// For boxed Strings, we have to remove the 0-n indexed entries,
15211521
// since they just noisy up the output and are redundant
15221522
// Make boxed primitive Strings look like such
1523-
keys.splice(0, value.length);
1523+
ArrayPrototypeSplice(keys, 0, value.length);
15241524
} else if (isBooleanObject(value)) {
15251525
fn = BooleanPrototypeValueOf;
15261526
type = 'Boolean';
@@ -1717,7 +1717,7 @@ function getDuplicateErrorFrameRanges(frames) {
17171717
maxRange = range;
17181718
maxDuplicates = duplicateRanges;
17191719
}
1720-
range = extraSteps.pop();
1720+
range = ArrayPrototypePop(extraSteps);
17211721
nextStart = i;
17221722
duplicateRanges = 0;
17231723
continue;
@@ -1731,7 +1731,7 @@ function getDuplicateErrorFrameRanges(frames) {
17311731
}
17321732

17331733
if (duplicateRanges * range >= 3) {
1734-
result.push(i + range, range, duplicateRanges);
1734+
ArrayPrototypePush(result, i + range, range, duplicateRanges);
17351735
// Skip over the collapsed portion to avoid overlapping matches.
17361736
i += range * (duplicateRanges + 1) - 1;
17371737
}
@@ -1751,11 +1751,11 @@ function getStackString(ctx, error) {
17511751
if (typeof stack === 'string') {
17521752
return stack;
17531753
}
1754-
ctx.seen.push(error);
1754+
ArrayPrototypePush(ctx.seen, error);
17551755
ctx.indentationLvl += 4;
17561756
const result = formatValue(ctx, stack);
17571757
ctx.indentationLvl -= 4;
1758-
ctx.seen.pop();
1758+
ArrayPrototypePop(ctx.seen);
17591759
return `${ErrorPrototypeToString(error)}\n ${result}`;
17601760
}
17611761
return ErrorPrototypeToString(error);
@@ -1781,7 +1781,7 @@ function getStackFrames(ctx, err, stack) {
17811781
if (len > 0) {
17821782
const skipped = len - 2;
17831783
const msg = ` ... ${skipped} lines matching cause stack trace ...`;
1784-
frames.splice(offset + 1, skipped, ctx.stylize(msg, 'undefined'));
1784+
ArrayPrototypeSplice(frames, offset + 1, skipped, ctx.stylize(msg, 'undefined'));
17851785
}
17861786
}
17871787
}
@@ -1800,7 +1800,7 @@ function getStackFrames(ctx, err, stack) {
18001800
(duplicateRanges > 1 ?
18011801
`${length} lines ${duplicateRanges} times...` :
18021802
'lines ...');
1803-
frames.splice(offset, length * duplicateRanges, ctx.stylize(msg, 'undefined'));
1803+
ArrayPrototypeSplice(frames, offset, length * duplicateRanges, ctx.stylize(msg, 'undefined'));
18041804
}
18051805
}
18061806

@@ -2153,7 +2153,7 @@ function groupArrayElements(ctx, output, value) {
21532153
}
21542154

21552155
function handleMaxCallStackSize(ctx, err, constructorName, indentationLvl) {
2156-
ctx.seen.pop();
2156+
ArrayPrototypePop(ctx.seen);
21572157
ctx.indentationLvl = indentationLvl;
21582158
return ctx.stylize(
21592159
`[${constructorName}: Inspection interrupted ` +

0 commit comments

Comments
 (0)