Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions test/parallel/test-internal-util-construct-sab.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@

require('../common');
const assert = require('assert');
const { kMaxLength } = require('buffer');
const { isSharedArrayBuffer } = require('util/types');
const { constructSharedArrayBuffer } = require('internal/util');

// We're testing that we can construct a SAB even when the global is not exposed.
assert.strictEqual(typeof SharedArrayBuffer, 'undefined');

for (const length of [undefined, 0, 1, 2 ** 32]) {
for (const length of [undefined, 0, 1, 2 ** 16]) {
assert(isSharedArrayBuffer(constructSharedArrayBuffer(length)));
}

for (const length of [-1, Number.MAX_SAFE_INTEGER + 1, 2 ** 64]) {
// Specifically test the following cases:
// - out-of-range allocation requests should not crash the process
// - no int64 overflow
for (const length of [-1, kMaxLength + 1, 2 ** 64]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we want to keep Number.MAX_SAFE_INTEGER + 1 test case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intention behind Number.MAX_SAFE_INTEGER was that this is the maximum array buffer size on 64-bit builds, so that particular test value lives on as kMaxLength + 1.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it's still interesting to test with an "unsafe" integer

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's kinda covered by 2^64, but can add this if preferred

Copy link
Contributor

@aduh95 aduh95 Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it had to be one or the other, I would pick Number.MAX_SAFE_INTEGER + 1 over 2 ** 64, that seems more interesting / intent revelling to me. I don't feel strongly either way, so non-blocking comment

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The significance of 2^64 is that the size is passed via an int64_t intermediate, so has potential to overflow if not correctly clamped. It's a little academic, I can't imagine any of our internals will be allocating terabytes of shared memory 😀

assert.throws(() => constructSharedArrayBuffer(length), RangeError);
}
Loading