[nginx] Don't lose pointer to first nonempty buf in ngx_*_sendfi...

Gleb Smirnoff glebius at nginx.com
Thu Aug 8 11:37:16 UTC 2013


details:   http://hg.nginx.org/nginx/rev/ad137a80919f
branches:  
changeset: 5320:ad137a80919f
user:      Gleb Smirnoff <glebius at nginx.com>
date:      Thu Aug 08 15:06:39 2013 +0400
description:
Don't lose pointer to first nonempty buf in ngx_*_sendfile_chain().

In ngx_*_sendfile_chain() when calculating pointer to a first
non-zero sized buf, use "in" as iterator.  This fixes processing
of zero sized buf(s) after EINTR.  Otherwise function can return
zero sized buf to caller, and later ngx_http_write_filter()
logs warning.

diffstat:

 src/os/unix/ngx_darwin_sendfile_chain.c   |  30 ++++++++++++--------------
 src/os/unix/ngx_freebsd_sendfile_chain.c  |  34 ++++++++++++++----------------
 src/os/unix/ngx_linux_sendfile_chain.c    |  30 ++++++++++++--------------
 src/os/unix/ngx_solaris_sendfilev_chain.c |  30 ++++++++++++--------------
 4 files changed, 58 insertions(+), 66 deletions(-)

diffs (298 lines):

