From 5e1bee817cbdda548a7f28a4d03803b40d716508 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Tue, 21 Jan 2020 13:56:14 +0100 Subject: [PATCH] util: fix inspection of typed arrays with unusual length MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: https://github.com/nodejs/node/pull/31458 Reviewed-By: Michaƫl Zasso Reviewed-By: Luigi Pinca Reviewed-By: Anto Aravinth Reviewed-By: James M Snell Reviewed-By: Rich Trott --- lib/internal/util/inspect.js | 6 +++--- test/parallel/test-util-inspect.js | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index c49487e5074309..f1300cf640000c 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -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); @@ -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' ? diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 1dfc7064999742..27e9dabd569b61 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -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 },