Skip to content

Commit 02e8c57

Browse files
committed
simpliy _copyActual, add clarifying comments
1 parent 0bc0b81 commit 02e8c57

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

lib/buffer.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ function copyImpl(source, target, targetStart, sourceStart, sourceEnd) {
210210
throw new ERR_INVALID_ARG_TYPE('source', ['Buffer', 'Uint8Array'], source);
211211
if (!ArrayBufferIsView(target))
212212
throw new ERR_INVALID_ARG_TYPE('target', ['Buffer', 'Uint8Array'], target);
213+
// Guaranteed real buffers
213214

214215
if (targetStart === undefined) {
215216
targetStart = 0;
@@ -218,6 +219,7 @@ function copyImpl(source, target, targetStart, sourceStart, sourceEnd) {
218219
if (targetStart < 0)
219220
throw new ERR_OUT_OF_RANGE('targetStart', '>= 0', targetStart);
220221
}
222+
// Guaranteed targetStart >= 0
221223

222224
if (sourceStart === undefined) {
223225
sourceStart = 0;
@@ -226,6 +228,7 @@ function copyImpl(source, target, targetStart, sourceStart, sourceEnd) {
226228
if (sourceStart < 0 || sourceStart > source.byteLength)
227229
throw new ERR_OUT_OF_RANGE('sourceStart', `>= 0 && <= ${source.byteLength}`, sourceStart);
228230
}
231+
// Guaranteed 0 <= sourceStart <= source.byteLength
229232

230233
if (sourceEnd === undefined) {
231234
sourceEnd = source.byteLength;
@@ -234,34 +237,29 @@ function copyImpl(source, target, targetStart, sourceStart, sourceEnd) {
234237
if (sourceEnd < 0)
235238
throw new ERR_OUT_OF_RANGE('sourceEnd', '>= 0', sourceEnd);
236239
}
240+
// Guaranteed 0 <= sourceEnd
237241

238242
if (targetStart >= target.byteLength || sourceStart >= sourceEnd)
239243
return 0;
240244

245+
// Guaranteed 0 <= sourceStart < sourceEnd (NO GUARANTEE <= source.byteLnegth)
246+
// Guaranteed 0 <= targetStart < target.byteLength here
247+
241248
return _copyActual(source, target, targetStart, sourceStart, sourceEnd);
242249
}
243250

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.
244257
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-
251258
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-
259259
const sourceLength = sourceEnd - sourceStart;
260260
const targetLength = target.byteLength - targetStart;
261261
const copyLength = MathMin(sourceLength, targetLength);
262-
263262
_copy(source, target, targetStart, sourceStart, copyLength);
264-
265263
return copyLength;
266264
}
267265

@@ -609,6 +607,7 @@ Buffer.concat = function concat(list, length) {
609607
throw new ERR_INVALID_ARG_TYPE(
610608
`list[${i}]`, ['Buffer', 'Uint8Array'], list[i]);
611609
}
610+
// (src, target, t_beg, s_beg, s_end)
612611
pos += _copyActual(buf, buffer, pos, 0, buf.length);
613612
}
614613

0 commit comments

Comments
 (0)