Skip to content

Commit

Permalink
util: fix sparse array inspection
Browse files Browse the repository at this point in the history
For very special sparse arrays it was possible that util.inspect
visualized the entries not in the intended way.

PR-URL: #22283
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
  • Loading branch information
BridgeAR committed Aug 15, 2018
1 parent 1abc613 commit 6bba368
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
40 changes: 18 additions & 22 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -971,33 +971,29 @@ function formatNamespaceObject(ctx, value, recurseTimes, keys) {
function formatSpecialArray(ctx, value, recurseTimes, keys, maxLength, valLen) {
const output = [];
const keyLen = keys.length;
let visibleLength = 0;
let i = 0;
if (keyLen !== 0 && numberRegExp.test(keys[0])) {
for (const key of keys) {
if (visibleLength === maxLength)
for (const key of keys) {
if (output.length === maxLength)
break;
const index = +key;
// Arrays can only have up to 2^32 - 1 entries
if (index > 2 ** 32 - 2)
break;
if (`${i}` !== key) {
if (!numberRegExp.test(key))
break;
const index = +key;
// Arrays can only have up to 2^32 - 1 entries
if (index > 2 ** 32 - 2)
const emptyItems = index - i;
const ending = emptyItems > 1 ? 's' : '';
const message = `<${emptyItems} empty item${ending}>`;
output.push(ctx.stylize(message, 'undefined'));
i = index;
if (output.length === maxLength)
break;
if (i !== index) {
if (!numberRegExp.test(key))
break;
const emptyItems = index - i;
const ending = emptyItems > 1 ? 's' : '';
const message = `<${emptyItems} empty item${ending}>`;
output.push(ctx.stylize(message, 'undefined'));
i = index;
if (++visibleLength === maxLength)
break;
}
output.push(formatProperty(ctx, value, recurseTimes, key, 1));
visibleLength++;
i++;
}
output.push(formatProperty(ctx, value, recurseTimes, key, 1));
i++;
}
if (i < valLen && visibleLength !== maxLength) {
if (i < valLen && output.length !== maxLength) {
const len = valLen - i;
const ending = len > 1 ? 's' : '';
const message = `<${len} empty item${ending}>`;
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,19 @@ assert.strictEqual(
"[ <2 empty items>, '00': 1, '01': 2 ]");
assert.strictEqual(util.inspect(arr2, { showHidden: true }),
"[ <2 empty items>, [length]: 2, '00': 1, '01': 2 ]");
delete arr2['00'];
arr2[0] = 0;
assert.strictEqual(util.inspect(arr2),
"[ 0, <1 empty item>, '01': 2 ]");
assert.strictEqual(util.inspect(arr2, { showHidden: true }),
"[ 0, <1 empty item>, [length]: 2, '01': 2 ]");
delete arr2['01'];
arr2[2 ** 32 - 2] = 'max';
arr2[2 ** 32 - 1] = 'too far';
assert.strictEqual(
util.inspect(arr2),
"[ 0, <4294967293 empty items>, 'max', '4294967295': 'too far' ]"
);

const arr3 = [];
arr3[-1] = -1;
Expand Down

0 comments on commit 6bba368

Please sign in to comment.