Commit 9cf1a9c6c21 for nodejs

commit 9cf1a9c6c2182466554284ac47ff633377ba47c6
Author: Paolo Insogna <paolo@cowtech.it>
Date:   Thu Dec 3 13:29:32 2026 +0100

    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 <openai/gpt-5.6-sol>
    Signed-off-by: Paolo Insogna <paolo@cowtech.it>
    PR-URL: https://github.com/nodejs/node/pull/67616
    Reviewed-By: James M Snell <jasnell@gmail.com>
    Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>

diff --git a/src/node_zlib.cc b/src/node_zlib.cc
index b4dd23e5ae0..fd97a1ddad0 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<Value> &args) {
     CompressionStream* wrap;
     ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This());
@@ -798,6 +807,11 @@ class ZlibStream final : public CompressionStream<ZlibContext> {

   // just pull the ints out of the args and call the other Init
   static void Init(const FunctionCallbackInfo<Value>& 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<ZlibContext> {
           "writeCallback,"
           " dictionary[, rejectGarbageAfterEnd])");

-    ZlibStream* wrap;
-    ASSIGN_OR_RETURN_UNWRAP(&wrap, args.This());
-
     Local<Context> 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<Value>& 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<CompressionContext> {
   }

   static void Init(const FunctionCallbackInfo<Value>& 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<Uint32Array>()->Length(), 2);
     uint32_t* write_result = reinterpret_cast<uint32_t*>(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 00000000000..7556ed9bc02
--- /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',
+  });
+}