mirror of
https://github.com/nodejs/node.git
synced 2025-05-07 12:03:30 +00:00

test-buffer-failed-alloc-typed-arrays.js is working fine, but the description was not correct. PR-URL: https://github.com/nodejs/node/pull/28351 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
34 lines
1.1 KiB
JavaScript
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 = [1e10, 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));
|
|
}
|
|
}
|
|
}
|