• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3require('../common');
4const assert = require('assert');
5const SlowBuffer = require('buffer').SlowBuffer;
6
7// Test failed or zero-sized Buffer allocations not affecting typed arrays.
8// This test exists because of a regression that occurred. Because Buffer
9// instances are allocated with the same underlying allocator as TypedArrays,
10// but Buffer's can optional be non-zero filled, there was a regression that
11// occurred when a Buffer allocated failed, the internal flag specifying
12// whether or not to zero-fill was not being reset, causing TypedArrays to
13// allocate incorrectly.
14const zeroArray = new Uint32Array(10).fill(0);
15const sizes = [1e10, 0, 0.1, -1, 'a', undefined, null, NaN];
16const allocators = [
17  Buffer,
18  SlowBuffer,
19  Buffer.alloc,
20  Buffer.allocUnsafe,
21  Buffer.allocUnsafeSlow,
22];
23for (const allocator of allocators) {
24  for (const size of sizes) {
25    try {
26      // Some of these allocations are known to fail. If they do,
27      // Uint32Array should still produce a zeroed out result.
28      allocator(size);
29    } catch {
30      assert.deepStrictEqual(zeroArray, new Uint32Array(10));
31    }
32  }
33}
34