Skip to content

Commit

Permalink
test: test buffer behavior when zeroFill undefined
Browse files Browse the repository at this point in the history
When ArrayBufferAllocator has an undefined zeroFill property,
Buffer.allocUnsafe() should zero fill.

Refs: 27e84ddd4e1#commitcomment-19182129

PR-URL: #11706
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
  • Loading branch information
Trott authored and fhinkel committed Mar 9, 2017
1 parent 0c10ec9 commit 9ee58c0
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions test/parallel/test-buffer-bindingobj-no-zerofill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

// Flags: --expose-internals

// Confirm that if a custom ArrayBufferAllocator does not define a zeroFill
// property, that the buffer module will zero-fill when allocUnsafe() is called.

require('../common');

const assert = require('assert');
const buffer = require('buffer');

// Monkey-patch setupBufferJS() to have an undefined zeroFill.
const process = require('process');
const originalBinding = process.binding;

const binding = originalBinding('buffer');
const originalSetup = binding.setupBufferJS;

binding.setupBufferJS = (proto, obj) => {
originalSetup(proto, obj);
assert.strictEqual(obj.zeroFill[0], 1);
delete obj.zeroFill;
};

const bindingObj = {};

binding.setupBufferJS(Buffer.prototype, bindingObj);
assert.strictEqual(bindingObj.zeroFill, undefined);

process.binding = (bindee) => {
if (bindee === 'buffer')
return binding;
return originalBinding(bindee);
};

// Load from file system because internal buffer is already loaded and we're
// testing code that runs on first load only.
// Do not move this require() to top of file. It is important that
// `process.binding('buffer').setupBufferJS` be monkey-patched before this runs.
const monkeyPatchedBuffer = require('../../lib/buffer');

// On unpatched buffer, allocUnsafe() should not zero fill memory. It's always
// possible that a segment of memory is already zeroed out, so try again and
// again until we succeed or we time out.
let uninitialized = buffer.Buffer.allocUnsafe(1024);
while (uninitialized.some((val) => val !== 0))

This comment has been minimized.

Copy link
@Qantas94Heavy

Qantas94Heavy Apr 9, 2017

Contributor

Isn't the while condition meant to end if the buffer contains a non-zero value? If so, I think it should look like this instead:

while (uninitialized.every((val) => val === 0))

This comment has been minimized.

Copy link
@addaleax

addaleax Apr 9, 2017

Member

@Qantas94Heavy yea, I think you’re right. Do you want to open a PR with this yourself? Thanks for catching this either way! :)

uninitialized = buffer.Buffer.allocUnsafe(1024);

// On monkeypatched buffer, zeroFill property is undefined. allocUnsafe() should
// zero-fill in that case.
const zeroFilled = monkeyPatchedBuffer.Buffer.allocUnsafe(1024);
assert(zeroFilled.every((val) => val === 0));

0 comments on commit 9ee58c0

Please sign in to comment.