mirror of
https://github.com/nodejs/node.git
synced 2026-10-28 18:25:13 +08:00
The test allocates a buffer one byte longer than MAX_STRING_LENGTH and
expects toString('utf8') to throw ERR_STRING_TOO_LONG. MAX_STRING_LENGTH
counts UTF-16 units rather than bytes, so that only holds if every byte
decodes to a single unit. For the two Buffer.allocUnsafe variants the
contents are whatever the allocator hands back, and a single multi-byte
UTF-8 sequence anywhere in the buffer decodes to fewer units than it
occupies bytes, bringing the result down to MAX_STRING_LENGTH or less
so that nothing is thrown.
This goes unnoticed when the allocator returns zeroed pages, which is
why the test passes elsewhere. The AIX builders expose non-zero
contents and fail with "Missing expected exception (Error)".
Refs: https://github.com/nodejs/reliability/issues/1649
Signed-off-by: Christian Aurich <christian.aurichzm@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/65755
Refs: https://github.com/nodejs/reliability/issues/1648
Refs: https://github.com/nodejs/reliability/issues/1649
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
50 lines
1.3 KiB
JavaScript
50 lines
1.3 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
|
|
// This test ensures that Node.js throws an Error when trying to convert a
|
|
// large buffer into a string.
|
|
// Regression test for https://github.com/nodejs/node/issues/649.
|
|
|
|
if (!common.enoughTestMem) {
|
|
common.skip('skipped due to memory requirements');
|
|
}
|
|
|
|
const assert = require('assert');
|
|
const {
|
|
Buffer,
|
|
constants: {
|
|
MAX_STRING_LENGTH,
|
|
},
|
|
} = require('buffer');
|
|
|
|
const len = MAX_STRING_LENGTH + 1;
|
|
const message = {
|
|
code: 'ERR_STRING_TOO_LONG',
|
|
name: 'Error',
|
|
};
|
|
|
|
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;
|
|
}
|
|
}
|
|
// MAX_STRING_LENGTH counts UTF-16 units, not bytes. Uninitialized memory can
|
|
// hold multi-byte UTF-8 sequences, which decode to fewer units than they
|
|
// occupy bytes, and a single one of them is enough to bring the result down
|
|
// to MAX_STRING_LENGTH or less. ASCII bytes keep the decoded length equal to
|
|
// the buffer length.
|
|
buf.fill(0);
|
|
assert.throws(() => { buf.toString('utf8'); }, message);
|
|
}
|
|
|
|
test(() => Buffer(len));
|
|
test(() => Buffer.alloc(len));
|
|
test(() => Buffer.allocUnsafe(len));
|
|
test(() => Buffer.allocUnsafeSlow(len));
|