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

Regarding Buffer #391

Closed
RyanZim opened this issue Mar 16, 2017 · 6 comments
Closed

Regarding Buffer #391

RyanZim opened this issue Mar 16, 2017 · 6 comments

Comments

@RyanZim
Copy link
Collaborator

RyanZim commented Mar 16, 2017

A little background:

Now, what are we going to do moving forward? Using new Buffer() emits a deprecation warning in Node v7.0.0-v7.2.1.

I'm wondering if we shouldn't consider some sort of fallback solution.


Also, this brings up the security question that prompted Node to change the Buffer API in the first place: new Buffer returns an uninitialized buffer. Currently, we are not zero-filling it. I can't see how this could be exploited in the current implementation, but it would be good to have this reviewed.

If we do not need the buffer to be initialized, we should use Buffer.allocUnsafe, not Buffer.alloc, in later versions.

Atten: @jprichardson @manidlou @dr-dimitru

@jprichardson
Copy link
Owner

To get through this mess, we should create an internal function bufferAlloc that checks for Buffer.alloc if available, and falls back to new Buffer() if not. Thoughts?

@RyanZim
Copy link
Collaborator Author

RyanZim commented Mar 16, 2017

To get through this mess, we should create an internal function bufferAlloc that checks for Buffer.alloc if available, and falls back to new Buffer() if not.

I agree, except should we be using alloc or allocUnsafe?

@jprichardson
Copy link
Owner

To err on the side of caution, my preference is alloc to prevent any potential security issues in the future. The only reason we'd need to use allocUnsafe is for performance, but it's my understanding the performance gains of allocUnsafe (not initializing mem to 0) are miniscule. It feels a little cargo-cultish, but I'm thinking alloc. This isn't a strong preference though and I could certainly be missing something in my analysis.

@RyanZim
Copy link
Collaborator Author

RyanZim commented Mar 16, 2017

@jprichardson I'm fine with alloc, but we should use new Buffer().fill(0) as a fallback then.

@dr-dimitru
Copy link
Contributor

Hi @jprichardson

Here is my snipped from #380 thread:

var _buffer = {
  allocUnsafe: function (len) {
    if (typeof Buffer.allocUnsafe === 'function') {
      try {
        // Node 4.4.* Buffer.allocUnsafe already exists, but throws exception in edge cases
        return Buffer.allocUnsafe(len);
      } catch (_error) {
        return new Buffer(len);
      }
    }
    return new Buffer(len);
  }
};

@jprichardson
Copy link
Owner

@RyanZim good point. Let's just go with allocUnsafe()

@dr-dimitru looks good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants