Skip to content

test: skip the test if the buffer allocation fails #58738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 20, 2025
Merged
Show file tree
Hide file tree
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
18 changes: 14 additions & 4 deletions test/pummel/test-buffer-large-size-buffer-alloc-unsafe-slow.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@ common.skipIf32Bits();
const assert = require('node:assert');
const size = 2 ** 31;

let largeBuffer;

// Test Buffer.allocUnsafe with size larger than integer range
try {
assert.throws(() => Buffer.allocUnsafeSlow(size).toString('utf8'), { code: 'ERR_STRING_TOO_LONG' });
largeBuffer = Buffer.allocUnsafeSlow(size);
} catch (e) {
if (e.code !== 'ERR_MEMORY_ALLOCATION_FAILED') {
throw e;
if (
e.code === 'ERR_MEMORY_ALLOCATION_FAILED' ||
/Array buffer allocation failed/.test(e.message)
) {
common.skip('insufficient space for Buffer.allocUnsafeSlow');
}
common.skip('insufficient space for Buffer.allocUnsafeSlow');

throw e;
}

assert.throws(() => largeBuffer.toString('utf8'), {
code: 'ERR_STRING_TOO_LONG',
});
21 changes: 14 additions & 7 deletions test/pummel/test-buffer-large-size-buffer-write.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@ const kStringMaxLength = require('buffer').constants.MAX_STRING_LENGTH;

const size = 2 ** 31;

// Test Buffer.write with size larger than integer range
let largeBuffer;

try {
const buf = Buffer.alloc(size);
assert.strictEqual(buf.write('a', 2, kStringMaxLength), 1);
assert.strictEqual(buf.write('a', 2, size), 1);
largeBuffer = Buffer.alloc(size);
} catch (e) {
if (e.code !== 'ERR_MEMORY_ALLOCATION_FAILED') {
throw e;
if (
e.code === 'ERR_MEMORY_ALLOCATION_FAILED' ||
/Array buffer allocation failed/.test(e.message)
) {
common.skip('insufficient space for Buffer.alloc');
}
common.skip('insufficient space for Buffer.alloc');

throw e;
}

// Test Buffer.write with size larger than integer range
assert.strictEqual(largeBuffer.write('a', 2, kStringMaxLength), 1);
assert.strictEqual(largeBuffer.write('a', 2, size), 1);
20 changes: 14 additions & 6 deletions test/pummel/test-buffer-large-size-slowbuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ const { Buffer } = require('node:buffer');

const size = 2 ** 31;

let largeBuffer;

// Test slow Buffer with size larger than integer range
try {
assert.throws(() => Buffer.allocUnsafeSlow(size).toString('utf8'), {
code: 'ERR_STRING_TOO_LONG',
});
largeBuffer = Buffer.allocUnsafeSlow(size);
} catch (e) {
if (e.code !== 'ERR_MEMORY_ALLOCATION_FAILED') {
throw e;
if (
e.code === 'ERR_MEMORY_ALLOCATION_FAILED' ||
/Array buffer allocation failed/.test(e.message)
) {
common.skip('insufficient space for slow Buffer allocation');
}
common.skip('insufficient space for slow Buffer allocation');

throw e;
}

assert.throws(() => largeBuffer.toString('utf8'), {
code: 'ERR_STRING_TOO_LONG',
});
19 changes: 13 additions & 6 deletions test/pummel/test-string-decoder-large-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@ const stringTooLongError = {
name: 'Error',
};

let largeBuffer;

try {
const buf = Buffer.allocUnsafe(size);
const decoder = new StringDecoder('utf8');
assert.throws(() => decoder.write(buf), stringTooLongError);
largeBuffer = Buffer.allocUnsafe(size);
} catch (e) {
if (e.code !== 'ERR_MEMORY_ALLOCATION_FAILED') {
throw e;
if (
e.code === 'ERR_MEMORY_ALLOCATION_FAILED' ||
/Array buffer allocation failed/.test(e.message)
) {
common.skip('insufficient space for Buffer.allocUnsafe');
}
common.skip('insufficient space for Buffer.alloc');

throw e;
}

const decoder = new StringDecoder('utf8');
assert.throws(() => decoder.write(largeBuffer), stringTooLongError);
Loading