From 5ef134fce5fb899ff4510e9da41e5d5f2af4b09c Mon Sep 17 00:00:00 2001 From: Thomas Watson Date: Thu, 19 Feb 2026 11:25:19 +0100 Subject: [PATCH 1/2] 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)`. --- lib/internal/streams/readable.js | 2 +- ...test-stream2-read-correct-num-bytes-in-utf8.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-stream2-read-correct-num-bytes-in-utf8.js diff --git a/lib/internal/streams/readable.js b/lib/internal/streams/readable.js index b6145bf2412db1..919c527a2be6f8 100644 --- a/lib/internal/streams/readable.js +++ b/lib/internal/streams/readable.js @@ -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 { diff --git a/test/parallel/test-stream2-read-correct-num-bytes-in-utf8.js b/test/parallel/test-stream2-read-correct-num-bytes-in-utf8.js new file mode 100644 index 00000000000000..b039c21ec7c43b --- /dev/null +++ b/test/parallel/test-stream2-read-correct-num-bytes-in-utf8.js @@ -0,0 +1,15 @@ +'use strict'; + +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); From 2984f13162380e6f54d0b91a810f7ff359d44ff0 Mon Sep 17 00:00:00 2001 From: Thomas Watson Date: Thu, 19 Feb 2026 11:43:24 +0100 Subject: [PATCH 2/2] fix lint --- test/parallel/test-stream2-read-correct-num-bytes-in-utf8.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/parallel/test-stream2-read-correct-num-bytes-in-utf8.js b/test/parallel/test-stream2-read-correct-num-bytes-in-utf8.js index b039c21ec7c43b..476f041e3f4087 100644 --- a/test/parallel/test-stream2-read-correct-num-bytes-in-utf8.js +++ b/test/parallel/test-stream2-read-correct-num-bytes-in-utf8.js @@ -1,5 +1,6 @@ 'use strict'; +require('../common'); const assert = require('assert'); const { Readable } = require('stream');