-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
buffer: fix check of buffer and rearrange conditional #8739
Conversation
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.
Can you try adding an analogue of assert.doesNotThrow(() => Buffer.from({ buffer: arrayBuf })); line to LGTM with that |
ensure that Buffer.from works when argument is in the from '{buffer: SharedArrayBuffer}'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@@ -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) || |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
I’ll start landing this:
|
Landed in 2154bc8, thanks for contributing! |
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>
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>
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>
Checklist
make -j8 test
(UNIX), orvcbuild test nosign
(Windows) passesAffected core subsystem(s)
Description of change
refer to #8510 for more information.
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.
@addaleax please take a look.