diff options
author | Deokjin Kim <deokjin81.kim@gmail.com> | 2023-05-08 08:58:41 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-07 23:58:41 +0000 |
commit | 2545019451ff375b538b0ad4fe09e906c50cbc39 (patch) | |
tree | f584b307860deea4199a4a5414fd95c323d038d2 /lib | |
parent | 528aacab8def0d8c57ae05d96f0cf73d6f976f28 (diff) | |
download | node-new-2545019451ff375b538b0ad4fe09e906c50cbc39.tar.gz |
buffer: combine checking range of sourceStart in `buf.copy`
Merging 2 checking range of sourceStart into 1. Plus,
add test case to increase coverage if sourceStart is greater
than length of source.
PR-URL: https://github.com/nodejs/node/pull/47758
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/buffer.js | 10 |
1 files changed, 2 insertions, 8 deletions
diff --git a/lib/buffer.js b/lib/buffer.js index ec20e3432f..0ff7c1920a 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -222,8 +222,8 @@ function _copy(source, target, targetStart, sourceStart, sourceEnd) { sourceStart = 0; } else { sourceStart = toInteger(sourceStart, 0); - if (sourceStart < 0) - throw new ERR_OUT_OF_RANGE('sourceStart', '>= 0', sourceStart); + if (sourceStart < 0 || sourceStart > source.length) + throw new ERR_OUT_OF_RANGE('sourceStart', `>= 0 && <= ${source.length}`, sourceStart); } if (sourceEnd === undefined) { @@ -237,12 +237,6 @@ function _copy(source, target, targetStart, sourceStart, sourceEnd) { if (targetStart >= target.length || sourceStart >= sourceEnd) return 0; - if (sourceStart > source.length) { - throw new ERR_OUT_OF_RANGE('sourceStart', - `<= ${source.length}`, - sourceStart); - } - return _copyActual(source, target, targetStart, sourceStart, sourceEnd); } |