Skip to content

Commit

Permalink
buffer: don't set kNoZeroFill flag in allocUnsafe
Browse files Browse the repository at this point in the history
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: #6006
PR-URL: #6007
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: Myles Borins <[email protected]>
  • Loading branch information
vkurchatkin authored and jasnell committed Apr 2, 2016
1 parent 0605555 commit e966d1f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 0 additions & 2 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down
12 changes: 11 additions & 1 deletion test/parallel/test-buffer-safe-unsafe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)));

0 comments on commit e966d1f

Please sign in to comment.