From 7e000dc8c578a932e13330868797964c744e005d Mon Sep 17 00:00:00 2001 From: Teddy Katz Date: Sun, 24 Jun 2018 00:20:17 -0700 Subject: [PATCH 1/3] util: add inspect suffix to BigInt64Array elements This commit updates `util.inspect` to add an `n` suffix to BigInts that appear in BigInt64Arrays. BigInts are formatted with an `n` suffix in most cases, but this did not occur in BigInt64Arrays due to an apparent oversight where the implementation of `inspect` for typed arrays assumed that all typed array elements are numbers. --- lib/util.js | 12 ++++++++++-- test/parallel/test-util-inspect-bigint.js | 2 ++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/util.js b/lib/util.js index f8845e4153947b..eb7d1958124ede 100644 --- a/lib/util.js +++ b/lib/util.js @@ -721,6 +721,10 @@ function formatNumber(fn, value) { return fn(`${value}`, 'number'); } +function formatBigInt(fn, value) { + return fn(`${value}n`, 'bigint'); +} + function formatPrimitive(fn, value, ctx) { if (typeof value === 'string') { if (ctx.compact === false && @@ -761,7 +765,7 @@ function formatPrimitive(fn, value, ctx) { return formatNumber(fn, value); // eslint-disable-next-line valid-typeof if (typeof value === 'bigint') - return fn(`${value}n`, 'bigint'); + return formatBigInt(fn, value); if (typeof value === 'boolean') return fn(`${value}`, 'boolean'); if (typeof value === 'undefined') @@ -897,8 +901,12 @@ function formatTypedArray(ctx, value, recurseTimes, keys) { const maxLength = Math.min(Math.max(0, ctx.maxArrayLength), value.length); const remaining = value.length - maxLength; const output = new Array(maxLength + (remaining > 0 ? 1 : 0)); + const elementFormatter = + types.isBigInt64Array(value) || types.isBigUint64Array(value) ? + formatBigInt : + formatNumber; for (var i = 0; i < maxLength; ++i) - output[i] = formatNumber(ctx.stylize, value[i]); + output[i] = elementFormatter(ctx.stylize, value[i]); if (remaining > 0) output[i] = `... ${remaining} more item${remaining > 1 ? 's' : ''}`; if (ctx.showHidden) { diff --git a/test/parallel/test-util-inspect-bigint.js b/test/parallel/test-util-inspect-bigint.js index 5e0233640e8313..92ddd6669007f4 100644 --- a/test/parallel/test-util-inspect-bigint.js +++ b/test/parallel/test-util-inspect-bigint.js @@ -10,3 +10,5 @@ const { inspect } = require('util'); assert.strictEqual(inspect(1n), '1n'); assert.strictEqual(inspect(Object(-1n)), '[BigInt: -1n]'); assert.strictEqual(inspect(Object(13n)), '[BigInt: 13n]'); +assert.strictEqual(inspect(new BigInt64Array([0n])), 'BigInt64Array [ 0n ]'); +assert.strictEqual(inspect(new BigUint64Array([0n])), 'BigUint64Array [ 0n ]'); From 2852b81a7c2096d1849690e30bbb7fb97414e16a Mon Sep 17 00:00:00 2001 From: Teddy Katz Date: Sun, 24 Jun 2018 00:34:00 -0700 Subject: [PATCH 2/3] squash: use typeof for array type check --- lib/util.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/util.js b/lib/util.js index eb7d1958124ede..82fd18e00ab8fe 100644 --- a/lib/util.js +++ b/lib/util.js @@ -901,10 +901,9 @@ function formatTypedArray(ctx, value, recurseTimes, keys) { const maxLength = Math.min(Math.max(0, ctx.maxArrayLength), value.length); const remaining = value.length - maxLength; const output = new Array(maxLength + (remaining > 0 ? 1 : 0)); - const elementFormatter = - types.isBigInt64Array(value) || types.isBigUint64Array(value) ? - formatBigInt : - formatNumber; + const elementFormatter = typeof value[0] === 'number' ? + formatNumber : + formatBigInt; for (var i = 0; i < maxLength; ++i) output[i] = elementFormatter(ctx.stylize, value[i]); if (remaining > 0) From 5be5bfd74361899a823b7655ee10b7cc9c5c3275 Mon Sep 17 00:00:00 2001 From: Teddy Katz Date: Sun, 24 Jun 2018 01:27:53 -0700 Subject: [PATCH 3/3] squash: avoid out-of-bounds reads --- lib/util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/util.js b/lib/util.js index 82fd18e00ab8fe..7c9901d1da524f 100644 --- a/lib/util.js +++ b/lib/util.js @@ -901,7 +901,7 @@ function formatTypedArray(ctx, value, recurseTimes, keys) { const maxLength = Math.min(Math.max(0, ctx.maxArrayLength), value.length); const remaining = value.length - maxLength; const output = new Array(maxLength + (remaining > 0 ? 1 : 0)); - const elementFormatter = typeof value[0] === 'number' ? + const elementFormatter = value.length > 0 && typeof value[0] === 'number' ? formatNumber : formatBigInt; for (var i = 0; i < maxLength; ++i)