On Saturday 24 of April 2010 10:34:08 Daniel Stenberg wrote:
> On Sat, 24 Apr 2010, Kamil Dudka wrote:
> > You say there we need to "make sure that maxfd is bigger than -1".  But
> > in the code you intentionally do not in order to keep it working.  What
> > about updating all the comments like following?
> >
> > /* In a real-world program you OF COURSE check the return code of the
> > function calls.  On success, the value of maxfd is guaranteed to be
> > greater or equal than than -1.  We call select(maxfd + 1, ...), specially
> > in case of (maxfd == -1), we call select(0, ...), which is basically
> > equal to sleep. */
>
> Yes I think that is a good idea. That seems like a better comment to use so
> that it is made more obvious what happens in the -1 case.

proposed patch

Kamil
From 4e7ddcc756253645b56724db375dc1ab94e44931 Mon Sep 17 00:00:00 2001
From: Kamil Dudka <[email protected]>
Date: Sat, 24 Apr 2010 12:05:37 +0200
Subject: [PATCH] test536: do not fail with threaded DNS resolver

Also tweaked comments in certain examples using curl_multi_fdset().
---
 CHANGES                             |    4 ++++
 docs/examples/fopen.c               |    8 +++++---
 docs/examples/multi-app.c           |    8 +++++---
 docs/examples/multi-debugcallback.c |    8 +++++---
 docs/examples/multi-double.c        |    8 +++++---
 docs/examples/multi-post.c          |    8 +++++---
 docs/examples/multi-single.c        |    8 +++++---
 tests/libtest/lib536.c              |   16 ++++++++++++----
 8 files changed, 46 insertions(+), 22 deletions(-)

diff --git a/CHANGES b/CHANGES
index 0184671..b765387 100644
--- a/CHANGES
+++ b/CHANGES
@@ -6,6 +6,10 @@
 
                                   Changelog
 
+Kamil Dudka (24 Apr 2010)
+- Fixed test536 in order to not fail with threaded DNS resolver and tweaked
+  comments in certain examples using curl_multi_fdset().
+
 Daniel Stenberg (21 Apr 2010)
 - The -O option caused curl to crash on windows and DOS due to the tool
   writing out of boundary memory.
diff --git a/docs/examples/fopen.c b/docs/examples/fopen.c
index e3814e6..ec12bd0 100644
--- a/docs/examples/fopen.c
+++ b/docs/examples/fopen.c
@@ -131,7 +131,6 @@ fill_buffer(URL_FILE *file,int want,int waittime)
     fd_set fdread;
     fd_set fdwrite;
     fd_set fdexcep;
-    int maxfd;
     struct timeval timeout;
     int rc;
 
