Skip to content

Commit

Permalink
Merge pull request #94 from quiltdata/memory_fix
Browse files Browse the repository at this point in the history
Fix memory usage when reading input
  • Loading branch information
evanmiller committed Dec 17, 2022
2 parents d2d77c4 + 288221c commit b2b1b93
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 40 deletions.
37 changes: 4 additions & 33 deletions ngx_http_zip_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "ngx_http_zip_file.h"
#include "ngx_http_zip_headers.h"

static size_t ngx_chain_length(ngx_chain_t *chain_link);
static ngx_chain_t *ngx_chain_last_link(ngx_chain_t *chain_link);
static ngx_int_t ngx_http_zip_discard_chain(ngx_http_request_t *r,
ngx_chain_t *in);
Expand Down Expand Up @@ -95,15 +94,6 @@ ngx_module_t ngx_http_zip_module = {
NGX_MODULE_V1_PADDING
};

static size_t ngx_chain_length(ngx_chain_t *chain_link)
{
size_t len;
for (len=0; chain_link; chain_link = chain_link->next) {
len += chain_link->buf->last - chain_link->buf->pos;
}
return len;
}

static ngx_chain_t *ngx_chain_last_link(ngx_chain_t *chain_link)
{
ngx_chain_t *cl;
Expand Down Expand Up @@ -139,34 +129,14 @@ ngx_http_zip_ranges_intersect(ngx_http_zip_range_t *range1, ngx_http_zip_range_t
static ngx_int_t ngx_http_zip_copy_unparsed_request(ngx_http_request_t *r,
ngx_chain_t *in, ngx_http_zip_ctx_t *ctx)
{
ngx_str_t *old_unparsed_request;
ngx_chain_t *chain_link;
size_t len, offset = 0;

old_unparsed_request = ctx->unparsed_request;

len = ngx_chain_length(in);

if (old_unparsed_request != NULL)
len += old_unparsed_request->len;

if ((ctx->unparsed_request = ngx_palloc(r->pool, sizeof(ngx_str_t))) == NULL
|| (ctx->unparsed_request->data = ngx_palloc(r->pool, len)) == NULL) {
return NGX_ERROR;
}

if (old_unparsed_request != NULL) {
ngx_memcpy(ctx->unparsed_request->data, old_unparsed_request->data, old_unparsed_request->len);
offset += old_unparsed_request->len;
}
void *buf;

for (chain_link = in; chain_link; chain_link = chain_link->next ) {
ngx_memcpy(ctx->unparsed_request->data + offset, chain_link->buf->pos, chain_link->buf->last - chain_link->buf->pos);
offset += chain_link->buf->last - chain_link->buf->pos;
buf = ngx_array_push_n(&ctx->unparsed_request, chain_link->buf->last - chain_link->buf->pos);
ngx_memcpy(buf, chain_link->buf->pos, chain_link->buf->last - chain_link->buf->pos);
}

ctx->unparsed_request->len = offset;

chain_link = ngx_chain_last_link(in);

return chain_link->buf->last_buf ? NGX_OK: NGX_AGAIN;
Expand Down Expand Up @@ -220,6 +190,7 @@ ngx_http_zip_main_request_header_filter(ngx_http_request_t *r)
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "mod_zip: X-Archive-Files found");

if ((ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_zip_ctx_t))) == NULL
|| ngx_array_init(&ctx->unparsed_request, r->pool, 64 * 1024, 1) == NGX_ERROR
|| ngx_array_init(&ctx->files, r->pool, 1, sizeof(ngx_http_zip_file_t)) == NGX_ERROR
|| ngx_array_init(&ctx->ranges, r->pool, 1, sizeof(ngx_http_zip_range_t)) == NGX_ERROR
|| ngx_array_init(&ctx->pass_srq_headers, r->pool, 1, sizeof(ngx_str_t)) == NGX_ERROR)
Expand Down
2 changes: 1 addition & 1 deletion ngx_http_zip_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ typedef struct {
} ngx_http_zip_piece_t;

typedef struct {
ngx_str_t *unparsed_request;
ngx_array_t unparsed_request;
ngx_http_zip_piece_t *pieces;
ngx_array_t files;
ngx_array_t ranges;
Expand Down
6 changes: 3 additions & 3 deletions ngx_http_zip_parsers.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ ngx_int_t
ngx_http_zip_parse_request(ngx_http_zip_ctx_t *ctx)
{
int cs;
u_char *p = ctx->unparsed_request->data;
u_char *pe = ctx->unparsed_request->data + ctx->unparsed_request->len;
u_char *eof = ctx->unparsed_request->data + ctx->unparsed_request->len;
u_char *p = ctx->unparsed_request.elts;
u_char *pe = p + ctx->unparsed_request.nelts;
u_char *eof = pe;
ngx_http_zip_file_t *parsing_file = NULL;


Expand Down
6 changes: 3 additions & 3 deletions ngx_http_zip_parsers.rl
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ ngx_int_t
ngx_http_zip_parse_request(ngx_http_zip_ctx_t *ctx)
{
int cs;
u_char *p = ctx->unparsed_request->data;
u_char *pe = ctx->unparsed_request->data + ctx->unparsed_request->len;
u_char *eof = ctx->unparsed_request->data + ctx->unparsed_request->len;
u_char *p = ctx->unparsed_request.elts;
u_char *pe = p + ctx->unparsed_request.nelts;
u_char *eof = pe;
ngx_http_zip_file_t *parsing_file = NULL;

%%{
Expand Down

0 comments on commit b2b1b93

Please sign in to comment.