diff -r 50f531a55b73 -r ad137a80919f src/os/unix/ngx_darwin_sendfile_chain.c
--- a/src/os/unix/ngx_darwin_sendfile_chain.c	Wed Aug 07 20:01:43 2013 +0400
+++ b/src/os/unix/ngx_darwin_sendfile_chain.c	Thu Aug 08 15:06:39 2013 +0400
@@ -317,9 +317,9 @@ ngx_darwin_sendfile_chain(ngx_connection
 
         c->sent += sent;
 
-        for (cl = in; cl; cl = cl->next) {
+        for ( /* void */ ; in; in = in->next) {
 
-            if (ngx_buf_special(cl->buf)) {
+            if (ngx_buf_special(in->buf)) {
                 continue;
             }
 
@@ -327,28 +327,28 @@ ngx_darwin_sendfile_chain(ngx_connection
                 break;
             }
 
-            size = ngx_buf_size(cl->buf);
+            size = ngx_buf_size(in->buf);
 
             if (sent >= size) {
                 sent -= size;
 
-                if (ngx_buf_in_memory(cl->buf)) {
-                    cl->buf->pos = cl->buf->last;
+                if (ngx_buf_in_memory(in->buf)) {
+                    in->buf->pos = in->buf->last;
                 }
 
-                if (cl->buf->in_file) {
-                    cl->buf->file_pos = cl->buf->file_last;
+                if (in->buf->in_file) {
+                    in->buf->file_pos = in->buf->file_last;
                 }
 
                 continue;
             }
 
-            if (ngx_buf_in_memory(cl->buf)) {
-                cl->buf->pos += (size_t) sent;
+            if (ngx_buf_in_memory(in->buf)) {
+                in->buf->pos += (size_t) sent;
             }
 
-            if (cl->buf->in_file) {
-                cl->buf->file_pos += sent;
+            if (in->buf->in_file) {
+                in->buf->file_pos += sent;
             }
 
             break;
@@ -360,13 +360,11 @@ ngx_darwin_sendfile_chain(ngx_connection
 
         if (!complete) {
             wev->ready = 0;
-            return cl;
+            return in;
         }
 
-        if (send >= limit || cl == NULL) {
-            return cl;
+        if (send >= limit || in == NULL) {
+            return in;
         }
-
-        in = cl;
     }
 }
diff -r 50f531a55b73 -r ad137a80919f src/os/unix/ngx_freebsd_sendfile_chain.c
--- a/src/os/unix/ngx_freebsd_sendfile_chain.c	Wed Aug 07 20:01:43 2013 +0400
+++ b/src/os/unix/ngx_freebsd_sendfile_chain.c	Thu Aug 08 15:06:39 2013 +0400
@@ -368,9 +368,9 @@ ngx_freebsd_sendfile_chain(ngx_connectio
 
         c->sent += sent;
 
-        for (cl = in; cl; cl = cl->next) {
+        for ( /* void */ ; in; in = in->next) {
 
-            if (ngx_buf_special(cl->buf)) {
+            if (ngx_buf_special(in->buf)) {
                 continue;
             }
 
@@ -378,28 +378,28 @@ ngx_freebsd_sendfile_chain(ngx_connectio
                 break;
             }
 
-            size = ngx_buf_size(cl->buf);
+            size = ngx_buf_size(in->buf);
 
             if (sent >= size) {
                 sent -= size;
 
-                if (ngx_buf_in_memory(cl->buf)) {
-                    cl->buf->pos = cl->buf->last;
+                if (ngx_buf_in_memory(in->buf)) {
+                    in->buf->pos = in->buf->last;
                 }
 
-                if (cl->buf->in_file) {
-                    cl->buf->file_pos = cl->buf->file_last;
+                if (in->buf->in_file) {
+                    in->buf->file_pos = in->buf->file_last;
                 }
 
                 continue;
             }
 
-            if (ngx_buf_in_memory(cl->buf)) {
-                cl->buf->pos += (size_t) sent;
+            if (ngx_buf_in_memory(in->buf)) {
+                in->buf->pos += (size_t) sent;
             }
 
-            if (cl->buf->in_file) {
-                cl->buf->file_pos += sent;
+            if (in->buf->in_file) {
+                in->buf->file_pos += sent;
             }
 
             break;
@@ -407,7 +407,7 @@ ngx_freebsd_sendfile_chain(ngx_connectio
 
 #if (NGX_HAVE_AIO_SENDFILE)
         if (c->busy_sendfile) {
-            return cl;
+            return in;
         }
 #endif
 
@@ -421,7 +421,7 @@ ngx_freebsd_sendfile_chain(ngx_connectio
              */
 
             wev->ready = 0;
-            return cl;
+            return in;
         }
 
         if (eintr) {
@@ -430,13 +430,11 @@ ngx_freebsd_sendfile_chain(ngx_connectio
 
         if (!complete) {
             wev->ready = 0;
-            return cl;
+            return in;
         }
 
-        if (send >= limit || cl == NULL) {
-            return cl;
+        if (send >= limit || in == NULL) {
+            return in;
         }
-
-        in = cl;
     }
 }
diff -r 50f531a55b73 -r ad137a80919f src/os/unix/ngx_linux_sendfile_chain.c
--- a/src/os/unix/ngx_linux_sendfile_chain.c	Wed Aug 07 20:01:43 2013 +0400
+++ b/src/os/unix/ngx_linux_sendfile_chain.c	Thu Aug 08 15:06:39 2013 +0400
@@ -325,9 +325,9 @@ ngx_linux_sendfile_chain(ngx_connection_
 
         c->sent += sent;
 
-        for (cl = in; cl; cl = cl->next) {
+        for ( /* void */ ; in; in = in->next) {
 
-            if (ngx_buf_special(cl->buf)) {
+            if (ngx_buf_special(in->buf)) {
                 continue;
             }
 
@@ -335,28 +335,28 @@ ngx_linux_sendfile_chain(ngx_connection_
                 break;
             }
 
-            size = ngx_buf_size(cl->buf);
+            size = ngx_buf_size(in->buf);
 
             if (sent >= size) {
                 sent -= size;
 
-                if (ngx_buf_in_memory(cl->buf)) {
-                    cl->buf->pos = cl->buf->last;
+                if (ngx_buf_in_memory(in->buf)) {
+                    in->buf->pos = in->buf->last;
                 }
 
-                if (cl->buf->in_file) {
-                    cl->buf->file_pos = cl->buf->file_last;
+                if (in->buf->in_file) {
+                    in->buf->file_pos = in->buf->file_last;
                 }
 
                 continue;
             }
 
-            if (ngx_buf_in_memory(cl->buf)) {
-                cl->buf->pos += (size_t) sent;
+            if (ngx_buf_in_memory(in->buf)) {
+                in->buf->pos += (size_t) sent;
             }
 
-            if (cl->buf->in_file) {
-                cl->buf->file_pos += sent;
+            if (in->buf->in_file) {
+                in->buf->file_pos += sent;
             }
 
             break;
@@ -368,13 +368,11 @@ ngx_linux_sendfile_chain(ngx_connection_
 
         if (!complete) {
             wev->ready = 0;
-            return cl;
+            return in;
         }
 
-        if (send >= limit || cl == NULL) {
-            return cl;
+        if (send >= limit || in == NULL) {
+            return in;
         }
-
-        in = cl;
     }
 }
diff -r 50f531a55b73 -r ad137a80919f src/os/unix/ngx_solaris_sendfilev_chain.c
--- a/src/os/unix/ngx_solaris_sendfilev_chain.c	Wed Aug 07 20:01:43 2013 +0400
+++ b/src/os/unix/ngx_solaris_sendfilev_chain.c	Thu Aug 08 15:06:39 2013 +0400
@@ -207,9 +207,9 @@ ngx_solaris_sendfilev_chain(ngx_connecti
 
         c->sent += sent;
 
-        for (cl = in; cl; cl = cl->next) {
+        for ( /* void */ ; in; in = in->next) {
 
-            if (ngx_buf_special(cl->buf)) {
+            if (ngx_buf_special(in->buf)) {
                 continue;
             }
 
@@ -217,28 +217,28 @@ ngx_solaris_sendfilev_chain(ngx_connecti
                 break;
             }
 
-            size = ngx_buf_size(cl->buf);
+            size = ngx_buf_size(in->buf);
 
             if ((off_t) sent >= size) {
                 sent = (size_t) ((off_t) sent - size);
 
-                if (ngx_buf_in_memory(cl->buf)) {
-                    cl->buf->pos = cl->buf->last;
+                if (ngx_buf_in_memory(in->buf)) {
+                    in->buf->pos = in->buf->last;
                 }
 
-                if (cl->buf->in_file) {
-                    cl->buf->file_pos = cl->buf->file_last;
+                if (in->buf->in_file) {
+                    in->buf->file_pos = in->buf->file_last;
                 }
 
                 continue;
             }
 
-            if (ngx_buf_in_memory(cl->buf)) {
-                cl->buf->pos += sent;
+            if (ngx_buf_in_memory(in->buf)) {
+                in->buf->pos += sent;
             }
 
-            if (cl->buf->in_file) {
-                cl->buf->file_pos += sent;
+            if (in->buf->in_file) {
+                in->buf->file_pos += sent;
             }
 
             break;
@@ -250,13 +250,11 @@ ngx_solaris_sendfilev_chain(ngx_connecti
 
         if (!complete) {
             wev->ready = 0;
-            return cl;
+            return in;
         }
 
-        if (send >= limit || cl == NULL) {
-            return cl;
+        if (send >= limit || in == NULL) {
+            return in;
         }
-
-        in = cl;
     }
 }



More information about the nginx-devel mailing list