From 973e508af7908e1fa3f0e269268e744be97d9334 Mon Sep 17 00:00:00 2001 From: "roberto@mercury" Date: Fri, 28 Sep 2012 12:37:11 +0200 Subject: [PATCH] reports details on memory allocation error, and bypass broken rack middlewares --- core/utils.c | 2 ++ plugins/rack/rack_plugin.c | 24 +++++++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/core/utils.c b/core/utils.c index 31899470..6f7a195f 100644 --- a/core/utils.c +++ b/core/utils.c @@ -2154,6 +2154,8 @@ void *uwsgi_malloc(size_t size) { char *ptr = malloc(size); if (ptr == NULL) { uwsgi_error("malloc()"); + uwsgi_log("!!! tried memory allocation of %llu bytes !!!\n", (unsigned long long) size); + uwsgi_backtrace(uwsgi.backtrace_depth); exit(1); } diff --git a/plugins/rack/rack_plugin.c b/plugins/rack/rack_plugin.c index 9a2fd6bf..53dd339a 100644 --- a/plugins/rack/rack_plugin.c +++ b/plugins/rack/rack_plugin.c @@ -128,7 +128,7 @@ VALUE rb_uwsgi_io_read(VALUE obj, VALUE args) { struct wsgi_request *wsgi_req; Data_Get_Struct(obj, struct wsgi_request, wsgi_req); VALUE chunk; - unsigned int chunk_size; + long chunk_size; /* When EOF is reached, this method returns nil if length is given and not nil, or "" if length is not given or is nil. @@ -153,7 +153,16 @@ VALUE rb_uwsgi_io_read(VALUE obj, VALUE args) { } // size specified else if (RARRAY_LEN(args) > 0) { - chunk_size = NUM2UINT(RARRAY_PTR(args)[0]); + if (RARRAY_PTR(args)[0] == Qnil) { + chunk_size = wsgi_req->post_cl; + } + else { + chunk_size = NUM2LONG(RARRAY_PTR(args)[0]); + // hack to bypass broken middlewares + if (chunk_size <= 0) { + chunk_size = wsgi_req->post_cl; + } + } char *tmp_chunk = uwsgi_malloc(chunk_size); size_t rlen = fread(tmp_chunk, 1, chunk_size, (FILE *) wsgi_req->async_post); // error, return Qnil @@ -196,7 +205,16 @@ VALUE rb_uwsgi_io_read(VALUE obj, VALUE args) { return chunk; } else if (RARRAY_LEN(args) > 0) { - chunk_size = NUM2UINT(RARRAY_PTR(args)[0]); + if (RARRAY_PTR(args)[0] == Qnil) { + chunk_size = wsgi_req->post_cl; + } + else { + chunk_size = NUM2LONG(RARRAY_PTR(args)[0]); + // hack to respect broken middlewares + if (chunk_size <= 0) { + chunk_size = wsgi_req->post_cl; + } + } if (wsgi_req->buf_pos+chunk_size > wsgi_req->post_cl) { chunk_size = wsgi_req->post_cl-wsgi_req->buf_pos; }