c260e0
From 070718b3e00d0341d44dd5ad4b48fd4468d047c6 Mon Sep 17 00:00:00 2001
c260e0
From: Daniel Stenberg <daniel@haxx.se>
c260e0
Date: Sat, 9 Mar 2013 22:26:07 +0100
c260e0
Subject: [PATCH 1/3] curl_multi_wait: avoid second loop if nothing to do
c260e0
c260e0
... hopefully this will also make clang-analyzer stop warning on
c260e0
potentional NULL dereferences (which were false positives anyway).
c260e0
c260e0
Upstream-commit: 136a3a0ee25f28fec1dde216467389f9e6e4f65c
c260e0
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
c260e0
---
c260e0
 lib/multi.c | 55 ++++++++++++++++++++++++++++++++-----------------------
c260e0
 1 file changed, 32 insertions(+), 23 deletions(-)
c260e0
c260e0
diff --git a/lib/multi.c b/lib/multi.c
c260e0
index 6dfce9b..1136849 100644
c260e0
--- a/lib/multi.c
c260e0
+++ b/lib/multi.c
c260e0
@@ -804,7 +804,8 @@ CURLMcode curl_multi_wait(CURLM *multi_handle,
c260e0
   curl_socket_t sockbunch[MAX_SOCKSPEREASYHANDLE];
c260e0
   int bitmap;
c260e0
   unsigned int i;
c260e0
-  unsigned int nfds = extra_nfds;
c260e0
+  unsigned int nfds = 0;
c260e0
+  unsigned int curlfds;
c260e0
   struct pollfd *ufds = NULL;
c260e0
   long timeout_internal;
c260e0
 
c260e0
@@ -842,6 +843,9 @@ CURLMcode curl_multi_wait(CURLM *multi_handle,
c260e0
     easy = easy->next; /* check next handle */
c260e0
   }
c260e0
 
c260e0
+  curlfds = nfds; /* number of internal file descriptors */
c260e0
+  nfds += extra_nfds; /* add the externally provided ones */
c260e0
+
c260e0
   if(nfds) {
c260e0
     ufds = malloc(nfds * sizeof(struct pollfd));
c260e0
     if(!ufds)
c260e0
@@ -849,32 +853,37 @@ CURLMcode curl_multi_wait(CURLM *multi_handle,
c260e0
   }
c260e0
   nfds = 0;
c260e0
 
c260e0
-  /* Add the curl handles to our pollfds first */
c260e0
-  easy=multi->easy.next;
c260e0
-  while(easy != &multi->easy) {
c260e0
-    bitmap = multi_getsock(easy, sockbunch, MAX_SOCKSPEREASYHANDLE);
c260e0
+  /* only do the second loop if we found descriptors in the first stage run
c260e0
+     above */
c260e0
 
c260e0
-    for(i=0; i< MAX_SOCKSPEREASYHANDLE; i++) {
c260e0
-      curl_socket_t s = CURL_SOCKET_BAD;
c260e0
+  if(curlfds) {
c260e0
+    /* Add the curl handles to our pollfds first */
c260e0
+    easy=multi->easy.next;
c260e0
+    while(easy != &multi->easy) {
c260e0
+      bitmap = multi_getsock(easy, sockbunch, MAX_SOCKSPEREASYHANDLE);
c260e0
 
c260e0
-      if(bitmap & GETSOCK_READSOCK(i)) {
c260e0
-        ufds[nfds].fd = sockbunch[i];
c260e0
-        ufds[nfds].events = POLLIN;
c260e0
-        ++nfds;
c260e0
-        s = sockbunch[i];
c260e0
-      }
c260e0
-      if(bitmap & GETSOCK_WRITESOCK(i)) {
c260e0
-        ufds[nfds].fd = sockbunch[i];
c260e0
-        ufds[nfds].events = POLLOUT;
c260e0
-        ++nfds;
c260e0
-        s = sockbunch[i];
c260e0
-      }
c260e0
-      if(s == CURL_SOCKET_BAD) {
c260e0
-        break;
c260e0
+      for(i=0; i< MAX_SOCKSPEREASYHANDLE; i++) {
c260e0
+        curl_socket_t s = CURL_SOCKET_BAD;
c260e0
+
c260e0
+        if(bitmap & GETSOCK_READSOCK(i)) {
c260e0
+          ufds[nfds].fd = sockbunch[i];
c260e0
+          ufds[nfds].events = POLLIN;
c260e0
+          ++nfds;
c260e0
+          s = sockbunch[i];
c260e0
+        }
c260e0
+        if(bitmap & GETSOCK_WRITESOCK(i)) {
c260e0
+          ufds[nfds].fd = sockbunch[i];
c260e0
+          ufds[nfds].events = POLLOUT;
c260e0
+          ++nfds;
c260e0
+          s = sockbunch[i];
c260e0
+        }
c260e0
+        if(s == CURL_SOCKET_BAD) {
c260e0
+          break;
c260e0
+        }
c260e0
       }
c260e0
-    }
c260e0
 
c260e0
-    easy = easy->next; /* check next handle */
c260e0
+      easy = easy->next; /* check next handle */
c260e0
+    }
c260e0
   }
c260e0
 
c260e0
   /* Add external file descriptions from poll-like struct curl_waitfd */
c260e0
-- 
c260e0
2.5.5
c260e0
c260e0
c260e0
From f8b84a52088a99d8128c2234f626ed233beabeae Mon Sep 17 00:00:00 2001
c260e0
From: Evgeny Turnaev <turnaev.e@gmail.com>
c260e0
Date: Thu, 18 Jul 2013 00:06:09 +0200
c260e0
Subject: [PATCH 2/3] curl_multi_wait: set revents for extra fds
c260e0
c260e0
Pass back the revents that happened for the user-provided file
c260e0
descriptors.
c260e0
c260e0
Upstream-commit: 6d30f8ebed34e7276c2a59ee20d466bff17fee56
c260e0
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
c260e0
---
c260e0
 lib/multi.c | 5 ++++-
c260e0
 1 file changed, 4 insertions(+), 1 deletion(-)
c260e0
c260e0
diff --git a/lib/multi.c b/lib/multi.c
c260e0
index 1136849..81bcfba 100644
c260e0
--- a/lib/multi.c
c260e0
+++ b/lib/multi.c
c260e0
@@ -803,7 +803,7 @@ CURLMcode curl_multi_wait(CURLM *multi_handle,
c260e0
   struct Curl_one_easy *easy;
c260e0
   curl_socket_t sockbunch[MAX_SOCKSPEREASYHANDLE];
c260e0
   int bitmap;
c260e0
-  unsigned int i;
c260e0
+  unsigned int i, j;
c260e0
   unsigned int nfds = 0;
c260e0
   unsigned int curlfds;
c260e0
   struct pollfd *ufds = NULL;
c260e0
@@ -905,6 +905,9 @@ CURLMcode curl_multi_wait(CURLM *multi_handle,
c260e0
   else
c260e0
     i = 0;
c260e0
 
c260e0
+  for(j = nfds - extra_nfds; j < nfds; j++)
c260e0
+    extra_fds[j].revents = ufds[j].revents;
c260e0
+
c260e0
   Curl_safefree(ufds);
c260e0
   if(ret)
c260e0
     *ret = i;
c260e0
-- 
c260e0
2.5.5
c260e0
c260e0
c260e0
From db2e5b5ffe5408aa892dee9e7f036fe0ea16963d Mon Sep 17 00:00:00 2001
c260e0
From: Daniel Stenberg <daniel@haxx.se>
c260e0
Date: Thu, 18 Jul 2013 23:36:59 +0200
c260e0
Subject: [PATCH 3/3] curl_multi_wait: fix revents
c260e0
c260e0
Commit 6d30f8ebed34e7276 didn't work properly. First, it used the wrong
c260e0
array index, but this fix also:
c260e0
c260e0
1 - only does the copying if indeed there was any activity
c260e0
c260e0
2 - makes sure to properly translate between internal and external
c260e0
bitfields, which are not guaranteed to match
c260e0
c260e0
Reported-by: Evgeny Turnaev
c260e0
c260e0
Upstream-commit: 513e587c5eb966038731530c8f47fe0cf27513ce
c260e0
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
c260e0
---
c260e0
 lib/multi.c | 28 +++++++++++++++++++++++-----
c260e0
 1 file changed, 23 insertions(+), 5 deletions(-)
c260e0
c260e0
diff --git a/lib/multi.c b/lib/multi.c
c260e0
index 81bcfba..0e0bb19 100644
c260e0
--- a/lib/multi.c
c260e0
+++ b/lib/multi.c
c260e0
@@ -803,7 +803,7 @@ CURLMcode curl_multi_wait(CURLM *multi_handle,
c260e0
   struct Curl_one_easy *easy;
c260e0
   curl_socket_t sockbunch[MAX_SOCKSPEREASYHANDLE];
c260e0
   int bitmap;
c260e0
-  unsigned int i, j;
c260e0
+  unsigned int i;
c260e0
   unsigned int nfds = 0;
c260e0
   unsigned int curlfds;
c260e0
   struct pollfd *ufds = NULL;
c260e0
@@ -899,15 +899,33 @@ CURLMcode curl_multi_wait(CURLM *multi_handle,
c260e0
     ++nfds;
c260e0
   }
c260e0
 
c260e0
-  if(nfds)
c260e0
+  if(nfds) {
c260e0
     /* wait... */
c260e0
     i = Curl_poll(ufds, nfds, timeout_ms);
c260e0
+
c260e0
+    if(i) {
c260e0
+      unsigned int j;
c260e0
+      /* copy revents results from the poll to the curl_multi_wait poll
c260e0
+         struct, the bit values of the actual underlying poll() implementation
c260e0
+         may not be the same as the ones in the public libcurl API! */
c260e0
+      for(j = 0; j < extra_nfds; j++) {
c260e0
+        unsigned short mask = 0;
c260e0
+        unsigned r = ufds[curlfds + j].revents;
c260e0
+
c260e0
+        if(r & POLLIN)
c260e0
+          mask |= CURL_WAIT_POLLIN;
c260e0
+        if(r & POLLOUT)
c260e0
+          mask |= CURL_WAIT_POLLOUT;
c260e0
+        if(r & POLLPRI)
c260e0
+          mask |= CURL_WAIT_POLLPRI;
c260e0
+
c260e0
+        extra_fds[j].revents = mask;
c260e0
+      }
c260e0
+    }
c260e0
+  }
c260e0
   else
c260e0
     i = 0;
c260e0
 
c260e0
-  for(j = nfds - extra_nfds; j < nfds; j++)
c260e0
-    extra_fds[j].revents = ufds[j].revents;
c260e0
-
c260e0
   Curl_safefree(ufds);
c260e0
   if(ret)
c260e0
     *ret = i;
c260e0
-- 
c260e0
2.5.5
c260e0