You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@apr.apache.org by bo...@apache.org on 2007/03/31 04:21:59 UTC

svn commit: r524355 - in /apr/apr/trunk: CHANGES file_io/unix/readwrite.c

Author: bojan
Date: Fri Mar 30 19:21:58 2007
New Revision: 524355

URL: http://svn.apache.org/viewvc?view=rev&rev=524355
Log:
Fix locking bug with apr_file_read()/apr_file_gets()

Modified:
    apr/apr/trunk/CHANGES
    apr/apr/trunk/file_io/unix/readwrite.c

Modified: apr/apr/trunk/CHANGES
URL: http://svn.apache.org/viewvc/apr/apr/trunk/CHANGES?view=diff&rev=524355&r1=524354&r2=524355
==============================================================================
--- apr/apr/trunk/CHANGES (original)
+++ apr/apr/trunk/CHANGES Fri Mar 30 19:21:58 2007
@@ -1,5 +1,7 @@
 Changes for APR 1.3.0
 
+  *) Fix locking bug with apr_file_read()/apr_file_gets() [Bojan Smojver]
+
   *) Add the apr_pollcb API as an alternative more efficient method
      of polling sockets, compared to apr_pollset. [Paul Querna]
 

Modified: apr/apr/trunk/file_io/unix/readwrite.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/file_io/unix/readwrite.c?view=diff&rev=524355&r1=524354&r2=524355
==============================================================================
--- apr/apr/trunk/file_io/unix/readwrite.c (original)
+++ apr/apr/trunk/file_io/unix/readwrite.c Fri Mar 30 19:21:58 2007
@@ -25,6 +25,62 @@
 #define USE_WAIT_FOR_IO
 #endif
 
+static apr_status_t apr_file_read_buffered(apr_file_t *thefile, void *buf, apr_size_t *nbytes)
+{
+    apr_ssize_t rv;
+    char *pos = (char *)buf;
+    apr_uint64_t blocksize;
+    apr_uint64_t size = *nbytes;
+
+    if (thefile->direction == 1) {
+        rv = apr_file_flush(thefile);
+        if (rv) {
+            return rv;
+        }
+        thefile->bufpos = 0;
+        thefile->direction = 0;
+        thefile->dataRead = 0;
+    }
+
+    rv = 0;
+    if (thefile->ungetchar != -1) {
+        *pos = (char)thefile->ungetchar;
+        ++pos;
+        --size;
+        thefile->ungetchar = -1;
+    }
+    while (rv == 0 && size > 0) {
+        if (thefile->bufpos >= thefile->dataRead) {
+            int bytesread = read(thefile->filedes, thefile->buffer, 
+                                 thefile->bufsize);
+            if (bytesread == 0) {
+                thefile->eof_hit = TRUE;
+                rv = APR_EOF;
+                break;
+            }
+            else if (bytesread == -1) {
+                rv = errno;
+                break;
+            }
+            thefile->dataRead = bytesread;
+            thefile->filePtr += thefile->dataRead;
+            thefile->bufpos = 0;
+        }
+
+        blocksize = size > thefile->dataRead - thefile->bufpos ? thefile->dataRead - thefile->bufpos : size;
+        memcpy(pos, thefile->buffer + thefile->bufpos, blocksize);
+        thefile->bufpos += blocksize;
+        pos += blocksize;
+        size -= blocksize;
+    }
+
+    *nbytes = pos - (char *)buf;
+    if (*nbytes) {
+        rv = 0;
+    }
+    return rv;
+}
+
 APR_DECLARE(apr_status_t) apr_file_read(apr_file_t *thefile, void *buf, apr_size_t *nbytes)
 {
     apr_ssize_t rv;
@@ -36,67 +92,14 @@
     }
 
     if (thefile->buffered) {
-        char *pos = (char *)buf;
-        apr_uint64_t blocksize;
-        apr_uint64_t size = *nbytes;
-
 #if APR_HAS_THREADS
         if (thefile->thlock) {
             apr_thread_mutex_lock(thefile->thlock);
         }
 #endif
 
-        if (thefile->direction == 1) {
-            rv = apr_file_flush(thefile);
-            if (rv) {
-#if APR_HAS_THREADS
-                if (thefile->thlock) {
-                    apr_thread_mutex_unlock(thefile->thlock);
-                }
-#endif
-                return rv;
-            }
-            thefile->bufpos = 0;
-            thefile->direction = 0;
-            thefile->dataRead = 0;
-        }
-
-        rv = 0;
-        if (thefile->ungetchar != -1) {
-            *pos = (char)thefile->ungetchar;
-            ++pos;
-            --size;
-            thefile->ungetchar = -1;
-        }
-        while (rv == 0 && size > 0) {
-            if (thefile->bufpos >= thefile->dataRead) {
-                int bytesread = read(thefile->filedes, thefile->buffer, 
-                                     thefile->bufsize);
-                if (bytesread == 0) {
-                    thefile->eof_hit = TRUE;
-                    rv = APR_EOF;
-                    break;
-                }
-                else if (bytesread == -1) {
-                    rv = errno;
-                    break;
-                }
-                thefile->dataRead = bytesread;
-                thefile->filePtr += thefile->dataRead;
-                thefile->bufpos = 0;
-            }
+        rv = apr_file_read_buffered(thefile, buf, nbytes);
 
-            blocksize = size > thefile->dataRead - thefile->bufpos ? thefile->dataRead - thefile->bufpos : size;
-            memcpy(pos, thefile->buffer + thefile->bufpos, blocksize);
-            thefile->bufpos += blocksize;
-            pos += blocksize;
-            size -= blocksize;
-        }
-
-        *nbytes = pos - (char *)buf;
-        if (*nbytes) {
-            rv = 0;
-        }
 #if APR_HAS_THREADS
         if (thefile->thlock) {
             apr_thread_mutex_unlock(thefile->thlock);
@@ -374,7 +377,7 @@
             }
             else {
                 nbytes = 1;
-                rv = apr_file_read(thefile, str, &nbytes);
+                rv = apr_file_read_buffered(thefile, str, &nbytes);
                 if (rv != APR_SUCCESS) {
                     break;
                 }