@@ -65,17 +65,18 @@ const {
65
65
const {
66
66
codes : {
67
67
ERR_BUFFER_OUT_OF_BOUNDS ,
68
- ERR_OUT_OF_RANGE ,
69
68
ERR_INVALID_ARG_TYPE ,
70
69
ERR_INVALID_ARG_VALUE ,
71
70
ERR_INVALID_BUFFER_SIZE ,
72
71
ERR_INVALID_OPT_VALUE ,
73
- ERR_NO_LONGER_SUPPORTED ,
74
72
ERR_UNKNOWN_ENCODING
75
73
} ,
76
74
hideStackFrames
77
75
} = require ( 'internal/errors' ) ;
78
- const { validateString } = require ( 'internal/validators' ) ;
76
+ const {
77
+ validateInt32,
78
+ validateString
79
+ } = require ( 'internal/validators' ) ;
79
80
80
81
const {
81
82
FastBuffer,
@@ -445,7 +446,7 @@ Buffer.concat = function concat(list, length) {
445
446
}
446
447
}
447
448
} else {
448
- length = length >>> 0 ;
449
+ validateInt32 ( length , ' length' , 0 ) ;
449
450
}
450
451
451
452
const buffer = Buffer . allocUnsafe ( length ) ;
@@ -700,35 +701,27 @@ Buffer.prototype.compare = function compare(target,
700
701
701
702
if ( targetStart === undefined )
702
703
targetStart = 0 ;
703
- else if ( targetStart < 0 )
704
- throw new ERR_OUT_OF_RANGE ( 'targetStart' , '>= 0' , targetStart ) ;
705
704
else
706
- targetStart >>>= 0 ;
705
+ validateInt32 ( targetStart , 'targetStart' , 0 ) ;
707
706
708
707
if ( targetEnd === undefined )
709
708
targetEnd = target . length ;
710
- else if ( targetEnd > target . length )
711
- throw new ERR_OUT_OF_RANGE ( 'targetEnd' , `<= ${ target . length } ` , targetEnd ) ;
712
709
else
713
- targetEnd >>>= 0 ;
710
+ validateInt32 ( targetEnd , 'targetEnd' , 0 , target . length ) ;
714
711
715
712
if ( sourceStart === undefined )
716
713
sourceStart = 0 ;
717
- else if ( sourceStart < 0 )
718
- throw new ERR_OUT_OF_RANGE ( 'sourceStart' , '>= 0' , sourceStart ) ;
719
714
else
720
- sourceStart >>>= 0 ;
715
+ validateInt32 ( sourceStart , 'sourceStart' , 0 ) ;
721
716
722
717
if ( sourceEnd === undefined )
723
718
sourceEnd = this . length ;
724
- else if ( sourceEnd > this . length )
725
- throw new ERR_OUT_OF_RANGE ( 'sourceEnd' , `<= ${ this . length } ` , sourceEnd ) ;
726
719
else
727
- sourceEnd >>>= 0 ;
720
+ validateInt32 ( sourceEnd , 'sourceEnd' , 0 , this . length ) ;
728
721
729
722
if ( sourceStart >= sourceEnd )
730
723
return ( targetStart >= targetEnd ? 0 : - 1 ) ;
731
- else if ( targetStart >= targetEnd )
724
+ if ( targetStart >= targetEnd )
732
725
return 1 ;
733
726
734
727
return compareOffset ( this , target , targetStart , sourceStart , targetEnd ,
@@ -867,17 +860,13 @@ function _fill(buf, value, offset, end, encoding) {
867
860
offset = 0 ;
868
861
end = buf . length ;
869
862
} else {
863
+ validateInt32 ( offset , 'offset' , 0 ) ;
870
864
// Invalid ranges are not set to a default, so can range check early.
871
- if ( offset < 0 )
872
- throw new ERR_OUT_OF_RANGE ( 'offset' , '>= 0' , offset ) ;
873
865
if ( end === undefined ) {
874
866
end = buf . length ;
875
867
} else {
876
- if ( end > buf . length || end < 0 )
877
- throw new ERR_OUT_OF_RANGE ( 'end' , `>= 0 and <= ${ buf . length } ` , end ) ;
878
- end = end >>> 0 ;
868
+ validateInt32 ( end , 'end' , 0 , buf . length ) ;
879
869
}
880
- offset = offset >>> 0 ;
881
870
if ( offset >= end )
882
871
return buf ;
883
872
}
@@ -896,39 +885,37 @@ Buffer.prototype.write = function write(string, offset, length, encoding) {
896
885
// Buffer#write(string);
897
886
if ( offset === undefined ) {
898
887
return this . utf8Write ( string , 0 , this . length ) ;
899
-
888
+ }
900
889
// Buffer#write(string, encoding)
901
- } else if ( length === undefined && typeof offset === 'string' ) {
890
+ if ( length === undefined && typeof offset === 'string' ) {
902
891
encoding = offset ;
903
892
length = this . length ;
904
893
offset = 0 ;
905
894
906
895
// Buffer#write(string, offset[, length][, encoding])
907
- } else if ( isFinite ( offset ) ) {
908
- offset = offset >>> 0 ;
909
- if ( isFinite ( length ) ) {
910
- length = length >>> 0 ;
896
+ } else {
897
+ if ( offset === undefined ) {
898
+ offset = 0 ;
911
899
} else {
912
- encoding = length ;
913
- length = undefined ;
900
+ validateInt32 ( offset , 'offset' , 0 , this . length ) ;
914
901
}
915
902
916
903
const remaining = this . length - offset ;
917
- if ( length === undefined || length > remaining )
918
- length = remaining ;
919
904
920
- if ( string . length > 0 && ( length < 0 || offset < 0 ) )
921
- throw new ERR_BUFFER_OUT_OF_BOUNDS ( ) ;
922
- } else {
923
- // If someone is still calling the obsolete form of write(), tell them.
924
- // we don't want eg buf.write("foo", "utf8", 10) to silently turn into
925
- // buf.write("foo", "utf8"), so we can't ignore extra args
926
- throw new ERR_NO_LONGER_SUPPORTED (
927
- 'Buffer.write(string, encoding, offset[, length])'
928
- ) ;
905
+ if ( length === undefined ) {
906
+ length = remaining ;
907
+ } else if ( typeof length === 'string' ) {
908
+ encoding = length ;
909
+ length = remaining ;
910
+ } else {
911
+ validateInt32 ( length , 'length' , 0 , this . length ) ;
912
+ if ( length > remaining )
913
+ length = remaining ;
914
+ }
929
915
}
930
916
931
- if ( ! encoding ) return this . utf8Write ( string , offset , length ) ;
917
+ if ( ! encoding )
918
+ return this . utf8Write ( string , offset , length ) ;
932
919
933
920
encoding += '' ;
934
921
switch ( encoding . length ) {
0 commit comments