Skip to content

Commit 2154bc8

Browse files
ojssaddaleax
authored andcommitted
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: #8510 PR-URL: #8739 Reviewed-By: Ilkka Myller <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 647e8e5 commit 2154bc8

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

lib/buffer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,8 @@ function fromObject(obj) {
268268
}
269269

270270
if (obj) {
271-
if (isArrayBuffer(obj.buffer) || 'length' in obj ||
272-
isSharedArrayBuffer(obj)) {
271+
if ('length' in obj || isArrayBuffer(obj.buffer) ||
272+
isSharedArrayBuffer(obj.buffer)) {
273273
if (typeof obj.length !== 'number' || obj.length !== obj.length) {
274274
return new FastBuffer();
275275
}

test/parallel/test-buffer-sharedarraybuffer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ assert.deepStrictEqual(arr_buf, ar_buf, 0);
2727
// Checks for calling Buffer.byteLength on a SharedArrayBuffer
2828

2929
assert.strictEqual(Buffer.byteLength(sab), sab.byteLength, 0);
30+
31+
assert.doesNotThrow(() => Buffer.from({buffer: sab}));

0 commit comments

Comments
 (0)