Skip to content

Commit

Permalink
buffer: fix creating from zero-length ArrayBuffer
Browse files Browse the repository at this point in the history
Fixes regression where creating a new Buffer from an
empty ArrayBuffer would fail.

Ref: 85ab4a5
PR-URL: #7176
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Reviewed-By: Ron Korving <ron@ronkorving.nl>
  • Loading branch information
RReverser authored and addaleax committed Jun 13, 2016
1 parent 0c73c9f commit 0e9e149
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,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) {
Expand Down
5 changes: 5 additions & 0 deletions test/parallel/test-buffer-alloc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1459,3 +1459,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());
});

0 comments on commit 0e9e149

Please sign in to comment.