Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: fix inspection of typed arrays with unusual length #31458

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
return `${braces[0]}]`;
// Special handle the value. The original value is required below. The
// bound function is required to reconstruct missing information.
formatter = formatTypedArray.bind(null, bound);
formatter = formatTypedArray.bind(null, bound, size);
extrasType = kArrayExtrasType;
} else if (isMapIterator(value)) {
keys = getKeys(value, ctx.showHidden);
Expand Down Expand Up @@ -1401,8 +1401,8 @@ function formatArray(ctx, value, recurseTimes) {
return output;
}

function formatTypedArray(value, ctx, ignored, recurseTimes) {
const maxLength = MathMin(MathMax(0, ctx.maxArrayLength), value.length);
function formatTypedArray(value, length, ctx, ignored, recurseTimes) {
const maxLength = MathMin(MathMax(0, ctx.maxArrayLength), length);
const remaining = value.length - maxLength;
const output = new Array(maxLength);
const elementFormatter = value.length > 0 && typeof value[0] === 'number' ?
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,12 @@ assert(!/Object/.test(
);
});

{
const brokenLength = new Float32Array(2);
Object.defineProperty(brokenLength, 'length', { value: -1 });
assert.strictEqual(inspect(brokenLength), 'Float32Array(2) [ 0n, 0n ]');
}

assert.strictEqual(
util.inspect(Object.create({}, {
visible: { value: 1, enumerable: true },
Expand Down