Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ function fromObject(obj) {
}

if (obj) {
if (isArrayBuffer(obj.buffer) || 'length' in obj ||
isSharedArrayBuffer(obj)) {
if ('length' in obj || isArrayBuffer(obj.buffer) ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here we go. can we not use the in check? very expensive. why not just do another typeof obj.length check?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would break hypothetical code which actually explicitly sets obj.length = undefined, right? I would be okay with that, just making sure.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. it would cause this one edge case to throw:

Buffer.from({ 0: 'a', 1: 'b', length: undefined })

since this isn't documented or tested i feel like it's a valid minor version change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I shall change that sometime today @trevnorris.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ojss If it doesn't happen here, don't sweat it. Removing in isn't directly tied to this PR.

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