stream: fix decoded fromList chunk boundary check

Correct `fromList()` in decoded string mode to compare `n` against the
current chunk length, not the buffer array length.

This prevents over-consuming chunks, which can corrupt readable state
and crash with `TypeError` when mixing `setEncoding()` and `read(n)`.

PR-URL: https://github.com/nodejs/node/pull/61884
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Ilyas Shabi <ilyasshabi94@gmail.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
This commit is contained in:
Thomas Watson authored and GitHub committed 2026-02-22 00:19:40 +00:00
1 parent ca92ea03da
commit d10104bd94
2 files changed
+17 -1

No files matched your search

+1 -1
View File
@@ -1669,7 +1669,7 @@ function fromList(n, state) {
n -= str.length;
buf[idx++] = null;
} else {
if (n === buf.length) {
if (n === str.length) {
ret += str;
buf[idx++] = null;
} else {
@@ -0,0 +1,16 @@
'use strict';
require('../common');
const assert = require('assert');
const { Readable } = require('stream');
const readable = new Readable({ read() {} });
readable.setEncoding('utf8');
readable.push('abc');
readable.push('defgh');
readable.push(null);
assert.strictEqual(readable.read(5), 'abcde');
assert.strictEqual(readable.read(3), 'fgh');
assert.strictEqual(readable.read(1), null);