]> git.kaiwu.me - njs.git/commitdiff
Zlib: guard QuickJS stream cleanup after initialization
authorDmitry Volyntsev <xeioex@nginx.com>
Tue, 1 Dec 2026 00:13:16 +0000 (16:13 -0800)
committerDmitry Volyntsev <xeioexception@gmail.com>
Tue, 1 Dec 2026 01:29:33 +0000 (17:29 -0800)
The QuickJS wrappers introduced in bb3dcf21 use stream.opaque to
check whether the stream needs cleanup. It is set before *Init2(),
so if zlib fails to allocate its state, the wrapper calls *End()
with an uninitialized stream.state.

Only end streams that were successfully initialized and have not
already been passed to *End().

external/qjs_zlib_module.c

index 8af08c8941e229c897b0cca22b08987b98364e10..ca5260e143c1dd39c5d62416e52c32ec950cc66d 100644 (file)
@@ -76,6 +76,7 @@ qjs_zlib_ext_deflate(JSContext *ctx, JSValueConst this_val, int argc,
     JSValue      ret, options;
     z_stream     stream;
     njs_chb_t    chain;
+    njs_bool_t   initialized;
     qjs_bytes_t  bytes, dictionary;
 
     chunk_size = NJS_ZLIB_CHUNK_SIZE;
@@ -89,7 +90,7 @@ qjs_zlib_ext_deflate(JSContext *ctx, JSValueConst this_val, int argc,
     dictionary.length = 0;
     dictionary_copy = NULL;
     dictionary_length = 0;
-    stream.opaque = NULL;
+    initialized = 0;
 
     options = argv[1];
 
@@ -250,6 +251,8 @@ qjs_zlib_ext_deflate(JSContext *ctx, JSValueConst this_val, int argc,
         goto fail;
     }
 
+    initialized = 1;
+
     if (dictionary_copy != NULL) {
         rc = deflateSetDictionary(&stream, dictionary_copy, dictionary_length);
         if (rc != Z_OK) {
@@ -296,7 +299,7 @@ fail:
 
     js_free(ctx, dictionary_copy);
 
-    if (stream.opaque != NULL) {
+    if (initialized) {
         deflateEnd(&stream);
     }
 
@@ -319,6 +322,7 @@ qjs_zlib_ext_inflate(JSContext *ctx, JSValueConst this_val, int argc,
     JSValue      ret, options;
     z_stream     stream;
     njs_chb_t    chain;
+    njs_bool_t   initialized;
     qjs_bytes_t  bytes, dictionary;
 
     chunk_size = NJS_ZLIB_CHUNK_SIZE;
@@ -329,7 +333,7 @@ qjs_zlib_ext_inflate(JSContext *ctx, JSValueConst this_val, int argc,
     dictionary.length = 0;
     dictionary_copy = NULL;
     dictionary_length = 0;
-    stream.opaque = NULL;
+    initialized = 0;
 
     options = argv[1];
 
@@ -426,6 +430,8 @@ qjs_zlib_ext_inflate(JSContext *ctx, JSValueConst this_val, int argc,
         goto fail;
     }
 
+    initialized = 1;
+
     if (dictionary_copy != NULL) {
         rc = inflateSetDictionary(&stream, dictionary_copy, dictionary_length);
         if (rc != Z_OK) {
@@ -459,6 +465,8 @@ qjs_zlib_ext_inflate(JSContext *ctx, JSValueConst this_val, int argc,
     }
 
     rc = inflateEnd(&stream);
+    initialized = 0;
+
     if (njs_slow_path(rc != Z_OK)) {
         qjs_throw_error(ctx, "inflateEnd() failed");
         goto fail;
@@ -484,7 +492,7 @@ fail:
         qjs_bytes_free(ctx, &dictionary);
     }
 
-    if (stream.opaque != NULL) {
+    if (initialized) {
         inflateEnd(&stream);
     }