@@ -144,6 +143,7 @@ fill_buffer(URL_FILE *file,int want,int waittime)
     /* attempt to fill buffer */
     do
     {
+        int maxfd = -1;
         FD_ZERO(&fdread);
         FD_ZERO(&fdwrite);
         FD_ZERO(&fdexcep);
@@ -156,8 +156,10 @@ fill_buffer(URL_FILE *file,int want,int waittime)
         curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
 
         /* In a real-world program you OF COURSE check the return code of the
-           function calls, *and* you make sure that maxfd is bigger than -1
-           so that the call to select() below makes sense! */
+           function calls.  On success, the value of maxfd is guaranteed to be
+           greater or equal than than -1.  We call select(maxfd + 1, ...),
+           specially in case of (maxfd == -1), we call select(0, ...), which is
+           basically equal to sleep. */
 
         rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
 
diff --git a/docs/examples/multi-app.c b/docs/examples/multi-app.c
index a86dd0e..699ce59 100644
--- a/docs/examples/multi-app.c
+++ b/docs/examples/multi-app.c
@@ -66,7 +66,7 @@ int main(int argc, char **argv)
     fd_set fdread;
     fd_set fdwrite;
     fd_set fdexcep;
-    int maxfd;
+    int maxfd = -1;
 
     FD_ZERO(&fdread);
     FD_ZERO(&fdwrite);
@@ -80,8 +80,10 @@ int main(int argc, char **argv)
     curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
 
     /* In a real-world program you OF COURSE check the return code of the
-       function calls, *and* you make sure that maxfd is bigger than -1 so
-       that the call to select() below makes sense! */
+       function calls.  On success, the value of maxfd is guaranteed to be
+       greater or equal than than -1.  We call select(maxfd + 1, ...),
+       specially in case of (maxfd == -1), we call select(0, ...), which is
+       basically equal to sleep. */
 
     rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
 
diff --git a/docs/examples/multi-debugcallback.c b/docs/examples/multi-debugcallback.c
index b10b47c..96af1c4 100644
--- a/docs/examples/multi-debugcallback.c
+++ b/docs/examples/multi-debugcallback.c
@@ -140,7 +140,7 @@ int main(int argc, char **argv)
     fd_set fdread;
     fd_set fdwrite;
     fd_set fdexcep;
-    int maxfd;
+    int maxfd = -1;
 
     FD_ZERO(&fdread);
     FD_ZERO(&fdwrite);
@@ -154,8 +154,10 @@ int main(int argc, char **argv)
     curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
 
     /* In a real-world program you OF COURSE check the return code of the
-       function calls, *and* you make sure that maxfd is bigger than -1
-       so that the call to select() below makes sense! */
+       function calls.  On success, the value of maxfd is guaranteed to be
+       greater or equal than than -1.  We call select(maxfd + 1, ...),
+       specially in case of (maxfd == -1), we call select(0, ...), which is
+       basically equal to sleep. */
 
     rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
 
diff --git a/docs/examples/multi-double.c b/docs/examples/multi-double.c
index ef3bf92..66ae737 100644
--- a/docs/examples/multi-double.c
+++ b/docs/examples/multi-double.c
@@ -57,7 +57,7 @@ int main(int argc, char **argv)
     fd_set fdread;
     fd_set fdwrite;
     fd_set fdexcep;
-    int maxfd;
+    int maxfd = -1;
 
     FD_ZERO(&fdread);
     FD_ZERO(&fdwrite);
@@ -71,8 +71,10 @@ int main(int argc, char **argv)
     curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
 
     /* In a real-world program you OF COURSE check the return code of the
-       function calls, *and* you make sure that maxfd is bigger than -1 so
-       that the call to select() below makes sense! */
+       function calls.  On success, the value of maxfd is guaranteed to be
+       greater or equal than than -1.  We call select(maxfd + 1, ...),
+       specially in case of (maxfd == -1), we call select(0, ...), which is
+       basically equal to sleep. */
 
     rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
 
diff --git a/docs/examples/multi-post.c b/docs/examples/multi-post.c
index 229e843..e01693e 100644
--- a/docs/examples/multi-post.c
+++ b/docs/examples/multi-post.c
@@ -77,7 +77,7 @@ int main(int argc, char *argv[])
       fd_set fdread;
       fd_set fdwrite;
       fd_set fdexcep;
-      int maxfd;
+      int maxfd = -1;
 
       FD_ZERO(&fdread);
       FD_ZERO(&fdwrite);
@@ -91,8 +91,10 @@ int main(int argc, char *argv[])
       curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
 
       /* In a real-world program you OF COURSE check the return code of the
-         function calls, *and* you make sure that maxfd is bigger than -1
-         so that the call to select() below makes sense! */
+         function calls.  On success, the value of maxfd is guaranteed to be
+         greater or equal than than -1.  We call select(maxfd + 1, ...),
+         specially in case of (maxfd == -1), we call select(0, ...), which is
+         basically equal to sleep. */
 
       rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
 
diff --git a/docs/examples/multi-single.c b/docs/examples/multi-single.c
index 3e236f3..cc3b6c8 100644
--- a/docs/examples/multi-single.c
+++ b/docs/examples/multi-single.c
@@ -51,7 +51,7 @@ int main(int argc, char **argv)
     fd_set fdread;
     fd_set fdwrite;
     fd_set fdexcep;
-    int maxfd;
+    int maxfd = -1;
 
     FD_ZERO(&fdread);
     FD_ZERO(&fdwrite);
@@ -65,8 +65,10 @@ int main(int argc, char **argv)
     curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
 
     /* In a real-world program you OF COURSE check the return code of the
-       function calls, *and* you make sure that maxfd is bigger than -1 so
-       that the call to select() below makes sense! */
+       function calls.  On success, the value of maxfd is guaranteed to be
+       greater or equal than than -1.  We call select(maxfd + 1, ...),
+       specially in case of (maxfd == -1), we call select(0, ...), which is
+       basically equal to sleep. */
 
     rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
 
diff --git a/tests/libtest/lib536.c b/tests/libtest/lib536.c
index 04bc696..158b3a1 100644
--- a/tests/libtest/lib536.c
+++ b/tests/libtest/lib536.c
@@ -21,7 +21,7 @@
 
 static CURLMcode perform(CURLM * multi)
 {
-  int handles, maxfd;
+  int handles;
   CURLMcode code;
   fd_set fdread, fdwrite, fdexcep;
   struct timeval mp_start;
@@ -31,6 +31,9 @@ static CURLMcode perform(CURLM * multi)
   mp_start = tutil_tvnow();
 
   for (;;) {
+    static struct timeval timeout = /* 100 ms */ { 0, 100000L };
+    int maxfd = -1;
+
     code = curl_multi_perform(multi, &handles);
     if (tutil_tvdiff(tutil_tvnow(), mp_start) >
         MULTI_PERFORM_HANG_TIMEOUT) {
@@ -53,9 +56,14 @@ static CURLMcode perform(CURLM * multi)
     FD_ZERO(&fdwrite);
     FD_ZERO(&fdexcep);
     curl_multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
-    if (maxfd < 0)
-      return (CURLMcode) ~CURLM_OK;
-    if (select(maxfd + 1, &fdread, &fdwrite, &fdexcep, 0) == -1)
+
+    /* In a real-world program you OF COURSE check the return code of the
+       function calls.  On success, the value of maxfd is guaranteed to be
+       greater or equal than than -1.  We call select(maxfd + 1, ...),
+       specially in case of (maxfd == -1), we call select(0, ...), which is
+       basically equal to sleep. */
+
+    if (select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout) == -1)
       return (CURLMcode) ~CURLM_OK;
   }
 
-- 
1.7.0.2

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html

Reply via email to