Skip to content

Commit 51d8fbf

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 821b9ac commit 51d8fbf

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

Diff for: lib/internal/util/inspect.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
813813
return `${braces[0]}]`;
814814
// Special handle the value. The original value is required below. The
815815
// bound function is required to reconstruct missing information.
816-
formatter = formatTypedArray.bind(null, bound);
816+
formatter = formatTypedArray.bind(null, bound, size);
817817
extrasType = kArrayExtrasType;
818818
} else if (isMapIterator(value)) {
819819
keys = getKeys(value, ctx.showHidden);
@@ -1389,8 +1389,8 @@ function formatArray(ctx, value, recurseTimes) {
13891389
return output;
13901390
}
13911391

1392-
function formatTypedArray(value, ctx, ignored, recurseTimes) {
1393-
const maxLength = MathMin(MathMax(0, ctx.maxArrayLength), value.length);
1392+
function formatTypedArray(value, length, ctx, ignored, recurseTimes) {
1393+
const maxLength = MathMin(MathMax(0, ctx.maxArrayLength), length);
13941394
const remaining = value.length - maxLength;
13951395
const output = new Array(maxLength);
13961396
const elementFormatter = value.length > 0 && typeof value[0] === 'number' ?

Diff for: test/parallel/test-util-inspect.js

+6
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)