Skip to content

Commit e146591

Browse files
watsonaduh95
authored andcommitted
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: #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>
1 parent 5a9874a commit e146591

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

‎lib/internal/streams/readable.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1664,7 +1664,7 @@ function fromList(n, state) {
16641664
n-=str.length;
16651665
buf[idx++]=null;
16661666
}else{
1667-
if(n===buf.length){
1667+
if(n===str.length){
16681668
ret+=str;
16691669
buf[idx++]=null;
16701670
}else{
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
require('../common');
4+
constassert=require('assert');
5+
const{ Readable }=require('stream');
6+
7+
constreadable=newReadable({read(){}});
8+
readable.setEncoding('utf8');
9+
10+
readable.push('abc');
11+
readable.push('defgh');
12+
readable.push(null);
13+
14+
assert.strictEqual(readable.read(5),'abcde');
15+
assert.strictEqual(readable.read(3),'fgh');
16+
assert.strictEqual(readable.read(1),null);

0 commit comments

Comments
 (0)