Skip to content
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

Closed
wants to merge 2 commits into from

Conversation

ojss
Copy link
Contributor

@ojss ojss commented Sep 23, 2016

Checklist
  • make -j8 test (UNIX), or vcbuild test nosign (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)
  • buffer
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.

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.
@nodejs-github-bot nodejs-github-bot added the buffer Issues and PRs related to the buffer subsystem. label Sep 23, 2016
@addaleax
Copy link
Member

Can you try adding an analogue of test-buffer-alloc.js’s

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

line to test-buffer-sharedarraybuffer.js?

LGTM with that

ensure that Buffer.from works when argument is in the from
'{buffer: SharedArrayBuffer}'
Copy link
Member

@addaleax addaleax left a comment

Choose a reason for hiding this comment

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

Copy link
Member

@imyller imyller left a 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) ||
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.

@addaleax addaleax self-assigned this Oct 6, 2016
@addaleax
Copy link
Member

addaleax commented Oct 6, 2016

I’ll start landing this:

  • Approvals (LGTM): 2
  • No objections
  • The PR has been open for the minimum time of 48 or 72 hours
  • All of the requested changes have been made (@trevnorris indicated that his suggestions could well be left for another PR)
  • CI has only unrelated failures

@addaleax
Copy link
Member

addaleax commented Oct 6, 2016

Landed in 2154bc8, thanks for contributing!

@addaleax addaleax closed this Oct 6, 2016
addaleax pushed a commit that referenced this pull request Oct 6, 2016
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]>
jasnell pushed a commit that referenced this pull request Oct 10, 2016
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]>
Fishrock123 pushed a commit that referenced this pull request Oct 11, 2016
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]>
@MylesBorins
Copy link
Contributor

as #8510 was dont land for v4.x I'm adding the label here too.

@addaleax let me know if I am mistaken

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
buffer Issues and PRs related to the buffer subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants