Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Changing Buffer.length, possible to read/write to neighboring Buffers #8595

Closed
hakovala opened this issue Oct 22, 2014 · 2 comments
Closed

Comments

@hakovala
Copy link

Length of the Buffer can be changed after creating the Buffer even though Buffer has fixed size. Writing outside of the buffers initial size will override content in other neighboring Buffers.

Here's a simple code where writing to a will override data in b

var a = new Buffer('hello');
var b = new Buffer('something important')
a.length = 50;
a.fill('a')
console.log(b.toString());

Output:

aaaaaaaaaaaaaaaaaaa

Reading is also possible. Printing content of a will print also content of b.

var a = new Buffer('hello');
var b = new Buffer('something important')
a.length = 50;
console.log(a.toString());

Output:

hellosomething important�/�/
@vkurchatkin
Copy link

@hakovala buffers a really unsafe by design. Of course it's possible to make length non-writable using Object.defineProperty, but that implies performance hit. So, not modifying length is possibly the best option.

Similar issue #5378

@trevnorris
Copy link

Yes. .length is not meant to be changed, but it's not made read-only because the increase in construction time is actually quite significant.

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

No branches or pull requests

3 participants