@@ -189,6 +189,21 @@ function validateOffsetLengthRead(offset, length, bufferLength) {
189
189
}
190
190
}
191
191
192
+ function validateOffsetLengthWrite ( offset , length , byteLength ) {
193
+ let err ;
194
+
195
+ if ( offset > byteLength ) {
196
+ err = new errors . RangeError ( 'ERR_OUT_OF_RANGE' , 'offset' ) ;
197
+ } else if ( offset + length > byteLength || offset + length > kMaxLength ) {
198
+ err = new errors . RangeError ( 'ERR_OUT_OF_RANGE' , 'length' ) ;
199
+ }
200
+
201
+ if ( err !== undefined ) {
202
+ Error . captureStackTrace ( err , validateOffsetLengthWrite ) ;
203
+ throw err ;
204
+ }
205
+ }
206
+
192
207
// Special case of `makeCallback()` that is specific to async `*stat()` calls as
193
208
// an optimization, since the data passed back to the callback needs to be
194
209
// transformed anyway.
@@ -826,11 +841,7 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
826
841
length = buffer . length - offset ;
827
842
if ( typeof position !== 'number' )
828
843
position = null ;
829
- const byteLength = buffer . byteLength ;
830
- if ( offset > byteLength )
831
- throw new errors . RangeError ( 'ERR_OUT_OF_RANGE' , 'offset' ) ;
832
- if ( offset + length > byteLength || offset + length > kMaxLength )
833
- throw new errors . RangeError ( 'ERR_OUT_OF_RANGE' , 'length' ) ;
844
+ validateOffsetLengthWrite ( offset , length , buffer . byteLength ) ;
834
845
return binding . writeBuffer ( fd , buffer , offset , length , position , req ) ;
835
846
}
836
847
@@ -865,11 +876,7 @@ fs.writeSync = function(fd, buffer, offset, length, position) {
865
876
offset = 0 ;
866
877
if ( typeof length !== 'number' )
867
878
length = buffer . length - offset ;
868
- const byteLength = buffer . byteLength ;
869
- if ( offset > byteLength )
870
- throw new errors . RangeError ( 'ERR_OUT_OF_RANGE' , 'offset' ) ;
871
- if ( offset + length > byteLength || offset + length > kMaxLength )
872
- throw new errors . RangeError ( 'ERR_OUT_OF_RANGE' , 'length' ) ;
879
+ validateOffsetLengthWrite ( offset , length , buffer . byteLength ) ;
873
880
return binding . writeBuffer ( fd , buffer , offset , length , position ) ;
874
881
}
875
882
if ( typeof buffer !== 'string' )
0 commit comments