stream: defend against re-entrancy in writev · nodejs/node@bf5acb7 · GitHub
Skip to content

Commit bf5acb7

Browse files
jasnelladuh95
authored andcommitted
stream: defend against re-entrancy in writev
Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode PR-URL: #65652 Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent b01594f commit bf5acb7

4 files changed

Lines changed: 85 additions & 24 deletions

File tree

‎lib/internal/streams/iter/broadcast.js‎

Lines changed: 18 additions & 3 deletions

‎lib/internal/streams/iter/push.js‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -657,12 +657,10 @@ class PushWriter {
657657
writev(chunks, options) {
658658
validateArray(chunks, 'chunks');
659659
const signal = getWriterSignal(options);
660-
if (!signal && this.#queue.canWriteSync()) {
661-
const bytes = convertChunks(chunks);
662-
this.#queue.writeSync(bytes);
660+
const bytes = convertChunks(chunks);
661+
if (!signal && this.#queue.writeSync(bytes)) {
663662
return kResolvedPromise;
664663
}
665-
const bytes = convertChunks(chunks);
666664
return this.#queue.writeAsync(bytes, signal);
667665
}
668666

‎lib/internal/streams/iter/utils.js‎

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -185,19 +185,6 @@ function toUint8Array(chunk) {
185185
return chunk;
186186
}
187187

188-
/**
189-
* Check if all chunks in an array are already Uint8Array.
190-
* Short-circuits on the first non-Uint8Array chunk found.
191-
* @param {Array<Uint8Array|string>} chunks
192-
* @returns {boolean}
193-
*/
194-
function allUint8Array(chunks) {
195-
for (let i = 0; i < chunks.length; i++) {
196-
if (!isUint8Array(chunks[i])) return false;
197-
}
198-
return true;
199-
}
200-
201188
function snapshotByteView(value) {
202189
const buffer = TypedArrayPrototypeGetBuffer(value);
203190
const sharedBufferView = isSharedArrayBuffer(buffer) ?
@@ -317,9 +304,6 @@ function concatBytes(chunks) {
317304
* @returns {Uint8Array[]}
318305
*/
319306
function convertChunks(chunks) {
320-
if (allUint8Array(chunks)) {
321-
return ArrayPrototypeSlice(chunks);
322-
}
323307
const len = chunks.length;
324308
const result = new Array(len);
325309
for (let i = 0; i < len; i++) {
@@ -446,7 +430,6 @@ module.exports = {
446430
kMultiConsumerDefaultBudget,
447431
kPushDefaultBudget,
448432
kResolvedPromise,
449-
allUint8Array,
450433
concatBytes,
451434
convertChunks,
452435
createBatchEntry,
Lines changed: 65 additions & 0 deletions

0 commit comments

Comments
 (0)