Is this a bug with node or expected behavior? Reproducible in 0.12.7 and io 3.1.0:
> new Buffer(5)
<Buffer 00 00 02 00 00>
> new Buffer(5)
<Buffer 00 00 00 00 00>
> new Buffer(5)
<Buffer 28 94 00 02 01>
> new Buffer(5)
<Buffer 00 00 00 02 00>
> new Buffer(5)
<Buffer 00 00 00 00 00>
> new Buffer(5)
<Buffer 00 98 94 00 02>
This has pretty much always been the case. Zeroing memory adds overhead, so when you create a new Buffer, you see the contents of whatever that chunk of memory contained last.
If you need/want to zero out the Buffer, just do buffer.fill(0);
UPDATE:
With node v6.0.0 the Buffer creation API has changed. You can now directly allocate a zero-filled via Buffer.alloc(size) and get the old behavior via Buffer.allocUnsafe(size). These new Buffer APIs have also been backported to v4.x in v4.5.0. The new Buffer()-style of Buffer creation is now considered deprecated and will be removed in a future major version (probably v8.0.0 at the earliest if I had to guess, to allow for a hard deprecation cycle).
Additionally, since node v5.10.0 (and v4.5.0 for v4.x branch), you can pass the --zero-fill-buffers argument to the node binary which will silently and automatically zero-fill all Buffers upon creation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With