GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — buffer: combine checking range of sourceStart in `buf.copy` · nodejs/node@e457d89

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib
    • buffer.js
  • test/parallel
    • test-buffer-alloc.js
    • test-buffer-copy.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/buffer.js‎

Copy file name to clipboardExpand all lines: lib/buffer.js
+2-8Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ function _copy(source, target, targetStart, sourceStart, sourceEnd) {
222222
sourceStart = 0;
223223
} else {
224224
sourceStart = toInteger(sourceStart, 0);
225-
if (sourceStart < 0)
226-
throw new ERR_OUT_OF_RANGE('sourceStart', '>= 0', sourceStart);
225+
if (sourceStart < 0 || sourceStart > source.length)
226+
throw new ERR_OUT_OF_RANGE('sourceStart', `>= 0 && <= ${source.length}`, sourceStart);
227227
}
228228

229229
if (sourceEnd === undefined) {
@@ -237,12 +237,6 @@ function _copy(source, target, targetStart, sourceStart, sourceEnd) {
237237
if (targetStart >= target.length || sourceStart >= sourceEnd)
238238
return 0;
239239

240-
if (sourceStart > source.length) {
241-
throw new ERR_OUT_OF_RANGE('sourceStart',
242-
`<= ${source.length}`,
243-
sourceStart);
244-
}
245-
246240
return _copyActual(source, target, targetStart, sourceStart, sourceEnd);
247241
}
248242

Collapse file

‎test/parallel/test-buffer-alloc.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-buffer-alloc.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ b.copy(Buffer.alloc(0), 1, 1, 1);
124124
b.copy(Buffer.alloc(1), 1, 1, 1);
125125

126126
// Try to copy 0 bytes from past the end of the source buffer
127-
b.copy(Buffer.alloc(1), 0, 2048, 2048);
127+
b.copy(Buffer.alloc(1), 0, 1024, 1024);
128128

129129
// Testing for smart defaults and ability to pass string values as offset
130130
{
Collapse file

‎test/parallel/test-buffer-copy.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-buffer-copy.js
+9-2Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,15 @@ assert.throws(
155155
{
156156
code: 'ERR_OUT_OF_RANGE',
157157
name: 'RangeError',
158-
message: 'The value of "sourceStart" is out of range. ' +
159-
'It must be >= 0. Received -1'
158+
}
159+
);
160+
161+
// Copy throws if sourceStart is greater than length of source
162+
assert.throws(
163+
() => Buffer.allocUnsafe(5).copy(Buffer.allocUnsafe(5), 0, 100),
164+
{
165+
code: 'ERR_OUT_OF_RANGE',
166+
name: 'RangeError',
160167
}
161168
);
162169

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.