Skip to content

Commit

Permalink
fix(ext/node): fix Buffer.copy when sourceStart > source.length (deno…
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k committed Nov 28, 2023
1 parent 4ed9278 commit 567d7ff
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 6 deletions.
1 change: 0 additions & 1 deletion cli/tests/node_compat/config.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"test-buffer-alloc.js",
"test-buffer-arraybuffer.js",
"test-buffer-bytelength.js",
"test-buffer-copy.js",
"test-buffer-from.js",
"test-buffer-includes.js",
"test-buffer-indexof.js",
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/node_compat/test/parallel/test-buffer-alloc.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ b.copy(Buffer.alloc(0), 1, 1, 1);
b.copy(Buffer.alloc(1), 1, 1, 1);

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

// Testing for smart defaults and ability to pass string values as offset
{
Expand Down
2 changes: 0 additions & 2 deletions cli/tests/node_compat/test/parallel/test-buffer-copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ assert.throws(
}
);

/*
// Copy throws if sourceStart is greater than length of source
assert.throws(
() => Buffer.allocUnsafe(5).copy(Buffer.allocUnsafe(5), 0, 100),
Expand All @@ -174,7 +173,6 @@ assert.throws(
name: 'RangeError',
}
);
*/

{
// Check sourceEnd resets to targetEnd if former is greater than the latter
Expand Down
8 changes: 6 additions & 2 deletions ext/node/polyfills/internal/buffer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1536,8 +1536,12 @@ Buffer.prototype.copy = function copy(
sourceStart = 0;
} else {
sourceStart = toInteger(sourceStart, 0);
if (sourceStart < 0) {
throw new codes.ERR_OUT_OF_RANGE("sourceStart", ">= 0", sourceStart);
if (sourceStart < 0 || sourceStart > this.length) {
throw new codes.ERR_OUT_OF_RANGE(
"sourceStart",
`>= 0 && <= ${this.length}`,
sourceStart,
);
}
if (sourceStart >= MAX_UINT32) {
throw new codes.ERR_OUT_OF_RANGE(
Expand Down

0 comments on commit 567d7ff

Please sign in to comment.