2016-08-23 14:16:45 -07:00
|
|
|
'use strict';
|
2025-05-07 18:28:20 +02:00
|
|
|
|
|
|
|
const common = require('../common');
|
2018-03-21 01:02:49 +05:30
|
|
|
|
2023-09-28 14:50:20 +02:00
|
|
|
// This test ensures that Node.js throws an Error when trying to convert a
|
|
|
|
// large buffer into a string.
|
2018-03-21 01:02:49 +05:30
|
|
|
// Regression test for https://github.com/nodejs/node/issues/649.
|
|
|
|
|
2025-05-07 18:28:20 +02:00
|
|
|
if (!common.enoughTestMem) {
|
|
|
|
common.skip('skipped due to memory requirements');
|
|
|
|
}
|
|
|
|
|
2016-08-23 14:16:45 -07:00
|
|
|
const assert = require('assert');
|
2023-09-28 14:50:20 +02:00
|
|
|
const {
|
2025-05-09 20:41:31 +01:00
|
|
|
Buffer,
|
2023-09-28 14:50:20 +02:00
|
|
|
constants: {
|
|
|
|
MAX_STRING_LENGTH,
|
|
|
|
},
|
|
|
|
} = require('buffer');
|
2016-08-23 14:16:45 -07:00
|
|
|
|
2023-09-28 14:50:20 +02:00
|
|
|
const len = MAX_STRING_LENGTH + 1;
|
2019-12-25 18:02:16 +01:00
|
|
|
const message = {
|
2023-09-28 14:50:20 +02:00
|
|
|
code: 'ERR_STRING_TOO_LONG',
|
|
|
|
name: 'Error',
|
2019-12-25 18:02:16 +01:00
|
|
|
};
|
2025-05-23 23:02:37 +02:00
|
|
|
|
|
|
|
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.alloc(len));
|
|
|
|
test(() => Buffer.allocUnsafe(len));
|
|
|
|
test(() => Buffer.allocUnsafeSlow(len));
|