From e966d1f5db5e06a06de843bf6f01a4e1170531a1 Mon Sep 17 00:00:00 2001 From: Vladimir Kurchatkin Date: Sat, 2 Apr 2016 02:39:25 +0300 Subject: [PATCH] buffer: don't set `kNoZeroFill` flag in allocUnsafe If `kNoZeroFill` is set here, it won't be reset in case of pooled allocation. In case of "slow" allocation it will be set later anyway. Fixes: https://github.com/nodejs/node/issues/6006 PR-URL: https://github.com/nodejs/node/pull/6007 Reviewed-By: James M Snell Reviewed-By: Colin Ihrig Reviewed-By: Evan Lucas Reviewed-By: Myles Borins --- lib/buffer.js | 2 -- test/parallel/test-buffer-safe-unsafe.js | 12 +++++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 4c98c0cdb52ef0..6eba7502664888 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -148,8 +148,6 @@ Buffer.alloc = function(size, fill, encoding) { Buffer.allocUnsafe = function(size) { if (typeof size !== 'number') throw new TypeError('"size" argument must be a number'); - if (size > 0) - flags[kNoZeroFill] = 1; return allocate(size); }; diff --git a/test/parallel/test-buffer-safe-unsafe.js b/test/parallel/test-buffer-safe-unsafe.js index 6646f760564432..9f8b6b7410b8fb 100644 --- a/test/parallel/test-buffer-safe-unsafe.js +++ b/test/parallel/test-buffer-safe-unsafe.js @@ -7,8 +7,18 @@ const safe = Buffer.alloc(10); function isZeroFilled(buf) { for (let n = 0; n < buf.length; n++) - if (buf[n] > 0) return false; + if (buf[n] !== 0) return false; return true; } assert(isZeroFilled(safe)); + +// Test that unsafe allocations doesn't affect subsequent safe allocations +Buffer.allocUnsafe(10); +assert(isZeroFilled(new Float64Array(10))); + +new Buffer(10); +assert(isZeroFilled(new Float64Array(10))); + +Buffer.allocUnsafe(10); +assert(isZeroFilled(Buffer.alloc(10)));