From 03211391a56ada98ca69260de1cb7606076b7dd2 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 11 Apr 2013 21:30:06 +0100 Subject: [PATCH] Fix: buffer.copy ignores sourceEnd if it's set to 0 --- index.js | 4 +++- test/buffer.js | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index a4bf90c..19aae8f 100644 --- a/index.js +++ b/index.js @@ -306,7 +306,9 @@ Buffer.prototype.slice = function(start, end) { Buffer.prototype.copy = function(target, target_start, start, end) { var source = this; start || (start = 0); - end || (end = this.length); + if (end === undefined || isNaN(end)) { + end = this.length; + } target_start || (target_start = 0); if (end < start) throw new Error('sourceEnd < sourceStart'); diff --git a/test/buffer.js b/test/buffer.js index 93b08b4..f431cc3 100644 --- a/test/buffer.js +++ b/test/buffer.js @@ -223,3 +223,12 @@ test("fill", function(t) { t.equal(b.toString('hex'), '02020202020202020202'); t.end(); }); + +test('copy() empty buffer with sourceEnd=0', function (t) { + t.plan(1); + var source = new B([42]); + var destination = new B([43]); + source.copy(destination, 0, 0, 0); + t.equal(destination.readUInt8(0), 43); + t.end(); +}); -- 2.34.1