Skip to content

Commit 1af0fa4

Browse files
Trottitaloacasas
authored andcommitted
test: test buffer behavior when zeroFill undefined
When ArrayBufferAllocator has an undefined zeroFill property, Buffer.allocUnsafe() should zero fill. Refs: 27e84ddd4e1#commitcomment-19182129 PR-URL: #11706 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]>
1 parent 1e52ba3 commit 1af0fa4

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
// Flags: --expose-internals
4+
5+
// Confirm that if a custom ArrayBufferAllocator does not define a zeroFill
6+
// property, that the buffer module will zero-fill when allocUnsafe() is called.
7+
8+
require('../common');
9+
10+
const assert = require('assert');
11+
const buffer = require('buffer');
12+
13+
// Monkey-patch setupBufferJS() to have an undefined zeroFill.
14+
const process = require('process');
15+
const originalBinding = process.binding;
16+
17+
const binding = originalBinding('buffer');
18+
const originalSetup = binding.setupBufferJS;
19+
20+
binding.setupBufferJS = (proto, obj) => {
21+
originalSetup(proto, obj);
22+
assert.strictEqual(obj.zeroFill[0], 1);
23+
delete obj.zeroFill;
24+
};
25+
26+
const bindingObj = {};
27+
28+
binding.setupBufferJS(Buffer.prototype, bindingObj);
29+
assert.strictEqual(bindingObj.zeroFill, undefined);
30+
31+
process.binding = (bindee) => {
32+
if (bindee === 'buffer')
33+
return binding;
34+
return originalBinding(bindee);
35+
};
36+
37+
// Load from file system because internal buffer is already loaded and we're
38+
// testing code that runs on first load only.
39+
// Do not move this require() to top of file. It is important that
40+
// `process.binding('buffer').setupBufferJS` be monkey-patched before this runs.
41+
const monkeyPatchedBuffer = require('../../lib/buffer');
42+
43+
// On unpatched buffer, allocUnsafe() should not zero fill memory. It's always
44+
// possible that a segment of memory is already zeroed out, so try again and
45+
// again until we succeed or we time out.
46+
let uninitialized = buffer.Buffer.allocUnsafe(1024);
47+
while (uninitialized.some((val) => val !== 0))
48+
uninitialized = buffer.Buffer.allocUnsafe(1024);
49+
50+
// On monkeypatched buffer, zeroFill property is undefined. allocUnsafe() should
51+
// zero-fill in that case.
52+
const zeroFilled = monkeyPatchedBuffer.Buffer.allocUnsafe(1024);
53+
assert(zeroFilled.every((val) => val === 0));

0 commit comments

Comments
 (0)