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: nodejs@85ab4a5
PR-URL: nodejs#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 evanlucas committed Jul 18, 2016
1 parent f0d9610 commit 3b767b8
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 @@ -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) {
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 @@ -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());
});

0 comments on commit 3b767b8

Please sign in to comment.