]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
multi: make max_total_* members size_t
authorStefan Eissing <stefan@eissing.org>
Thu, 20 Nov 2025 09:58:54 +0000 (10:58 +0100)
committerDaniel Stenberg <daniel@haxx.se>
Fri, 21 Nov 2025 14:57:26 +0000 (15:57 +0100)
Check size_t conversion on setting these members via CURLMIPT_*. Use
members without casting.

Closes #19618

lib/cshutdn.c
lib/curl_setup.h
lib/curlx/warnless.c
lib/curlx/warnless.h
lib/multi.c
lib/multihandle.h
lib/url.c

index f9f636640a035f0fbc995fb6439d7dfb88916710..e039d027625b096b2770e3f7b16de4af139bce69 100644 (file)
@@ -414,8 +414,7 @@ void Curl_cshutdn_add(struct cshutdn *cshutdn,
                       size_t conns_in_pool)
 {
   struct Curl_easy *data = cshutdn->multi->admin;
-  size_t max_total = (cshutdn->multi->max_total_connections > 0) ?
-                     (size_t)cshutdn->multi->max_total_connections : 0;
+  size_t max_total = cshutdn->multi->max_total_connections;
 
   /* Add the connection to our shutdown list for non-blocking shutdown
    * during multi processing. */
index 05e6149f6139142a4dcbabbca03c98023b99fc71..d75de9416cbf0ac3df476b2f43e52b1fceeaf525 100644 (file)
 #endif
 #endif
 
+#if SIZEOF_LONG > SIZEOF_SIZE_T
+#error "unexpected: 'long' is larger than 'size_t'"
+#endif
 /*
  * Arg 2 type for gethostname in case it has not been defined in config file.
  */
index 5ed27b3e4cbfcb0422a12786e9e29a88c4522137..fafa83c98c975230761332b3650ee914c87609e2 100644 (file)
@@ -333,6 +333,18 @@ bool curlx_sotouz_fits(curl_off_t sonum, size_t *puznum)
   return TRUE;
 }
 
+
+bool curlx_sltouz(long slnum, size_t *puznum)
+{
+  if(slnum < 0) {
+    *puznum = 0;
+    return FALSE;
+  }
+  /* We error in curl_setup.h if SIZEOF_LONG > SIZEOF_SIZE_T */
+  *puznum = (size_t)slnum;
+  return TRUE;
+}
+
 curl_off_t curlx_uztoso(size_t uznum)
 {
 #if SIZEOF_SIZE_T >= SIZEOF_CURL_OFF_T
index c8861d8488c3a29a07e2a533d644c1691bea517b..52e7ec2a5c5d25d30d7685f52332f1cad76779f2 100644 (file)
@@ -73,6 +73,10 @@ bool curlx_sztouz(ssize_t sznum, size_t *puznum);
  * too large and set 0 */
 bool curlx_sotouz_fits(curl_off_t sonum, size_t *puznum);
 
+/* Convert a long to size_t, return FALSE if negative or too large
+ * and set 0 */
+bool curlx_sltouz(long sznum, size_t *puznum);
+
 #ifdef _WIN32
 #undef  read
 #define read(fd, buf, count)  (ssize_t)_read(fd, buf, curlx_uztoui(count))
index c52ee54c77232b330c462dec6a08e5764d426d3d..3fc1c948f507a97e32eb1e8a978944f42eb358f5 100644 (file)
@@ -3271,10 +3271,12 @@ CURLMcode curl_multi_setopt(CURLM *m,
       multi->maxconnects = (unsigned int)uarg;
     break;
   case CURLMOPT_MAX_HOST_CONNECTIONS:
-    multi->max_host_connections = va_arg(param, long);
+    if(!curlx_sltouz(va_arg(param, long), &multi->max_host_connections))
+      res = CURLM_BAD_FUNCTION_ARGUMENT;
     break;
   case CURLMOPT_MAX_TOTAL_CONNECTIONS:
-    multi->max_total_connections = va_arg(param, long);
+    if(!curlx_sltouz(va_arg(param, long), &multi->max_total_connections))
+      res = CURLM_BAD_FUNCTION_ARGUMENT;
     break;
     /* options formerly used for pipelining */
   case CURLMOPT_MAX_PIPELINE_LENGTH:
index 69f977bb94ed3513427aefcb7594f0cccdd999de..fbb8bdfeeb5980f46dd38585489c8753f6036d5a 100644 (file)
@@ -149,11 +149,10 @@ struct Curl_multi {
   struct cshutdn cshutdn; /* connection shutdown handling */
   struct cpool cpool;     /* connection pool (bundles) */
 
-  long max_host_connections; /* if >0, a fixed limit of the maximum number
-                                of connections per host */
-
-  long max_total_connections; /* if >0, a fixed limit of the maximum number
-                                 of connections in total */
+  size_t max_host_connections; /* if >0, a fixed limit of the maximum number
+                                  of connections per host */
+  size_t max_total_connections; /* if >0, a fixed limit of the maximum number
+                                   of connections in total */
 
   /* timer callback and user data pointer for the *socket() API */
   curl_multi_timer_callback timer_cb;
index b4069e30f18bfc32ef4f841865a46109841af29c..7e4d455a8cd86b4fe0463e9b0e3e5b15134272c1 100644 (file)
--- a/lib/url.c
+++ b/lib/url.c
@@ -3690,7 +3690,7 @@ static CURLcode create_conn(struct Curl_easy *data,
           CURL_TRC_M(data, "Allowing sub-requests (like DoH) to override "
                      "max connection limit");
         else {
-          infof(data, "No connections available, total of %ld reached.",
+          infof(data, "No connections available, total of %zu reached.",
                 data->multi->max_total_connections);
           connections_available = FALSE;
         }