Skip to content

Commit 5e1bee8

Browse files
BridgeARcodebytere
authored andcommitted
util: fix inspection of typed arrays with unusual length
This makes sure `util.inspect()` does not throw in case the typed array's length property was set to something invalid. Instead, always use the original information. PR-URL: #31458 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Anto Aravinth <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 729b961 commit 5e1bee8

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

lib/internal/util/inspect.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
824824
return `${braces[0]}]`;
825825
// Special handle the value. The original value is required below. The
826826
// bound function is required to reconstruct missing information.
827-
formatter = formatTypedArray.bind(null, bound);
827+
formatter = formatTypedArray.bind(null, bound, size);
828828
extrasType = kArrayExtrasType;
829829
} else if (isMapIterator(value)) {
830830
keys = getKeys(value, ctx.showHidden);
@@ -1405,8 +1405,8 @@ function formatArray(ctx, value, recurseTimes) {
14051405
return output;
14061406
}
14071407

1408-
function formatTypedArray(value, ctx, ignored, recurseTimes) {
1409-
const maxLength = MathMin(MathMax(0, ctx.maxArrayLength), value.length);
1408+
function formatTypedArray(value, length, ctx, ignored, recurseTimes) {
1409+
const maxLength = MathMin(MathMax(0, ctx.maxArrayLength), length);
14101410
const remaining = value.length - maxLength;
14111411
const output = new Array(maxLength);
14121412
const elementFormatter = value.length > 0 && typeof value[0] === 'number' ?

test/parallel/test-util-inspect.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,12 @@ assert(!/Object/.test(
311311
);
312312
});
313313

314+
{
315+
const brokenLength = new Float32Array(2);
316+
Object.defineProperty(brokenLength, 'length', { value: -1 });
317+
assert.strictEqual(inspect(brokenLength), 'Float32Array(2) [ 0n, 0n ]');
318+
}
319+
314320
assert.strictEqual(
315321
util.inspect(Object.create({}, {
316322
visible: { value: 1, enumerable: true },

0 commit comments

Comments
 (0)