nodejs/test/parallel/test-buffer-failed-alloc-typed-arrays.js
Filip Skokan da69d13623
Revert "buffer: move SlowBuffer to EOL"
This reverts commit 647175ee0b8ca19d6f315216c879b1dc89ad2759.

PR-URL: https://github.com/nodejs/node/pull/58211
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
2025-05-07 19:31:21 +00:00

34 lines
1.1 KiB
JavaScript

'use strict';
require('../common');
const assert = require('assert');
const SlowBuffer = require('buffer').SlowBuffer;
// 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,
SlowBuffer,
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));
}
}
}