]> git.kaiwu.me - njs.git/commitdiff
Zlib: fixed inflate().
authorDmitry Volyntsev <xeioex@nginx.com>
Tue, 23 Apr 2024 00:52:06 +0000 (17:52 -0700)
committerDmitry Volyntsev <xeioex@nginx.com>
Tue, 23 Apr 2024 00:52:06 +0000 (17:52 -0700)
Previously, the function might fail to return the last part of the
compressed content. This problem is more visible when output size > 1024
or when chunkSize < the content size.

external/njs_zlib_module.c
src/test/njs_unit_test.c

index 3f2e23feb3808ede8d8cee1c3e4f475c826a245b..9e35c2e83b39852134f1d27b66d2616d511481e9 100644 (file)
@@ -463,7 +463,7 @@ njs_zlib_ext_inflate(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
 
     njs_chb_init(&chain, njs_vm_memory_pool(vm));
 
-    while (stream.avail_in > 0) {
+    while (rc != Z_STREAM_END) {
         stream.next_out = njs_chb_reserve(&chain, chunk_size);
         if (njs_slow_path(stream.next_out == NULL)) {
             njs_vm_memory_error(vm);
index 1740a4caf7980a69dd53839bea453cf27ce8fd14..ff34c940017430e7dbe32fc25b360bae5e27ccff 100644 (file)
@@ -22351,6 +22351,13 @@ static njs_unit_test_t  njs_zlib_test[] =
               ".map(v => zlib.deflateSync(v).toString('base64'))"),
       njs_str("eJwLd/R2BAAC+gEl,eJw7t/HcpnObAQ/sBIE=") },
 
+    { njs_str("const zlib = require('zlib');"
+              "const buf = 'αβγ'.repeat(56);"
+              "const enc = zlib.deflateRawSync(buf, {chunkSize:64}).toString('base64');"
+              "const dec = zlib.inflateRawSync(Buffer.from(enc, 'base64')).toString();"
+              "buf == dec"),
+      njs_str("true") },
+
     { njs_str("const zlib = require('zlib');"
               "['WAKA'.repeat(1024), 'αβγ'.repeat(1024)]"
               ".map(v => [v, zlib.deflateRawSync(v).toString('base64')])"