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 <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>
This commit is contained in:
Christian Aurich authored and GitHub committed 2026-09-05 05:44:06 +00:00
1 parent 57860ef5d4
commit ad67159bea
1 file changed
+6
@@ -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);
}