summaryrefslogtreecommitdiff
path: root/lib/util.js
diff options
context:
space:
mode:
authorRuben Bridgewater <ruben@bridgewater.de>2018-08-12 22:27:45 +0200
committerRuben Bridgewater <ruben@bridgewater.de>2018-08-15 17:37:45 +0200
commit6bba368ccf43cd7bd4c003007237b55c0a16511d (patch)
tree22d5f1fa33b3ff95a91ea2080b8fadb16908dba0 /lib/util.js
parent1abc613b32d8e29f264c1703dcb6fd9eb6e51845 (diff)
downloadandroid-node-v8-6bba368ccf43cd7bd4c003007237b55c0a16511d.tar.gz
android-node-v8-6bba368ccf43cd7bd4c003007237b55c0a16511d.tar.bz2
android-node-v8-6bba368ccf43cd7bd4c003007237b55c0a16511d.zip
util: fix sparse array inspection
For very special sparse arrays it was possible that util.inspect visualized the entries not in the intended way. PR-URL: https://github.com/nodejs/node/pull/22283 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Diffstat (limited to 'lib/util.js')
-rw-r--r--lib/util.js40
1 files changed, 18 insertions, 22 deletions
diff --git a/lib/util.js b/lib/util.js
index 240489ad0d..793aa3ca16 100644
--- a/lib/util.js
+++ b/lib/util.js
@@ -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}>`;