]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
tests/server: sync `wait_ms()` with the libcurl implementation
authorViktor Szakats <commit@vsz.me>
Sat, 8 Mar 2025 21:15:07 +0000 (22:15 +0100)
committerViktor Szakats <commit@vsz.me>
Sun, 9 Mar 2025 11:04:30 +0000 (12:04 +0100)
It contains a series of bugfixes and updates applied to libcurl's
`Curl_wait_ms()` over the years, but missed from the copy in
`tests/server/util.c`:
d65321f93916e60f65b89d9bcb502341ea5c5939,
  52e822173aa3cd4f610531d32fbf943f026cdca6,
  5912da253b64de3cb2a1a229558077219b2c8a35
4a8f459837e2b7dc146825fc9a864045f7d1ae4a
1ad49feb71418f26aa6114c7a20ce1463beb3ea9

It fixes `wait_ms()` to check for, and return `SOCKERRNO`. Fixing error
handling on Windows.

Also:
- tests/server: change callers to check `SOCKERRNO`.
- `wait_ms()`: fix to check for the correct error code on Windows.
  Pending for `Curl_wait_ms()`: #16621.
- `Curl_wait_ms()`: tidy-up `Sleep()` argument cast (nit).
- lib/curl_trc: drop an unused header.

Closes #16627

lib/curl_trc.c
lib/select.c
tests/server/mqttd.c
tests/server/rtspd.c
tests/server/sockfilt.c
tests/server/socksd.c
tests/server/sws.c
tests/server/util.c
tests/server/util.h

index ea74964e65cd84029fec9369173811f1e1f25360..ffad3e45b9718f475163f5bdc4812aeabba34424 100644 (file)
@@ -30,7 +30,6 @@
 #include "urldata.h"
 #include "easyif.h"
 #include "cfilters.h"
-#include "timeval.h"
 #include "multiif.h"
 #include "strcase.h"
 
index 7b52f55d8fd9417c5ae17bbef7b9fc65085ad4a3..a7e9ce53ac2f7744c70e61f3a901ef8b23867e85 100644 (file)
@@ -86,7 +86,7 @@ int Curl_wait_ms(timediff_t timeout_ms)
     timeout_ms = ULONG_MAX-1;
     /* do not use ULONG_MAX, because that is equal to INFINITE */
 #endif
-  Sleep((ULONG)timeout_ms);
+  Sleep((DWORD)timeout_ms);
 #else
   /* avoid using poll() for this since it behaves incorrectly with no sockets
      on Apple operating systems */
index 11355f95e141cef4e9c24efecb493c466cf84847..abf4c9714eac76f6fd559b29c5617b041c913603 100644 (file)
@@ -762,9 +762,9 @@ static curl_socket_t mqttd_sockdaemon(curl_socket_t sock,
         rc = wait_ms(delay);
         if(rc) {
           /* should not happen */
-          error = errno;
+          error = SOCKERRNO;
           logmsg("wait_ms() failed with error (%d) %s",
-                 error, strerror(error));
+                 error, sstrerror(error));
           sclose(sock);
           return CURL_SOCKET_BAD;
         }
index 9389c3cf700fc6076789e9bcd7307c54d0d156d6..73e83f1589534eb1ec717e59310433e0d4db9a93 100644 (file)
@@ -982,9 +982,9 @@ static int rtspd_send_doc(curl_socket_t sock, struct rtspd_httprequest *req)
               break;
             if(res) {
               /* should not happen */
-              error = errno;
+              error = SOCKERRNO;
               logmsg("wait_ms() failed with error (%d) %s",
-                     error, strerror(error));
+                     error, sstrerror(error));
               break;
             }
           }
index a0ef93bbd864bcd34f7cc49805956810e4398954..305065c10e95c58cc903b9fecee4c3343fa092fc 100644 (file)
@@ -1253,9 +1253,9 @@ static curl_socket_t sockfilt_sockdaemon(curl_socket_t sock,
         rc = wait_ms(delay);
         if(rc) {
           /* should not happen */
-          error = errno;
+          error = SOCKERRNO;
           logmsg("wait_ms() failed with error (%d) %s",
-                 error, strerror(error));
+                 error, sstrerror(error));
           sclose(sock);
           return CURL_SOCKET_BAD;
         }
index 4d45a44b5528f227770a4d3060600c99a02d2164..ffa2e6eb9e68bca29cd11f8019d08a4c56055c4a 100644 (file)
@@ -790,9 +790,9 @@ static curl_socket_t socksd_sockdaemon(curl_socket_t sock,
         rc = wait_ms(delay);
         if(rc) {
           /* should not happen */
-          error = errno;
+          error = SOCKERRNO;
           logmsg("wait_ms() failed with error (%d) %s",
-                 error, strerror(error));
+                 error, sstrerror(error));
           sclose(sock);
           return CURL_SOCKET_BAD;
         }
