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 sparse array inspection #22283

Closed
wants to merge 1 commit into from
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
40 changes: 18 additions & 22 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -967,33 +967,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 @@ -345,6 +345,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