buffer: pad aligned allocations by a multiple of 8

Signed-off-by: Lazizbek Ergashev <lazerg2@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/65605
Fixes: https://github.com/nodejs/node/issues/65601
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
This commit is contained in:
Lazizbek Ergashev authored and GitHub committed 2026-09-03 05:35:54 +00:00
1 parent b53ddc0daa
commit 0a1a37ecbb
5 files changed
+46 -16

No files matched your search

@@ -111,6 +111,24 @@ for (const alloc of [Buffer.allocUnsafe, Buffer.allocUnsafeSlow]) {
assert.strictEqual(buf.buffer.byteLength, 100);
}
// The padding an aligned allocation needs does not leave a partial element
// behind it, so a typed array view running from the buffer to the end of its
// ArrayBuffer can be created without an explicit length.
{
const bufs = [Buffer.allocUnsafe(8), Buffer.allocUnsafeSlow(4096, 4096)];
for (const alignment of alignments) {
bufs.push(Buffer.allocUnsafe(8, alignment),
Buffer.allocUnsafeSlow(8, alignment));
}
for (const buf of bufs) {
const rest = buf.buffer.byteLength - buf.byteOffset;
assert.strictEqual(new BigUint64Array(buf.buffer, buf.byteOffset).byteLength,
rest);
assert.strictEqual(new Uint32Array(buf.buffer, buf.byteOffset).byteLength,
rest);
}
}
// Buffer.allocUnsafeSlow() is never pooled, even when aligned.
{
const a = Buffer.allocUnsafeSlow(64, 64);