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: zero-sized slices have .buffer but not .parent #8266

Closed
bnoordhuis opened this issue Aug 25, 2016 · 5 comments
Closed

buffer: zero-sized slices have .buffer but not .parent #8266

bnoordhuis opened this issue Aug 25, 2016 · 5 comments
Labels
buffer Issues and PRs related to the buffer subsystem.

Comments

@bnoordhuis
Copy link
Member

Something that tripped me up earlier today. With master:

$ git rev-parse HEAD
7f1bb8044a3cb3cf8bcf525bcb4dd7d924e05fe3

$ out/Release/node
> Buffer(1).slice(0,1).parent  // one-byte slice has .parent
ArrayBuffer { byteLength: 8192 }

> Buffer(1).slice(0,0).buffer  // zero-sized slice has .buffer
ArrayBuffer { byteLength: 8192 }

> Buffer(1).slice(0,0).parent  // zero-sized slice doesn't have .parent 
undefined
@bnoordhuis bnoordhuis added the buffer Issues and PRs related to the buffer subsystem. label Aug 25, 2016
@addaleax
Copy link
Member

This seems weirdly intentional@trevnorris ?

@trevnorris
Copy link
Contributor

@bnoordhuis That is intentional. It's backwards compatible with the pre-typed array implementation. Check it against v0.12.

@bnoordhuis
Copy link
Member Author

Huh, you're right. It's weird because v0.10 doesn't work like that.

Was that an intentional change in v0.12? I can't think of a plausible reason why someone would want it to work like that, it goes against the principle of least surprise.

@jasnell
Copy link
Member

jasnell commented Aug 26, 2016

Yeah, I've also found this to be a bit odd. @trevnorris does it still have to be this way? What breaks otherwise?

@trevnorris
Copy link
Contributor

@bnoordhuis Change happened b/c of an optimization I implemented, and while writing the change making sure a zero-slice was pointing to its parent was neither documented nor tested. So must have slipped through.

@jasnell Since this change has been around for so long and this is the first bug report based on the change, and also because the use of .parent is pretty much deprecated in favor of using .buffer, I'd say this could change w/o anyone noticing.

thefourtheye added a commit to thefourtheye/io.js that referenced this issue Aug 28, 2016
If the Buffer object's length is zero, or equal to the underlying
buffer object's length, `parent` property returns `undefined`.

    > new Buffer(0).parent
    undefined
    > new Buffer(Buffer.poolSize).parent
    undefined

This patch makes the buffer objects to consistently expose the buffer
object via the `parent` property, always.

Fixes: nodejs#8266
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

No branches or pull requests

4 participants