@@ -69,12 +69,14 @@ const {
69
69
ERR_INVALID_ARG_VALUE ,
70
70
ERR_INVALID_BUFFER_SIZE ,
71
71
ERR_INVALID_OPT_VALUE ,
72
- ERR_NO_LONGER_SUPPORTED ,
73
72
ERR_UNKNOWN_ENCODING
74
73
} ,
75
74
hideStackFrames
76
75
} = require ( 'internal/errors' ) ;
77
- const { validateString } = require ( 'internal/validators' ) ;
76
+ const {
77
+ validateInteger,
78
+ validateString
79
+ } = require ( 'internal/validators' ) ;
78
80
79
81
const {
80
82
FastBuffer,
@@ -437,15 +439,15 @@ Buffer.concat = function concat(list, length) {
437
439
if ( list . length === 0 )
438
440
return new FastBuffer ( ) ;
439
441
440
- if ( length === undefined ) {
442
+ if ( length == null ) {
441
443
length = 0 ;
442
444
for ( i = 0 ; i < list . length ; i ++ ) {
443
445
if ( list [ i ] . length ) {
444
446
length += list [ i ] . length ;
445
447
}
446
448
}
447
449
} else {
448
- length = length >>> 0 ;
450
+ validateInteger ( length , ' length' ) ;
449
451
}
450
452
451
453
const buffer = Buffer . allocUnsafe ( length ) ;
@@ -691,32 +693,32 @@ Buffer.prototype.compare = function compare(target,
691
693
else if ( targetStart < 0 )
692
694
throw new ERR_OUT_OF_RANGE ( 'targetStart' , '>= 0' , targetStart ) ;
693
695
else
694
- targetStart >>>= 0 ;
696
+ validateInteger ( targetStart , 'targetStart' ) ;
695
697
696
698
if ( targetEnd === undefined )
697
699
targetEnd = target . length ;
698
700
else if ( targetEnd > target . length )
699
701
throw new ERR_OUT_OF_RANGE ( 'targetEnd' , `<= ${ target . length } ` , targetEnd ) ;
700
702
else
701
- targetEnd >>>= 0 ;
703
+ validateInteger ( targetEnd , 'targetEnd' ) ;
702
704
703
705
if ( sourceStart === undefined )
704
706
sourceStart = 0 ;
705
707
else if ( sourceStart < 0 )
706
708
throw new ERR_OUT_OF_RANGE ( 'sourceStart' , '>= 0' , sourceStart ) ;
707
709
else
708
- sourceStart >>>= 0 ;
710
+ validateInteger ( sourceStart , 'sourceStart' ) ;
709
711
710
712
if ( sourceEnd === undefined )
711
713
sourceEnd = this . length ;
712
714
else if ( sourceEnd > this . length )
713
715
throw new ERR_OUT_OF_RANGE ( 'sourceEnd' , `<= ${ this . length } ` , sourceEnd ) ;
714
716
else
715
- sourceEnd >>>= 0 ;
717
+ validateInteger ( sourceEnd , 'sourceEnd' ) ;
716
718
717
719
if ( sourceStart >= sourceEnd )
718
720
return ( targetStart >= targetEnd ? 0 : - 1 ) ;
719
- else if ( targetStart >= targetEnd )
721
+ if ( targetStart >= targetEnd )
720
722
return 1 ;
721
723
722
724
return compareOffset ( this , target , targetStart , sourceStart , targetEnd ,
@@ -861,11 +863,11 @@ function _fill(buf, value, offset, end, encoding) {
861
863
if ( end === undefined ) {
862
864
end = buf . length ;
863
865
} else {
866
+ validateInteger ( end , 'end' ) ;
864
867
if ( end > buf . length || end < 0 )
865
868
throw new ERR_OUT_OF_RANGE ( 'end' , `>= 0 and <= ${ buf . length } ` , end ) ;
866
- end = end >>> 0 ;
867
869
}
868
- offset = offset >>> 0 ;
870
+ validateInteger ( offset , ' offset' ) ;
869
871
if ( offset >= end )
870
872
return buf ;
871
873
}
@@ -884,39 +886,40 @@ Buffer.prototype.write = function write(string, offset, length, encoding) {
884
886
// Buffer#write(string);
885
887
if ( offset === undefined ) {
886
888
return this . utf8Write ( string , 0 , this . length ) ;
887
-
889
+ }
888
890
// Buffer#write(string, encoding)
889
- } else if ( length === undefined && typeof offset === 'string' ) {
891
+ if ( length === undefined && typeof offset === 'string' ) {
890
892
encoding = offset ;
891
893
length = this . length ;
892
894
offset = 0 ;
893
895
894
896
// Buffer#write(string, offset[, length][, encoding])
895
- } else if ( isFinite ( offset ) ) {
896
- offset = offset >>> 0 ;
897
- if ( isFinite ( length ) ) {
898
- length = length >>> 0 ;
897
+ } else {
898
+ if ( offset === undefined ) {
899
+ offset = 0 ;
899
900
} else {
900
- encoding = length ;
901
- length = undefined ;
901
+ validateInteger ( offset , 'offset' ) ;
902
902
}
903
903
904
904
const remaining = this . length - offset ;
905
- if ( length === undefined || length > remaining )
905
+
906
+ if ( length === undefined ) {
906
907
length = remaining ;
908
+ } else if ( typeof length === 'string' ) {
909
+ encoding = length ;
910
+ length = remaining ;
911
+ } else {
912
+ validateInteger ( length , 'length' ) ;
913
+ if ( length > remaining )
914
+ length = remaining ;
915
+ }
907
916
908
- if ( string . length > 0 && ( length < 0 || offset < 0 ) )
917
+ if ( length < 0 || offset < 0 )
909
918
throw new ERR_BUFFER_OUT_OF_BOUNDS ( ) ;
910
- } else {
911
- // If someone is still calling the obsolete form of write(), tell them.
912
- // we don't want eg buf.write("foo", "utf8", 10) to silently turn into
913
- // buf.write("foo", "utf8"), so we can't ignore extra args
914
- throw new ERR_NO_LONGER_SUPPORTED (
915
- 'Buffer.write(string, encoding, offset[, length])'
916
- ) ;
917
919
}
918
920
919
- if ( ! encoding ) return this . utf8Write ( string , offset , length ) ;
921
+ if ( ! encoding )
922
+ return this . utf8Write ( string , offset , length ) ;
920
923
921
924
encoding += '' ;
922
925
switch ( encoding . length ) {
0 commit comments