test: skip the test if the buffer allocation fails

Use the error message as another condition to skip the test when the
buffer allocation fails.

Refs: https://github.com/nodejs/node/commit/795dd8eb7988ae38553e
Refs: https://github.com/nodejs/node/commit/e9c6004a2d580008082b
PR-URL: https://github.com/nodejs/node/pull/58738
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
This commit is contained in:
Luigi Pinca 2025-06-20 08:12:31 +02:00 committed by GitHub
parent 433d9a0486
commit 3d608bbe8b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 55 additions and 23 deletions

View File

@ -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',
});

View File

@ -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);

View File

@ -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',
});

View File

@ -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);