]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
libtests: replace `atoi()` with `curlx_str_number()`
authorViktor Szakats <commit@vsz.me>
Thu, 13 Nov 2025 02:40:42 +0000 (03:40 +0100)
committerViktor Szakats <commit@vsz.me>
Thu, 13 Nov 2025 10:41:12 +0000 (11:41 +0100)
Also:
- lib1568: fail in global initialization error.

Closes #19506

tests/libtest/Makefile.inc
tests/libtest/cli_hx_download.c
tests/libtest/cli_hx_upload.c
tests/libtest/cli_ws_data.c
tests/libtest/first.c
tests/libtest/lib1568.c
tests/libtest/lib1960.c
tests/libtest/lib521.c
tests/libtest/lib562.c
tests/libtest/lib591.c

index 67ffabad0b3d31302e1098570bebf581c3e0cf70..8efeba914df8418eeb699670d3d9be1ff5b188e3 100644 (file)
@@ -38,6 +38,7 @@ CURLX_C = \
   ../../lib/curlx/fopen.c \
   ../../lib/curlx/multibyte.c \
   ../../lib/curlx/strerr.c \
+  ../../lib/curlx/strparse.c \
   ../../lib/curlx/timediff.c \
   ../../lib/curlx/timeval.c \
   ../../lib/curlx/version_win32.c \
