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: don't set zero fill for zero-length buffer #2931

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const kNoZeroFill = 0;

function createPool() {
poolSize = Buffer.poolSize;
flags[kNoZeroFill] = 1;
if (poolSize > 0)
flags[kNoZeroFill] = 1;
allocPool = new Uint8Array(poolSize);
Object.setPrototypeOf(allocPool, Buffer.prototype);
poolOffset = 0;
Expand Down Expand Up @@ -64,7 +65,8 @@ Buffer.__proto__ = Uint8Array;
function SlowBuffer(length) {
if (+length != length)
length = 0;
flags[kNoZeroFill] = 1;
if (length > 0)
flags[kNoZeroFill] = 1;
const ui8 = new Uint8Array(+length);
Object.setPrototypeOf(ui8, Buffer.prototype);
return ui8;
Expand All @@ -75,8 +77,11 @@ SlowBuffer.__proto__ = Buffer;


function allocate(size) {
if (size === 0)
return SlowBuffer(0);
if (size === 0) {
const ui8 = new Uint8Array(size);
Object.setPrototypeOf(ui8, Buffer.prototype);
return ui8;
}
if (size < (Buffer.poolSize >>> 1)) {
if (size > (poolSize - poolOffset))
createPool();
Expand All @@ -85,7 +90,11 @@ function allocate(size) {
alignPool();
return b;
} else {
flags[kNoZeroFill] = 1;
// Even though this is checked above, the conditional is a safety net and
// sanity check to prevent the arraybuffer from being allocated with
// garbage.
if (size > 0)
flags[kNoZeroFill] = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should just reset the flag ourselves?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already considered that. If for some reason buffer instantiation throws, and is caught, it won't have a chance to reset the value. The likelihood of that happening is slim to none. Just being super paranoid. I can swap the impl if you think it's unnecessary.

const ui8 = new Uint8Array(size);
Object.setPrototypeOf(ui8, Buffer.prototype);
return ui8;
Expand Down
19 changes: 19 additions & 0 deletions test/parallel/test-buffer-zero-fill-reset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

require('../common');
const assert = require('assert');


function testUint8Array(ui) {
const length = ui.length;
for (let i = 0; i < length; i++)
if (ui[i] !== 0) return false;
return true;
}


for (let i = 0; i < 100; i++) {
new Buffer(0);
let ui = new Uint8Array(65);
assert.ok(testUint8Array(ui), 'Uint8Array is not zero-filled');
}