From e9280ccaeb1b3d4f42a975ba70e8873dcc8a0d77 Mon Sep 17 00:00:00 2001 From: Paolo Insogna Date: Thu, 19 Nov 2026 13:58:17 +0100 Subject: [PATCH] zlib: throw on init() while a write is in progress init() on a compression stream handle could reinitialize the context while a write was still running on the thread pool. Throw instead, like reset() already does. Assisted-By: OpenAI GPT-5.6 Sol Signed-off-by: Paolo Insogna --- src/node_zlib.cc | 28 +++++++++++---- test/parallel/test-zlib-init-during-write.js | 36 ++++++++++++++++++++ 2 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 test/parallel/test-zlib-init-during-write.js diff --git a/src/node_zlib.cc b/src/node_zlib.cc index 72947b8b7839..c8b0e5b71cdc 100644 --- a/src/node_zlib.cc +++ b/src/node_zlib.cc @@ -699,6 +699,15 @@ class CompressionStream : public AsyncWrap, Close(); } + // Used by the init() bindings: the context (and its buffers) must not be + // (re)initialized while the thread pool is still using it. + bool ThrowIfWriteInProgress() { + if (!write_in_progress_) return false; + env()->ThrowError( + "Cannot initialize zlib stream while a write is in progress"); + return true; + } + static void Reset(const FunctionCallbackInfo &args) { CompressionStream* wrap; ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This()); @@ -798,6 +807,11 @@ class ZlibStream final : public CompressionStream { // just pull the ints out of the args and call the other Init static void Init(const FunctionCallbackInfo& args) { + ZlibStream* wrap; + ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This()); + + if (wrap->ThrowIfWriteInProgress()) return; + // Refs: https://github.com/nodejs/node/issues/16649 // Refs: https://github.com/nodejs/node/issues/14161 if (args.Length() == 5) { @@ -813,9 +827,6 @@ class ZlibStream final : public CompressionStream { "writeCallback," " dictionary[, rejectGarbageAfterEnd])"); - ZlibStream* wrap; - ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This()); - Local context = args.GetIsolate()->GetCurrentContext(); // windowBits is special. On the compression side, 0 is an invalid value. @@ -914,6 +925,9 @@ class BrotliCompressionStream final : static void Init(const FunctionCallbackInfo& args) { BrotliCompressionStream* wrap; ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This()); + + if (wrap->ThrowIfWriteInProgress()) return; + CHECK((args.Length() == 3 || args.Length() == 4) && "init(params, writeResult, writeCallback[, dictionary])"); @@ -996,13 +1010,15 @@ class ZstdStream final : public CompressionStream { } static void Init(const FunctionCallbackInfo& args) { + ZstdStream* wrap; + ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This()); + + if (wrap->ThrowIfWriteInProgress()) return; + CHECK((args.Length() >= 4 && args.Length() <= 6) && "init(params, pledgedSrcSize, writeResult, writeCallback[, " "dictionary[, rejectGarbageAfterEnd]])"); - ZstdStream* wrap; - ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This()); - CHECK(args[2]->IsUint32Array()); CHECK_GE(args[2].As()->Length(), 2); uint32_t* write_result = reinterpret_cast(Buffer::Data(args[2])); diff --git a/test/parallel/test-zlib-init-during-write.js b/test/parallel/test-zlib-init-during-write.js new file mode 100644 index 000000000000..7556ed9bc02c --- /dev/null +++ b/test/parallel/test-zlib-init-during-write.js @@ -0,0 +1,36 @@ +'use strict'; +require('../common'); +const assert = require('assert'); +const { + createBrotliCompress, + createBrotliDecompress, + createDeflate, + createZstdCompress, + createZstdDecompress, +} = require('zlib'); + +// Tests that calling init() on the internal handle while an async write is in +// progress throws an error instead of releasing state still used by the +// thread pool. + +const factories = [createBrotliCompress, createBrotliDecompress, createDeflate]; +if (typeof createZstdCompress === 'function') { + factories.push(createZstdCompress, createZstdDecompress); +} + +for (const factory of factories) { + const stream = factory(); + const input = Buffer.alloc(1024, 0x41); + + stream.write(input, () => {}); + // The decompressors are fed invalid data and are expected to error later. + stream.on('error', () => {}); + stream.on('data', () => {}); + + // The write has been dispatched to the thread pool. + assert.throws(() => { + stream._handle.init(); + }, { + message: 'Cannot initialize zlib stream while a write is in progress', + }); +}