index b7ad5912a93ba1e6673816aa603d14ce50747c33..365348455a005b26f795dba7136d5fdb03c2dbeb 100644 (file)
@@ -302,6 +302,8 @@ static CURLcode test_cli_hx_download(const char *URL)
 
   while((ch = cgetopt(test_argc, test_argv, "aefhm:n:xA:F:M:P:r:T:V:"))
         != -1) {
+    const char *opt = coptarg;
+    curl_off_t num;
     switch(ch) {
     case 'h':
       usage_hx_download(NULL);
@@ -317,32 +319,39 @@ static CURLcode test_cli_hx_download(const char *URL)
       forbid_reuse_d = 1;
       break;
     case 'm':
-      max_parallel = (size_t)atol(coptarg);
+      if(!curlx_str_number(&opt, &num, LONG_MAX))
+        max_parallel = (size_t)num;
       break;
     case 'n':
-      transfer_count_d = (size_t)atol(coptarg);
+      if(!curlx_str_number(&opt, &num, LONG_MAX))
+        transfer_count_d = (size_t)num;
       break;
     case 'x':
       fresh_connect = 1;
       break;
     case 'A':
-      abort_offset = (size_t)atol(coptarg);
+      if(!curlx_str_number(&opt, &num, LONG_MAX))
+        abort_offset = (size_t)num;
       break;
     case 'F':
-      fail_offset = (size_t)atol(coptarg);
+      if(!curlx_str_number(&opt, &num, LONG_MAX))
+        fail_offset = (size_t)num;
       break;
     case 'M':
-      max_host_conns = (size_t)atol(coptarg);
+      if(!curlx_str_number(&opt, &num, LONG_MAX))
+        max_host_conns = (size_t)num;
       break;
     case 'P':
-      pause_offset = (size_t)atol(coptarg);
+      if(!curlx_str_number(&opt, &num, LONG_MAX))
+        pause_offset = (size_t)num;
       break;
     case 'r':
       free(resolve);
       resolve = strdup(coptarg);
       break;
     case 'T':
-      max_total_conns = (size_t)atol(coptarg);
+      if(!curlx_str_number(&opt, &num, LONG_MAX))
+        max_total_conns = (size_t)num;
       break;
     case 'V': {
       if(!strcmp("http/1.1", coptarg))
index 5069bcab636ec7d6ef3386a881356d6c82d3f654..dc053ae755bdbb21b1080d5ee1ab50144f2e6df2 100644 (file)
@@ -252,6 +252,8 @@ static CURLcode test_cli_hx_upload(const char *URL)
 
   while((ch = cgetopt(test_argc, test_argv, "aefhlm:n:A:F:M:P:r:RS:V:"))
         != -1) {
+    const char *opt = coptarg;
+    curl_off_t num;
     switch(ch) {
     case 'h':
       usage_hx_upload(NULL);
@@ -269,22 +271,27 @@ static CURLcode test_cli_hx_upload(const char *URL)
       announce_length = 1;
       break;
     case 'm':
-      max_parallel = (size_t)atol(coptarg);
+      if(!curlx_str_number(&opt, &num, LONG_MAX))
+        max_parallel = (size_t)num;
       break;
     case 'n':
-      transfer_count_u = (size_t)atol(coptarg);
+      if(!curlx_str_number(&opt, &num, LONG_MAX))
+        transfer_count_u = (size_t)num;
       break;
     case 'A':
-      abort_offset = (size_t)atol(coptarg);
+      if(!curlx_str_number(&opt, &num, LONG_MAX))
+        abort_offset = (size_t)num;
       break;
     case 'F':
-      fail_offset = (size_t)atol(coptarg);
+      if(!curlx_str_number(&opt, &num, LONG_MAX))
+        fail_offset = (size_t)num;
       break;
     case 'M':
       method = coptarg;
       break;
     case 'P':
-      pause_offset = (size_t)atol(coptarg);
+      if(!curlx_str_number(&opt, &num, LONG_MAX))
+        pause_offset = (size_t)num;
       break;
     case 'r':
       resolve = coptarg;
@@ -293,7 +300,8 @@ static CURLcode test_cli_hx_upload(const char *URL)
       reuse_easy = 1;
       break;
     case 'S':
-      send_total = (size_t)atol(coptarg);
+      if(!curlx_str_number(&opt, &num, LONG_MAX))
+        send_total = (size_t)num;
       break;
     case 'V': {
       if(!strcmp("http/1.1", coptarg))
index 8b8b1fa0d1b8151143ab6380c6fee9d726ce209b..7e5479b74312927da5ee3a26f3ab811fc9b6db83 100644 (file)
@@ -419,6 +419,8 @@ static CURLcode test_cli_ws_data(const char *URL)
   (void)URL;
 
   while((ch = cgetopt(test_argc, test_argv, "12c:hm:M:")) != -1) {
+    const char *opt = coptarg;
+    curl_off_t num;
     switch(ch) {
     case '1':
       model = 1;
@@ -430,13 +432,16 @@ static CURLcode test_cli_ws_data(const char *URL)
       test_ws_data_usage(NULL);
       return CURLE_BAD_FUNCTION_ARGUMENT;
     case 'c':
-      count = (size_t)atol(coptarg);
+      if(!curlx_str_number(&opt, &num, LONG_MAX))
+        count = (size_t)num;
       break;
     case 'm':
-      plen_min = (size_t)atol(coptarg);
+      if(!curlx_str_number(&opt, &num, LONG_MAX))
+        plen_min = (size_t)num;
       break;
     case 'M':
-      plen_max = (size_t)atol(coptarg);
+      if(!curlx_str_number(&opt, &num, LONG_MAX))
+        plen_max = (size_t)num;
       break;
     default:
       test_ws_data_usage("invalid option");
index 79baca331ea9661ccceb31013f5c8a20c725e0ef..ad1f92997111bcd03e3f4ae11bd16a3d32101c1e 100644 (file)
@@ -127,7 +127,7 @@ int cgetopt(int argc, const char * const argv[], const char *optstring)
 #ifdef CURLDEBUG
 static void memory_tracking_init(void)
 {
-  char *env;
+  const char *env;
   /* if CURL_MEMDEBUG is set, this starts memory tracking message logging */
   env = getenv("CURL_MEMDEBUG");
   if(env) {
@@ -137,9 +137,9 @@ static void memory_tracking_init(void)
   /* if CURL_MEMLIMIT is set, this enables fail-on-alloc-number-N feature */
   env = getenv("CURL_MEMLIMIT");
   if(env) {
-    long num = atol(env);
-    if(num > 0)
-      curl_dbg_memlimit(num);
+    curl_off_t num;
+    if(!curlx_str_number(&env, &num, LONG_MAX) && num > 0)
+      curl_dbg_memlimit((long)num);
   }
 }
 #else
@@ -215,7 +215,7 @@ int main(int argc, const char **argv)
   CURLcode result;
   entry_func_t entry_func;
   const char *entry_name;
-  char *env;
+  const char *env;
   size_t tmp;
 
   CURLX_SET_BINMODE(stdout);
@@ -271,11 +271,13 @@ int main(int argc, const char **argv)
   if(argc > 5)
     libtest_arg4 = argv[5];
 
+  testnum = 0;
   env = getenv("CURL_TESTNUM");
-  if(env)
-    testnum = atoi(env);
-  else
-    testnum = 0;
+  if(env) {
+    curl_off_t num;
+    if(!curlx_str_number(&env, &num, INT_MAX) && num > 0)
+      testnum = (int)num;
+  }
 
   result = entry_func(URL);
   curl_mfprintf(stderr, "Test ended with result %d\n", result);
index 9e73029d510ab8ee9d5831240d75d20d6a0a8a69..a7c454edf754fba20938cef765fedfaf475754bc 100644 (file)
 
 static CURLcode test_lib1568(const char *URL)
 {
-  CURLcode ret;
+  CURLcode ret = TEST_ERR_MAJOR_BAD;
   CURL *curl;
-  curl_global_init(CURL_GLOBAL_ALL);
+  curl_off_t port;
+
+  if(curlx_str_number(&libtest_arg2, &port, 0xffff))
+    return ret;
+
+  if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK)
+    return ret;
 
   curl = curl_easy_init();
   curl_easy_setopt(curl, CURLOPT_URL, URL);
@@ -39,12 +45,11 @@ static CURLcode test_lib1568(const char *URL)
   curl_easy_setopt(curl, CURLOPT_USERAGENT, "lib1568");
   curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
   curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L);
-  curl_easy_setopt(curl, CURLOPT_PORT, atol(libtest_arg2));
+  curl_easy_setopt(curl, CURLOPT_PORT, (long)port);
 
   ret = curl_easy_perform(curl);
 
   curl_easy_cleanup(curl);
-  curl = NULL;
 
   curl_global_cleanup();
   return ret;
index 1ee9277bef62711dac3cf3a160a7360c1bb5cc34..152b1053f1333ff8c65b31edd7e4f7802271697a 100644 (file)
@@ -79,16 +79,17 @@ static CURLcode test_lib1960(const char *URL)
   int status;
   curl_socket_t client_fd = CURL_SOCKET_BAD;
   struct sockaddr_in serv_addr;
-  unsigned short port;
+  curl_off_t port;
 
   if(!strcmp("check", URL))
     return CURLE_OK; /* no output makes it not skipped */
 
-  port = (unsigned short)atoi(libtest_arg3);
+  if(curlx_str_number(&libtest_arg3, &port, 0xffff))
+    return res;
 
   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
     curl_mfprintf(stderr, "curl_global_init() failed\n");
-    return TEST_ERR_MAJOR_BAD;
+    return res;
   }
 
   /*
@@ -103,7 +104,7 @@ static CURLcode test_lib1960(const char *URL)
   }
 
   serv_addr.sin_family = AF_INET;
-  serv_addr.sin_port = htons(port);
+  serv_addr.sin_port = htons((unsigned short)port);
 
   if(my_inet_pton(AF_INET, libtest_arg2, &serv_addr.sin_addr) <= 0) {
     curl_mfprintf(stderr, "inet_pton failed\n");
index c02c45a3c103cccfd067af253b95c60b7c556df4..acfec703811faddeeabe760d96d843b9c9688c7f 100644 (file)
 
 static CURLcode test_lib521(const char *URL)
 {
-  CURLcode res;
+  CURLcode res = TEST_ERR_MAJOR_BAD;
   CURL *curl;
+  curl_off_t port;
+
+  if(curlx_str_number(&libtest_arg2, &port, 0xffff))
+    return res;
 
   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
     curl_mfprintf(stderr, "curl_global_init() failed\n");
-    return TEST_ERR_MAJOR_BAD;
+    return res;
   }
 
   curl = curl_easy_init();
   if(!curl) {
     curl_mfprintf(stderr, "curl_easy_init() failed\n");
     curl_global_cleanup();
-    return TEST_ERR_MAJOR_BAD;
+    return res;
   }
 
   test_setopt(curl, CURLOPT_URL, URL);
-  test_setopt(curl, CURLOPT_PORT, atol(libtest_arg2));
+  test_setopt(curl, CURLOPT_PORT, (long)port);
   test_setopt(curl, CURLOPT_USERPWD, "xxx:yyy");
   test_setopt(curl, CURLOPT_VERBOSE, 1L);
 
index 1c3bf8cff8371d41192208c078c30360535b7ce1..e9716e5768a4ec99b4117c24fe8b49ebfc15e522 100644 (file)
 
 static CURLcode test_lib562(const char *URL)
 {
+  CURLcode res = TEST_ERR_MAJOR_BAD;
   CURL *curl;
-  CURLcode res = CURLE_OK;
+  curl_off_t port;
+
+  if(curlx_str_number(&libtest_arg2, &port, 0xffff))
+    return res;
 
   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
     curl_mfprintf(stderr, "curl_global_init() failed\n");
-    return TEST_ERR_MAJOR_BAD;
+    return res;
   }
 
   /* get a curl handle */
@@ -48,14 +52,14 @@ static CURLcode test_lib562(const char *URL)
   if(!curl) {
     curl_mfprintf(stderr, "curl_easy_init() failed\n");
     curl_global_cleanup();
-    return TEST_ERR_MAJOR_BAD;
+    return res;
   }
 
   /* enable verbose */
   test_setopt(curl, CURLOPT_VERBOSE, 1L);
 
   /* set port number */
-  test_setopt(curl, CURLOPT_PORT, atol(libtest_arg2));
+  test_setopt(curl, CURLOPT_PORT, (long)port);
 
   /* specify target */
   test_setopt(curl, CURLOPT_URL, URL);
index 0b7a0b4e855effef2ea53d07408e98488b981fc8..c30bde5e8be4062b1207bd6dfb6d0b045c97e0b2 100644 (file)
@@ -36,6 +36,10 @@ static CURLcode test_lib591(const char *URL)
   int msgs_left;
   CURLMsg *msg;
   FILE *upload = NULL;
+  curl_off_t accept_timeout;
+
+  if(curlx_str_number(&libtest_arg2, &accept_timeout, 65535))
+    return TEST_ERR_MAJOR_BAD;
 
   start_test_timing();
 
@@ -72,7 +76,7 @@ static CURLcode test_lib591(const char *URL)
   easy_setopt(curl, CURLOPT_FTPPORT, "-");
 
   /* server connection timeout */
-  easy_setopt(curl, CURLOPT_ACCEPTTIMEOUT_MS, atol(libtest_arg2)*1000);
+  easy_setopt(curl, CURLOPT_ACCEPTTIMEOUT_MS, (long)(accept_timeout * 1000));
 
   multi_init(multi);