Skip to content

Commit 725682c

Browse files
BridgeARTrott
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 67e067e commit 725682c

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
@@ -829,7 +829,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
829829
return `${braces[0]}]`;
830830
// Special handle the value. The original value is required below. The
831831
// bound function is required to reconstruct missing information.
832-
formatter = formatTypedArray.bind(null, bound);
832+
formatter = formatTypedArray.bind(null, bound, size);
833833
extrasType = kArrayExtrasType;
834834
} else if (isMapIterator(value)) {
835835
keys = getKeys(value, ctx.showHidden);
@@ -1410,8 +1410,8 @@ function formatArray(ctx, value, recurseTimes) {
14101410
return output;
14111411
}
14121412

1413-
function formatTypedArray(value, ctx, ignored, recurseTimes) {
1414-
const maxLength = MathMin(MathMax(0, ctx.maxArrayLength), value.length);
1413+
function formatTypedArray(value, length, ctx, ignored, recurseTimes) {
1414+
const maxLength = MathMin(MathMax(0, ctx.maxArrayLength), length);
14151415
const remaining = value.length - maxLength;
14161416
const output = new Array(maxLength);
14171417
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
@@ -312,6 +312,12 @@ assert(!/Object/.test(
312312
);
313313
});
314314

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

0 commit comments

Comments
 (0)