nodejs/test/parallel/test-buffer-failed-alloc-typed-arrays.js
James M Snell 647175ee0b buffer: move SlowBuffer to EOL
`SlowBuffer` has been deprecated for many years now. Let's remove it.

PR-URL: https://github.com/nodejs/node/pull/58008
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2025-04-27 07:40:37 -07:00

33 lines
1.1 KiB
JavaScript

'use strict';
require('../common');
const assert = require('assert');
const { Buffer } = require('buffer');
// Test failed or zero-sized Buffer allocations not affecting typed arrays.
// This test exists because of a regression that occurred. Because Buffer
// instances are allocated with the same underlying allocator as TypedArrays,
// but Buffer's can optional be non-zero filled, there was a regression that
// occurred when a Buffer allocated failed, the internal flag specifying
// whether or not to zero-fill was not being reset, causing TypedArrays to
// allocate incorrectly.
const zeroArray = new Uint32Array(10).fill(0);
const sizes = [1e20, 0, 0.1, -1, 'a', undefined, null, NaN];
const allocators = [
Buffer,
Buffer.alloc,
Buffer.allocUnsafe,
Buffer.allocUnsafeSlow,
];
for (const allocator of allocators) {
for (const size of sizes) {
try {
// Some of these allocations are known to fail. If they do,
// Uint32Array should still produce a zeroed out result.
allocator(size);
} catch {
assert.deepStrictEqual(zeroArray, new Uint32Array(10));
}
}
}