@@ -254,16 +254,8 @@ function assertSize(size) {
254
254
**/
255
255
Buffer . alloc = function alloc ( size , fill , encoding ) {
256
256
assertSize ( size ) ;
257
- if ( size > 0 && fill !== undefined ) {
258
- // Since we are filling anyway, don't zero fill initially.
259
- // Only pay attention to encoding if it's a string. This
260
- // prevents accidentally sending in a number that would
261
- // be interpreted as a start offset.
262
- if ( typeof encoding !== 'string' )
263
- encoding = undefined ;
264
- const ret = createUnsafeBuffer ( size ) ;
265
- if ( fill_ ( ret , fill , encoding ) > 0 )
266
- return ret ;
257
+ if ( fill !== undefined && size > 0 ) {
258
+ return _fill ( createUnsafeBuffer ( size ) , fill , encoding ) ;
267
259
}
268
260
return new FastBuffer ( size ) ;
269
261
} ;
@@ -834,14 +826,12 @@ Buffer.prototype.includes = function includes(val, byteOffset, encoding) {
834
826
// buffer.fill(buffer[, offset[, end]])
835
827
// buffer.fill(string[, offset[, end]][, encoding])
836
828
Buffer . prototype . fill = function fill ( val , start , end , encoding ) {
837
- fill_ ( this , val , start , end , encoding ) ;
838
- return this ;
829
+ return _fill ( this , val , start , end , encoding ) ;
839
830
} ;
840
831
841
- function fill_ ( buf , val , start , end , encoding ) {
842
- // Handle string cases:
832
+ function _fill ( buf , val , start , end , encoding ) {
843
833
if ( typeof val === 'string' ) {
844
- if ( typeof start === 'string' ) {
834
+ if ( start === undefined || typeof start === 'string' ) {
845
835
encoding = start ;
846
836
start = 0 ;
847
837
end = buf . length ;
@@ -850,46 +840,60 @@ function fill_(buf, val, start, end, encoding) {
850
840
end = buf . length ;
851
841
}
852
842
853
- if ( encoding !== undefined && typeof encoding !== 'string' ) {
854
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'encoding' ,
855
- 'string' , encoding ) ;
856
- }
857
- var normalizedEncoding = normalizeEncoding ( encoding ) ;
843
+ const normalizedEncoding = normalizeEncoding ( encoding ) ;
858
844
if ( normalizedEncoding === undefined ) {
845
+ if ( typeof encoding !== 'string' ) {
846
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'encoding' , 'string' ,
847
+ encoding ) ;
848
+ }
859
849
throw new errors . TypeError ( 'ERR_UNKNOWN_ENCODING' , encoding ) ;
860
850
}
861
851
862
852
if ( val . length === 0 ) {
863
- // Previously, if val === '', the Buffer would not fill,
864
- // which is rather surprising.
853
+ // If val === '' default to zero.
865
854
val = 0 ;
866
855
} else if ( val . length === 1 ) {
867
- var code = val . charCodeAt ( 0 ) ;
868
- if ( ( normalizedEncoding === 'utf8' && code < 128 ) ||
869
- normalizedEncoding === 'latin1' ) {
870
- // Fast path: If `val` fits into a single byte, use that numeric value.
871
- val = code ;
856
+ // Fast path: If `val` fits into a single byte, use that numeric value.
857
+ if ( normalizedEncoding === 'utf8' ) {
858
+ const code = val . charCodeAt ( 0 ) ;
859
+ if ( code < 128 ) {
860
+ val = code ;
861
+ }
862
+ } else if ( normalizedEncoding === 'latin1' ) {
863
+ val = val . charCodeAt ( 0 ) ;
872
864
}
873
865
}
874
- } else if ( typeof val === 'number' ) {
875
- val = val & 255 ;
866
+ } else {
867
+ encoding = undefined ;
876
868
}
877
869
878
- // Invalid ranges are not set to a default, so can range check early.
879
- if ( start < 0 || end > buf . length )
880
- throw new errors . RangeError ( 'ERR_INDEX_OUT_OF_RANGE' ) ;
881
-
882
- if ( end <= start )
883
- return 0 ;
884
-
885
- start = start >>> 0 ;
886
- end = end === undefined ? buf . length : end >>> 0 ;
887
- const fillLength = bindingFill ( buf , val , start , end , encoding ) ;
870
+ if ( start === undefined ) {
871
+ start = 0 ;
872
+ end = buf . length ;
873
+ } else {
874
+ // Invalid ranges are not set to a default, so can range check early.
875
+ if ( end === undefined ) {
876
+ if ( start < 0 )
877
+ throw new errors . RangeError ( 'ERR_INDEX_OUT_OF_RANGE' ) ;
878
+ end = buf . length ;
879
+ } else {
880
+ if ( start < 0 || end > buf . length || end < 0 )
881
+ throw new errors . RangeError ( 'ERR_INDEX_OUT_OF_RANGE' ) ;
882
+ end = end >>> 0 ;
883
+ }
884
+ start = start >>> 0 ;
885
+ if ( start >= end )
886
+ return buf ;
887
+ }
888
888
889
- if ( fillLength === - 1 )
890
- throw new errors . TypeError ( 'ERR_INVALID_ARG_VALUE' , 'value' , val ) ;
889
+ const res = bindingFill ( buf , val , start , end , encoding ) ;
890
+ if ( res < 0 ) {
891
+ if ( res === - 1 )
892
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_VALUE' , 'value' , val ) ;
893
+ throw new errors . RangeError ( 'ERR_INDEX_OUT_OF_RANGE' ) ;
894
+ }
891
895
892
- return fillLength ;
896
+ return buf ;
893
897
}
894
898
895
899
0 commit comments