From 3b767b86bded42d7b85e19f39264d1c21fdd3bfe Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Mon, 6 Jun 2016 13:06:56 +0100 Subject: [PATCH] buffer: fix creating from zero-length ArrayBuffer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes regression where creating a new Buffer from an empty ArrayBuffer would fail. Ref: https://github.com/nodejs/node/commit/85ab4a5f1281c4e1dd06450ac7bd3250326267fa PR-URL: https://github.com/nodejs/node/pull/7176 Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Сковорода Никита Андреевич Reviewed-By: Ron Korving --- lib/buffer.js | 2 +- test/parallel/test-buffer-alloc.js | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/buffer.js b/lib/buffer.js index 3be54669619567..a5f7ec5ffbb7b1 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -218,7 +218,7 @@ function fromArrayBuffer(obj, byteOffset, length) { const maxLength = obj.byteLength - byteOffset; - if (maxLength <= 0) + if (maxLength < 0) throw new RangeError("'offset' is out of bounds"); if (length === undefined) { diff --git a/test/parallel/test-buffer-alloc.js b/test/parallel/test-buffer-alloc.js index c06c1107039cb2..b771054bdd0656 100644 --- a/test/parallel/test-buffer-alloc.js +++ b/test/parallel/test-buffer-alloc.js @@ -1458,3 +1458,8 @@ const ubuf = Buffer.allocUnsafeSlow(10); assert(ubuf); assert(ubuf.buffer); assert.equal(ubuf.buffer.byteLength, 10); + +// Regression test +assert.doesNotThrow(() => { + Buffer.from(new ArrayBuffer()); +});