From 6ff1a51c8dc005012c27d6eb3507c19f48e14e2d 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 Fixes regression where creating a new Buffer from an empty ArrayBuffer would fail. Ref: https://github.com/nodejs/node/commit/85ab4a5f1281c4e1dd06450ac7bd3250326267fa --- 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 d3cbdf66a3a21d..3c750d3b880839 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -260,7 +260,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 0f06393d6a5aaa..f68fbd621028ac 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()); +});