@@ -210,6 +210,7 @@ function copyImpl(source, target, targetStart, sourceStart, sourceEnd) {
210
210
throw new ERR_INVALID_ARG_TYPE ( 'source' , [ 'Buffer' , 'Uint8Array' ] , source ) ;
211
211
if ( ! ArrayBufferIsView ( target ) )
212
212
throw new ERR_INVALID_ARG_TYPE ( 'target' , [ 'Buffer' , 'Uint8Array' ] , target ) ;
213
+ // Guaranteed real buffers
213
214
214
215
if ( targetStart === undefined ) {
215
216
targetStart = 0 ;
@@ -218,6 +219,7 @@ function copyImpl(source, target, targetStart, sourceStart, sourceEnd) {
218
219
if ( targetStart < 0 )
219
220
throw new ERR_OUT_OF_RANGE ( 'targetStart' , '>= 0' , targetStart ) ;
220
221
}
222
+ // Guaranteed targetStart >= 0
221
223
222
224
if ( sourceStart === undefined ) {
223
225
sourceStart = 0 ;
@@ -226,6 +228,7 @@ function copyImpl(source, target, targetStart, sourceStart, sourceEnd) {
226
228
if ( sourceStart < 0 || sourceStart > source . byteLength )
227
229
throw new ERR_OUT_OF_RANGE ( 'sourceStart' , `>= 0 && <= ${ source . byteLength } ` , sourceStart ) ;
228
230
}
231
+ // Guaranteed 0 <= sourceStart <= source.byteLength
229
232
230
233
if ( sourceEnd === undefined ) {
231
234
sourceEnd = source . byteLength ;
@@ -234,34 +237,29 @@ function copyImpl(source, target, targetStart, sourceStart, sourceEnd) {
234
237
if ( sourceEnd < 0 )
235
238
throw new ERR_OUT_OF_RANGE ( 'sourceEnd' , '>= 0' , sourceEnd ) ;
236
239
}
240
+ // Guaranteed 0 <= sourceEnd
237
241
238
242
if ( targetStart >= target . byteLength || sourceStart >= sourceEnd )
239
243
return 0 ;
240
244
245
+ // Guaranteed 0 <= sourceStart < sourceEnd (NO GUARANTEE <= source.byteLnegth)
246
+ // Guaranteed 0 <= targetStart < target.byteLength here
247
+
241
248
return _copyActual ( source , target , targetStart , sourceStart , sourceEnd ) ;
242
249
}
243
250
251
+ // Assumes `source`, `target` are real buffers.
252
+ // Assumes 0 <= sourceStart <= sourceEnd
253
+ // Assumes 0 <= targetStart <= target.byteLength
254
+ //
255
+ // This function clamps sourceEnd such that the source subarray will not exceed the length
256
+ // of the target subarray.
244
257
function _copyActual ( source , target , targetStart , sourceStart , sourceEnd ) {
245
- // Enforce: 0 <= sourceStart <= sourceEnd
246
- if ( ! ( 0 <= sourceStart && sourceStart <= sourceEnd ) ) {
247
- throw new ERR_BUFFER_OUT_OF_BOUNDS ( 'expected 0 <= sourceStart <= sourceEnd' +
248
- ' but got 0 <= ' + sourceStart + ' <= ' + sourceEnd ) ;
249
- }
250
-
251
258
sourceEnd = MathMin ( sourceEnd , source . byteLength ) ;
252
-
253
- // Enforce: 0 <= targetStart <= target.byteLength;
254
- if ( ! ( 0 <= targetStart && targetStart <= target . byteLength ) ) {
255
- throw new ERR_BUFFER_OUT_OF_BOUNDS ( 'expected 0 <= targetStart <= target.byteLength' +
256
- ' but got 0 <= ' + targetStart + ' <= ' + target . byteLength ) ;
257
- }
258
-
259
259
const sourceLength = sourceEnd - sourceStart ;
260
260
const targetLength = target . byteLength - targetStart ;
261
261
const copyLength = MathMin ( sourceLength , targetLength ) ;
262
-
263
262
_copy ( source , target , targetStart , sourceStart , copyLength ) ;
264
-
265
263
return copyLength ;
266
264
}
267
265
@@ -609,6 +607,7 @@ Buffer.concat = function concat(list, length) {
609
607
throw new ERR_INVALID_ARG_TYPE (
610
608
`list[${ i } ]` , [ 'Buffer' , 'Uint8Array' ] , list [ i ] ) ;
611
609
}
610
+ // (src, target, t_beg, s_beg, s_end)
612
611
pos += _copyActual ( buf , buffer , pos , 0 , buf . length ) ;
613
612
}
614
613
0 commit comments