Skip to content

Commit 5ef134f

Browse files
committed
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)`.
1 parent d5279e5 commit 5ef134f

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/internal/streams/readable.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1669,7 +1669,7 @@ function fromList(n, state) {
16691669
n -= str.length;
16701670
buf[idx++] = null;
16711671
} else {
1672-
if (n === buf.length) {
1672+
if (n === str.length) {
16731673
ret += str;
16741674
buf[idx++] = null;
16751675
} else {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const { Readable } = require('stream');
5+
6+
const readable = new Readable({ read() {} });
7+
readable.setEncoding('utf8');
8+
9+
readable.push('abc');
10+
readable.push('defgh');
11+
readable.push(null);
12+
13+
assert.strictEqual(readable.read(5), 'abcde');
14+
assert.strictEqual(readable.read(3), 'fgh');
15+
assert.strictEqual(readable.read(1), null);

0 commit comments

Comments
 (0)