@@ -34,9 +34,12 @@ const {
34
34
ObjectCreate,
35
35
ObjectDefineProperties,
36
36
ObjectDefineProperty,
37
+ ObjectGetOwnPropertyDescriptor,
38
+ ObjectGetPrototypeOf,
37
39
ObjectSetPrototypeOf,
38
40
SymbolSpecies,
39
41
SymbolToPrimitive,
42
+ Uint8ArrayPrototype,
40
43
} = primordials ;
41
44
42
45
const {
@@ -101,6 +104,13 @@ const {
101
104
addBufferPrototypeMethods
102
105
} = require ( 'internal/buffer' ) ;
103
106
107
+ const TypedArrayPrototype = ObjectGetPrototypeOf ( Uint8ArrayPrototype ) ;
108
+
109
+ const TypedArrayProto_byteLength =
110
+ ObjectGetOwnPropertyDescriptor ( TypedArrayPrototype ,
111
+ 'byteLength' ) . get ;
112
+ const TypedArrayFill = TypedArrayPrototype . fill ;
113
+
104
114
FastBuffer . prototype . constructor = Buffer ;
105
115
Buffer . prototype = FastBuffer . prototype ;
106
116
addBufferPrototypeMethods ( Buffer . prototype ) ;
@@ -1001,11 +1011,22 @@ function _fill(buf, value, offset, end, encoding) {
1001
1011
return buf ;
1002
1012
}
1003
1013
1004
- const res = bindingFill ( buf , value , offset , end , encoding ) ;
1005
- if ( res < 0 ) {
1006
- if ( res === - 1 )
1007
- throw new ERR_INVALID_ARG_VALUE ( 'value' , value ) ;
1008
- throw new ERR_BUFFER_OUT_OF_BOUNDS ( ) ;
1014
+
1015
+ if ( typeof value === 'number' ) {
1016
+ // OOB check
1017
+ const byteLen = TypedArrayProto_byteLength . call ( buf ) ;
1018
+ const fillLength = end - offset ;
1019
+ if ( offset > end || fillLength + offset > byteLen )
1020
+ throw new ERR_BUFFER_OUT_OF_BOUNDS ( ) ;
1021
+
1022
+ TypedArrayFill . call ( buf , value , offset , end ) ;
1023
+ } else {
1024
+ const res = bindingFill ( buf , value , offset , end , encoding ) ;
1025
+ if ( res < 0 ) {
1026
+ if ( res === - 1 )
1027
+ throw new ERR_INVALID_ARG_VALUE ( 'value' , value ) ;
1028
+ throw new ERR_BUFFER_OUT_OF_BOUNDS ( ) ;
1029
+ }
1009
1030
}
1010
1031
1011
1032
return buf ;
0 commit comments