From: Michael Williamson Date: Thu, 11 Apr 2013 20:30:06 +0000 (+0100) Subject: Fix: buffer.copy ignores sourceEnd if it's set to 0 X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=03211391a56ada98ca69260de1cb7606076b7dd2;p=buffer.git Fix: buffer.copy ignores sourceEnd if it's set to 0 --- 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(); +});