From f06f11d9ca36ff5791790e201cccb41e813c6e1f Mon Sep 17 00:00:00 2001 From: Christian Aurich Date: Thu, 3 Sep 2026 02:14:41 -0300 Subject: [PATCH] test: zero-fill buffers before the string length limit check 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 --- test/parallel/test-buffer-tostring-rangeerror.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/parallel/test-buffer-tostring-rangeerror.js b/test/parallel/test-buffer-tostring-rangeerror.js index b34f4c612618..856ed84d21cd 100644 --- a/test/parallel/test-buffer-tostring-rangeerror.js +++ b/test/parallel/test-buffer-tostring-rangeerror.js @@ -34,6 +34,12 @@ function test(getBuffer) { 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); }