Skip to content

Commit 8747191

Browse files
committed
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.
1 parent 7b24b93 commit 8747191

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/http/modules/ngx_http_gzip_filter_module.c

+12-6
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ typedef struct {
5757
unsigned nomem:1;
5858
unsigned buffering:1;
5959
unsigned zlib_ng:1;
60+
unsigned state_allocated:1;
6061

6162
size_t zin;
6263
size_t zout;
@@ -514,17 +515,19 @@ ngx_http_gzip_filter_memory(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)
514515
} else {
515516
/*
516517
* Another zlib variant, https://github.com/zlib-ng/zlib-ng.
517-
* It forces window bits to 13 for fast compression level,
518-
* uses 16-byte padding in one of window-sized buffers, and
519-
* uses 128K hash.
518+
* It used to force window bits to 13 for fast compression level,
519+
* uses (64 + sizeof(void*)) additional space on all allocations
520+
* for alignment, 16-byte padding in one of window-sized buffers,
521+
* and 128K hash.
520522
*/
521523

522524
if (conf->level == 1) {
523525
wbits = ngx_max(wbits, 13);
524526
}
525527

526528
ctx->allocated = 8192 + 16 + (1 << (wbits + 2))
527-
+ 131072 + (1 << (memlevel + 8));
529+
+ 131072 + (1 << (memlevel + 8))
530+
+ 4 * (64 + sizeof(void*));
528531
ctx->zlib_ng = 1;
529532
}
530533
}
@@ -926,13 +929,16 @@ ngx_http_gzip_filter_alloc(void *opaque, u_int items, u_int size)
926929

927930
alloc = items * size;
928931

929-
if (items == 1 && alloc % 512 != 0 && alloc < 8192) {
930-
932+
if (items == 1 && alloc % 512 != 0 && alloc < 8192
933+
&& !ctx->state_allocated)
934+
{
931935
/*
932936
* The zlib deflate_state allocation, it takes about 6K,
933937
* we allocate 8K. Other allocations are divisible by 512.
934938
*/
935939

940+
ctx->state_allocated = 1;
941+
936942
alloc = 8192;
937943
}
938944

0 commit comments

Comments
 (0)