@@ -65,17 +65,18 @@ const {
6565const {
6666 codes : {
6767 ERR_BUFFER_OUT_OF_BOUNDS ,
68- ERR_OUT_OF_RANGE ,
6968 ERR_INVALID_ARG_TYPE ,
7069 ERR_INVALID_ARG_VALUE ,
7170 ERR_INVALID_BUFFER_SIZE ,
7271 ERR_INVALID_OPT_VALUE ,
73- ERR_NO_LONGER_SUPPORTED ,
7472 ERR_UNKNOWN_ENCODING
7573 } ,
7674 hideStackFrames
7775} = require ( 'internal/errors' ) ;
78- const { validateString } = require ( 'internal/validators' ) ;
76+ const {
77+ validateInt32,
78+ validateString
79+ } = require ( 'internal/validators' ) ;
7980
8081const {
8182 FastBuffer,
@@ -445,7 +446,7 @@ Buffer.concat = function concat(list, length) {
445446 }
446447 }
447448 } else {
448- length = length >>> 0 ;
449+ validateInt32 ( length , ' length' , 0 ) ;
449450 }
450451
451452 const buffer = Buffer . allocUnsafe ( length ) ;
@@ -700,35 +701,27 @@ Buffer.prototype.compare = function compare(target,
700701
701702 if ( targetStart === undefined )
702703 targetStart = 0 ;
703- else if ( targetStart < 0 )
704- throw new ERR_OUT_OF_RANGE ( 'targetStart' , '>= 0' , targetStart ) ;
705704 else
706- targetStart >>>= 0 ;
705+ validateInt32 ( targetStart , 'targetStart' , 0 ) ;
707706
708707 if ( targetEnd === undefined )
709708 targetEnd = target . length ;
710- else if ( targetEnd > target . length )
711- throw new ERR_OUT_OF_RANGE ( 'targetEnd' , `<= ${ target . length } ` , targetEnd ) ;
712709 else
713- targetEnd >>>= 0 ;
710+ validateInt32 ( targetEnd , 'targetEnd' , 0 , target . length ) ;
714711
715712 if ( sourceStart === undefined )
716713 sourceStart = 0 ;
717- else if ( sourceStart < 0 )
718- throw new ERR_OUT_OF_RANGE ( 'sourceStart' , '>= 0' , sourceStart ) ;
719714 else
720- sourceStart >>>= 0 ;
715+ validateInt32 ( sourceStart , 'sourceStart' , 0 ) ;
721716
722717 if ( sourceEnd === undefined )
723718 sourceEnd = this . length ;
724- else if ( sourceEnd > this . length )
725- throw new ERR_OUT_OF_RANGE ( 'sourceEnd' , `<= ${ this . length } ` , sourceEnd ) ;
726719 else
727- sourceEnd >>>= 0 ;
720+ validateInt32 ( sourceEnd , 'sourceEnd' , 0 , this . length ) ;
728721
729722 if ( sourceStart >= sourceEnd )
730723 return ( targetStart >= targetEnd ? 0 : - 1 ) ;
731- else if ( targetStart >= targetEnd )
724+ if ( targetStart >= targetEnd )
732725 return 1 ;
733726
734727 return compareOffset ( this , target , targetStart , sourceStart , targetEnd ,
@@ -867,17 +860,13 @@ function _fill(buf, value, offset, end, encoding) {
867860 offset = 0 ;
868861 end = buf . length ;
869862 } else {
863+ validateInt32 ( offset , 'offset' , 0 ) ;
870864 // 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 ) ;
873865 if ( end === undefined ) {
874866 end = buf . length ;
875867 } 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 ) ;
879869 }
880- offset = offset >>> 0 ;
881870 if ( offset >= end )
882871 return buf ;
883872 }
@@ -896,39 +885,37 @@ Buffer.prototype.write = function write(string, offset, length, encoding) {
896885 // Buffer#write(string);
897886 if ( offset === undefined ) {
898887 return this . utf8Write ( string , 0 , this . length ) ;
899-
888+ }
900889 // Buffer#write(string, encoding)
901- } else if ( length === undefined && typeof offset === 'string' ) {
890+ if ( length === undefined && typeof offset === 'string' ) {
902891 encoding = offset ;
903892 length = this . length ;
904893 offset = 0 ;
905894
906895 // 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 ;
911899 } else {
912- encoding = length ;
913- length = undefined ;
900+ validateInt32 ( offset , 'offset' , 0 , this . length ) ;
914901 }
915902
916903 const remaining = this . length - offset ;
917- if ( length === undefined || length > remaining )
918- length = remaining ;
919904
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+ }
929915 }
930916
931- if ( ! encoding ) return this . utf8Write ( string , offset , length ) ;
917+ if ( ! encoding )
918+ return this . utf8Write ( string , offset , length ) ;
932919
933920 encoding += '' ;
934921 switch ( encoding . length ) {
0 commit comments