[nginx] Gzip: compatibility with recent zlib-ng versions.

Sergey Kandaurov pluknet at nginx.com
Mon Mar 27 19:35:13 UTC 2023


details:   https://hg.nginx.org/nginx/rev/fcb2333c9982
branches:  
changeset: 8153:fcb2333c9982
user:      Maxim Dounin <mdounin at mdounin.ru>
date:      Mon Mar 27 21:25:05 2023 +0300
description:
Gzip: compatibility with recent zlib-ng versions.

It now uses custom alloc_aligned() wrapper for all allocations,
therefore all allocations are larger than expected by (64 + sizeof(void*)).
Further, they are seen as allocations of 1 element.  Relevant calculations
were adjusted to reflect this, and state allocation is now protected
with a flag to avoid misinterpreting other allocations as the zlib
deflate_state allocation.

Further, it no longer forces window bits to 13 on compression level 1,
so the comment was adjusted to reflect this.

diffstat:

 src/http/modules/ngx_http_gzip_filter_module.c |  18 ++++++++++++------
 1 files changed, 12 insertions(+), 6 deletions(-)

diffs (54 lines):

diff -r d1cf09451ae8 -r fcb2333c9982 src/http/modules/ngx_http_gzip_filter_module.c
--- a/src/http/modules/ngx_http_gzip_filter_module.c	Fri Mar 24 02:57:43 2023 +0300
+++ b/src/http/modules/ngx_http_gzip_filter_module.c	Mon Mar 27 21:25:05 2023 +0300
@@ -57,6 +57,7 @@ typedef struct {
     unsigned             nomem:1;
     unsigned             buffering:1;
     unsigned             zlib_ng:1;
+    unsigned             state_allocated:1;
 
     size_t               zin;
     size_t               zout;
@@ -514,9 +515,10 @@ ngx_http_gzip_filter_memory(ngx_http_req
     } else {
         /*
          * Another zlib variant, https://github.com/zlib-ng/zlib-ng.
-         * It forces window bits to 13 for fast compression level,
-         * uses 16-byte padding in one of window-sized buffers, and
-         * uses 128K hash.
+         * It used to force window bits to 13 for fast compression level,
+         * uses (64 + sizeof(void*)) additional space on all allocations
+         * for alignment, 16-byte padding in one of window-sized buffers,
+         * and 128K hash.
          */
 
         if (conf->level == 1) {
@@ -524,7 +526,8 @@ ngx_http_gzip_filter_memory(ngx_http_req
         }
 
         ctx->allocated = 8192 + 16 + (1 << (wbits + 2))
-                         + 131072 + (1 << (memlevel + 8));
+                         + 131072 + (1 << (memlevel + 8))
+                         + 4 * (64 + sizeof(void*));
         ctx->zlib_ng = 1;
     }
 }
@@ -926,13 +929,16 @@ ngx_http_gzip_filter_alloc(void *opaque,
 
     alloc = items * size;
 
-    if (items == 1 && alloc % 512 != 0 && alloc < 8192) {
-
+    if (items == 1 && alloc % 512 != 0 && alloc < 8192
+        && !ctx->state_allocated)
+    {
         /*
          * The zlib deflate_state allocation, it takes about 6K,
          * we allocate 8K.  Other allocations are divisible by 512.
          */
 
+        ctx->state_allocated = 1;
+
         alloc = 8192;
     }
 


More information about the nginx-devel mailing list