@@ -652,6 +652,9 @@ function formatSpecialArray(ctx, value, recurseTimes, keys, maxLength, valLen) {
652
652
if ( visibleLength === maxLength )
653
653
break ;
654
654
const index = + key ;
655
+ // Arrays can only have up to 2^32 - 1 entries
656
+ if ( index > 2 ** 32 - 2 )
657
+ break ;
655
658
if ( i !== index ) {
656
659
if ( ! numberRegExp . test ( key ) )
657
660
break ;
@@ -674,6 +677,8 @@ function formatSpecialArray(ctx, value, recurseTimes, keys, maxLength, valLen) {
674
677
const message = `<${ len } empty item${ ending } >` ;
675
678
output . push ( ctx . stylize ( message , 'undefined' ) ) ;
676
679
i = valLen ;
680
+ if ( keyLen === 0 )
681
+ return output ;
677
682
}
678
683
const remaining = valLen - i ;
679
684
if ( remaining > 0 ) {
@@ -687,13 +692,18 @@ function formatSpecialArray(ctx, value, recurseTimes, keys, maxLength, valLen) {
687
692
// The array is not sparse
688
693
for ( i = valLen ; i < keyLen ; i ++ )
689
694
output . push ( formatProperty ( ctx , value , recurseTimes , keys [ i ] , 2 ) ) ;
690
- } else if ( keyLen !== 0 && keys [ keyLen - 1 ] !== `${ valLen - 1 } ` ) {
691
- for ( const key of keys ) {
692
- // Skip regular indices
693
- if ( ! numberRegExp . test ( key ) ) {
694
- output . push ( formatProperty ( ctx , value , recurseTimes , key , 2 ) ) ;
695
- }
695
+ } else if ( keys [ keyLen - 1 ] !== `${ valLen - 1 } ` ) {
696
+ const extra = [ ] ;
697
+ // Only handle special keys
698
+ var key ;
699
+ for ( i = keys . length - 1 ; i >= 0 ; i -- ) {
700
+ key = keys [ i ] ;
701
+ if ( numberRegExp . test ( key ) && + key < 2 ** 32 - 1 )
702
+ break ;
703
+ extra . push ( formatProperty ( ctx , value , recurseTimes , key , 2 ) ) ;
696
704
}
705
+ for ( i = extra . length - 1 ; i >= 0 ; i -- )
706
+ output . push ( extra [ i ] ) ;
697
707
}
698
708
return output ;
699
709
}
0 commit comments