libtests: replace atoi() with curlx_str_number()

Also:
- lib1568: fail in global initialization error.

Closes #19506
This commit is contained in:
Viktor Szakats 2025-11-13 03:40:42 +01:00
parent e496bcfd0a
commit 4415e865ad
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
10 changed files with 85 additions and 42 deletions

View 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 \

View 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))

View 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))

View 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");

View 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);

View file

@ -27,9 +27,15 @@
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;

View 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");

View file

@ -27,23 +27,27 @@
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);

View file

@ -35,12 +35,16 @@
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);

View 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);