test: skip in test-buffer-tostring-rangeerror on allocation failure

If the buffer allocation fails due to insufficient memory, there is no
point continue testing toString().

PR-URL: https://github.com/nodejs/node/pull/58415
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Joyee Cheung 2025-05-23 23:02:37 +02:00 committed by GitHub
parent 8287f6748b
commit 996a774eba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -23,8 +23,22 @@ const message = {
code: 'ERR_STRING_TOO_LONG',
name: 'Error',
};
assert.throws(() => Buffer(len).toString('utf8'), message);
assert.throws(() => Buffer.allocUnsafeSlow(len).toString('utf8'), message);
assert.throws(() => Buffer.alloc(len).toString('utf8'), message);
assert.throws(() => Buffer.allocUnsafe(len).toString('utf8'), message);
assert.throws(() => Buffer.allocUnsafeSlow(len).toString('utf8'), message);
function test(getBuffer) {
let buf;
try {
buf = getBuffer();
} catch (e) {
// If the buffer allocation fails, we skip the test.
if (e.code === 'ERR_MEMORY_ALLOCATION_FAILED' || /Array buffer allocation failed/.test(e.message)) {
return;
}
}
assert.throws(() => { buf.toString('utf8'); }, message);
}
test(() => Buffer(len));
test(() => Buffer.allocUnsafeSlow(len));
test(() => Buffer.alloc(len));
test(() => Buffer.allocUnsafe(len));
test(() => Buffer.allocUnsafeSlow(len));