Skip to content

Commit

Permalink
util: fix inspection of typed arrays with unusual length
Browse files Browse the repository at this point in the history
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 <targos@protonmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
BridgeAR authored and codebytere committed Feb 17, 2020
1 parent 729b961 commit 5e1bee8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,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 @@ -1405,8 +1405,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 @@ -311,6 +311,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

0 comments on commit 5e1bee8

Please sign in to comment.