Skip to content

Commit

Permalink
buffer: fix check for .buffer property
Browse files Browse the repository at this point in the history
isSharedArrayBuffer in fromObject was missing obj.buffer
moved the 'length' in obj check so that it is checked first making
the code slightly more performant and able to handle SharedArrayBuffer
without relying on an explicit check.

Ref: #8510
PR-URL: #8739
Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
ojss authored and Fishrock123 committed Oct 11, 2016
1 parent b579fca commit 5c10898
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ function fromObject(obj) {
}

if (obj) {
if (isArrayBuffer(obj.buffer) || 'length' in obj ||
isSharedArrayBuffer(obj)) {
if ('length' in obj || isArrayBuffer(obj.buffer) ||
isSharedArrayBuffer(obj.buffer)) {
if (typeof obj.length !== 'number' || obj.length !== obj.length) {
return new FastBuffer();
}
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-buffer-sharedarraybuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ assert.deepStrictEqual(arr_buf, ar_buf, 0);
// Checks for calling Buffer.byteLength on a SharedArrayBuffer

assert.strictEqual(Buffer.byteLength(sab), sab.byteLength, 0);

assert.doesNotThrow(() => Buffer.from({buffer: sab}));

0 comments on commit 5c10898

Please sign in to comment.