comparison src/http/modules/ngx_http_gzip_filter_module.c @ 7545:0ef2bc0ec9c9

Gzip: fixed "zero size buf" alerts after ac5a741d39cf. After ac5a741d39cf it is now possible that after zstream.avail_out reaches 0 and we allocate additional buffer, there will be no more data to put into this buffer, triggering "zero size buf" alert. Fix is to reset b->temporary flag in this case. Additionally, an optimization added to avoid allocating additional buffer in this case, by checking if last deflate() call returned Z_STREAM_END. Note that checking for Z_STREAM_END by itself is not enough to fix alerts, as deflate() can return Z_STREAM_END without producing any output if the buffer is smaller than gzip trailer. Reported by Witold Filipczyk, http://mailman.nginx.org/pipermail/nginx-devel/2019-July/012469.html.
author Maxim Dounin <mdounin@mdounin.ru>
date Wed, 31 Jul 2019 17:29:00 +0300
parents ac5a741d39cf
children 1f3d0d9f893f
comparison
equal deleted inserted replaced
7544:e1a3fa4c054f 7545:0ef2bc0ec9c9
776 } 776 }
777 } 777 }
778 778
779 ctx->out_buf->last = ctx->zstream.next_out; 779 ctx->out_buf->last = ctx->zstream.next_out;
780 780
781 if (ctx->zstream.avail_out == 0) { 781 if (ctx->zstream.avail_out == 0 && rc != Z_STREAM_END) {
782 782
783 /* zlib wants to output some more gzipped data */ 783 /* zlib wants to output some more gzipped data */
784 784
785 cl = ngx_alloc_chain_link(r->pool); 785 cl = ngx_alloc_chain_link(r->pool);
786 if (cl == NULL) { 786 if (cl == NULL) {
866 static ngx_int_t 866 static ngx_int_t
867 ngx_http_gzip_filter_deflate_end(ngx_http_request_t *r, 867 ngx_http_gzip_filter_deflate_end(ngx_http_request_t *r,
868 ngx_http_gzip_ctx_t *ctx) 868 ngx_http_gzip_ctx_t *ctx)
869 { 869 {
870 int rc; 870 int rc;
871 ngx_buf_t *b;
871 ngx_chain_t *cl; 872 ngx_chain_t *cl;
872 873
873 ctx->zin = ctx->zstream.total_in; 874 ctx->zin = ctx->zstream.total_in;
874 ctx->zout = ctx->zstream.total_out; 875 ctx->zout = ctx->zstream.total_out;
875 876
886 cl = ngx_alloc_chain_link(r->pool); 887 cl = ngx_alloc_chain_link(r->pool);
887 if (cl == NULL) { 888 if (cl == NULL) {
888 return NGX_ERROR; 889 return NGX_ERROR;
889 } 890 }
890 891
891 cl->buf = ctx->out_buf; 892 b = ctx->out_buf;
893
894 if (ngx_buf_size(b) == 0) {
895 b->temporary = 0;
896 }
897
898 b->last_buf = 1;
899
900 cl->buf = b;
892 cl->next = NULL; 901 cl->next = NULL;
893 *ctx->last_out = cl; 902 *ctx->last_out = cl;
894 ctx->last_out = &cl->next; 903 ctx->last_out = &cl->next;
895
896 ctx->out_buf->last_buf = 1;
897 904
898 ctx->zstream.avail_in = 0; 905 ctx->zstream.avail_in = 0;
899 ctx->zstream.avail_out = 0; 906 ctx->zstream.avail_out = 0;
900 907
901 ctx->done = 1; 908 ctx->done = 1;