From: Dmitry Volyntsev Date: Tue, 1 Dec 2026 00:13:16 +0000 (-0800) Subject: Zlib: guard QuickJS stream cleanup after initialization X-Git-Url: https://git.kaiwu.me/web/%7B@url%7D?a=commitdiff_plain;h=196637f3c88f543554803fb673c61b1b45e4bfbf;p=njs.git Zlib: guard QuickJS stream cleanup after initialization 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(). --- diff --git a/external/qjs_zlib_module.c b/external/qjs_zlib_module.c index 8af08c89..ca5260e1 100644 --- a/external/qjs_zlib_module.c +++ b/external/qjs_zlib_module.c @@ -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); }