index 9c2821884ff8a0b9a390bf0b664c7e71e9a1984f..88d549fa6ab3d0cf911f69a0bc69841e8bfe2828 100644 (file)
@@ -1221,9 +1221,9 @@ retry:
             res = wait_ms(250);
             if(res) {
               /* should not happen */
-              error = errno;
+              error = SOCKERRNO;
               logmsg("wait_ms() failed with error (%d) %s",
-                     error, strerror(error));
+                     error, sstrerror(error));
               break;
             }
           }
index 8f2da0aefbe46cbb9652816413134003f06faced..fcde5d4c184b24b85b9f0467da8dfc07e9d520da 100644 (file)
@@ -48,7 +48,6 @@
 #include "curlx.h" /* from the private lib dir */
 #include "getpart.h"
 #include "util.h"
-#include "timediff.h"
 
 /* need init from main() */
 const char *pidname = NULL;
@@ -248,19 +247,6 @@ FILE *test2fopen(long testno, const char *logdir2)
   return stream;
 }
 
-#if !defined(MSDOS) && !defined(USE_WINSOCK)
-static long timediff(struct timeval newer, struct timeval older)
-{
-  timediff_t diff = newer.tv_sec-older.tv_sec;
-  if(diff >= (LONG_MAX/1000))
-    return LONG_MAX;
-  else if(diff <= (LONG_MIN/1000))
-    return LONG_MIN;
-  return (long)(newer.tv_sec-older.tv_sec)*1000+
-    (long)(newer.tv_usec-older.tv_usec)/1000;
-}
-#endif
-
 /*
  * Portable function used for waiting a specific amount of ms.
  * Waiting indefinitely with this function is not allowed, a
@@ -270,45 +256,41 @@ static long timediff(struct timeval newer, struct timeval older)
  *   -1 = system call error, or invalid timeout value
  *    0 = specified timeout has elapsed
  */
-int wait_ms(int timeout_ms)
+int wait_ms(timediff_t timeout_ms)
 {
   int r = 0;
 
   if(!timeout_ms)
     return 0;
   if(timeout_ms < 0) {
-    CURL_SETERRNO(EINVAL);
+    SET_SOCKERRNO(SOCKEINVAL);
     return -1;
   }
-#ifdef MSDOS
-  delay(timeout_ms);
-#elif defined(USE_WINSOCK)
+#if defined(MSDOS)
+  delay((unsigned int)timeout_ms);
+#elif defined(_WIN32)
+  /* prevent overflow, timeout_ms is typecast to ULONG/DWORD. */
+#if TIMEDIFF_T_MAX >= ULONG_MAX
+  if(timeout_ms >= ULONG_MAX)
+    timeout_ms = ULONG_MAX-1;
+    /* do not use ULONG_MAX, because that is equal to INFINITE */
+#endif
   Sleep((DWORD)timeout_ms);
 #else
   /* avoid using poll() for this since it behaves incorrectly with no sockets
      on Apple operating systems */
   {
     struct timeval pending_tv;
-    struct timeval initial_tv = tvnow();
-    int pending_ms = timeout_ms;
-    do {
-      int error;
-      pending_tv.tv_sec = pending_ms / 1000;
-      pending_tv.tv_usec = (pending_ms % 1000) * 1000;
-      r = select(0, NULL, NULL, NULL, &pending_tv);
-      if(r != -1)
-        break;
-      error = errno;
-      if(error && (error != SOCKEINTR))
-        break;
-      pending_ms = timeout_ms - (int)timediff(tvnow(), initial_tv);
-      if(pending_ms <= 0)
-        break;
-    } while(r == -1);
+    r = select(0, NULL, NULL, NULL, curlx_mstotv(&pending_tv, timeout_ms));
+  }
+#endif /* _WIN32 */
+  if(r) {
+    if((r == -1) && (SOCKERRNO == SOCKEINTR))
+      /* make EINTR from select or poll not a "lethal" error */
+      r = 0;
+    else
+      r = -1;
   }
-#endif /* USE_WINSOCK */
-  if(r)
-    r = -1;
   return r;
 }
 
index d2a00e663214cf50a1dea6d84040c77bf58a81ff..5c7be488cc8a726cc784c9b89cb51b39773f2143 100644 (file)
@@ -87,7 +87,9 @@ const char *sstrerror(int err);
 /* fopens the test case file */
 FILE *test2fopen(long testno, const char *logdir);
 
-int wait_ms(int timeout_ms);
+#include "timediff.h"
+
+int wait_ms(timediff_t timeout_ms);
 curl_off_t our_getpid(void);
 int write_pidfile(const char *filename);
 int write_portfile(const char *filename, int port);