The current WinSock API documentation for select() states:

"Any two of the parameters, readfds, writefds, or exceptfds, can be given as null. At least one must be non-null, and any non-null descriptor set must contain at least one handle to a socket."

http://msdn.microsoft.com/en-ca/library/windows/desktop/ms740141(v=vs.85).aspx

When using select(), cURL doesn't adhere to this (WinSock-specific) rule, and can ask to monitor empty fd_sets, which leads to select() returning WSAEINVAL (i.e. EINVAL) and connections failing in mysterious ways as a result (at least when using the curl_multi_socket_action() interface).

The attached patch (against 7.37.0) changes how select() is called when USE_WINSOCK is defined to conform to the above rule.

--
Brad Spencer

--- curl-7.37.0/lib/select.c~   2013-09-08 19:11:15 -0300
+++ curl-7.37.0/lib/select.c    2014-05-16 14:52:55 -0300
@@ -288,7 +288,37 @@
       pending_tv.tv_sec = 0;
       pending_tv.tv_usec = 0;
     }
-    r = select((int)maxfd + 1, &fds_read, &fds_write, &fds_err, ptimeout);
+
+    /* WinSock select() must not be called with an fd_set that contains zero
+       fd flags, or it will return WSAEINVAL.  But, it also can't be called
+       with no fd_sets at all!  From the documentation:
+
+         Any two of the parameters, readfds, writefds, or exceptfds, can be
+         given as null. At least one must be non-null, and any non-null
+         descriptor set must contain at least one handle to a socket.
+
+       We know that we have at least one bit set in at least two fd_sets in
+       this case, but we may have no bits set in either fds_read or fd_write,
+       so check for that and handle it.  Luckily, with WinSock, we can _also_
+       ask how many bits are set on an fd_set.
+
+       It is unclear why WinSock doesn't just handle this for us instead of
+       calling this an error.
+
+       Note also that WinSock ignores the first argument, so we don't worry
+       about the fact that maxfd is computed incorrectly with WinSock (since
+       curl_socket_t is unsigned in such cases and thus -1 is the largest
+       value).
+    */
+    r = select((int)maxfd + 1,
+#ifndef USE_WINSOCK
+               &fds_read,
+               &fds_write,
+#else
+               fds_read.fd_count ? &fds_read : NULL,
+               fds_write.fd_count ? &fds_write : NULL,
+#endif
+               &fds_err, ptimeout);
     if(r != -1)
       break;
     error = SOCKERRNO;
@@ -446,6 +471,16 @@
     }
   }
 
+#ifdef USE_WINSOCK
+  /* WinSock select() can't handle zero events.  See the comment about this in
+     Curl_check_socket(). */
+  if(fds_read.fd_count == 0 && fds_write.fd_count == 0
+     && fds_err.fd_count == 0) {
+    r = Curl_wait_ms(timeout_ms);
+    return r;
+  }
+#endif
+  
   ptimeout = (timeout_ms < 0) ? NULL : &pending_tv;
 
   do {
@@ -457,7 +492,19 @@
       pending_tv.tv_sec = 0;
       pending_tv.tv_usec = 0;
     }
-    r = select((int)maxfd + 1, &fds_read, &fds_write, &fds_err, ptimeout);
+    r = select((int)maxfd + 1,
+#ifndef USE_WINSOCK
+               &fds_read, &fds_write, &fds_err,
+#else
+               /* WinSock select() can't handle fd_sets with zero bits set, so
+                  don't give it such arguments.  See the comment about this in
+                  Curl_check_socket().
+               */
+               fds_read.fd_count ? &fds_read : NULL,
+               fds_write.fd_count ? &fds_write : NULL,
+               fds_err.fd_count ? &fds_err : NULL,
+#endif
+               ptimeout);
     if(r != -1)
       break;
     error = SOCKERRNO;
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html

Reply via email to