Browse Source

buffer: fix check for `.buffer` property

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: https://github.com/nodejs/node/pull/8510
PR-URL: https://github.com/nodejs/node/pull/8739
Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
v7.x
Ojas Shirekar 8 years ago
committed by James M Snell
parent
commit
58f9b6d9ca
  1. 4
      lib/buffer.js
  2. 2
      test/parallel/test-buffer-sharedarraybuffer.js

4
lib/buffer.js

@ -268,8 +268,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();
}

2
test/parallel/test-buffer-sharedarraybuffer.js

@ -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}));

Loading…
Cancel
Save