From: Viktor Szakats Date: Thu, 13 Nov 2025 11:46:59 +0000 (+0100) Subject: tests/server: replace `atoi()` and `atol()` with `curlx_str_number()` X-Git-Tag: rc-8_18_0-1~311 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bb1391f94394e635c1a5c58253e7a3d3b36bde57;p=thirdparty%2Fcurl.git tests/server: replace `atoi()` and `atol()` with `curlx_str_number()` Closes #19510 --- diff --git a/tests/server/dnsd.c b/tests/server/dnsd.c index 93edcb2397..c541b05295 100644 --- a/tests/server/dnsd.c +++ b/tests/server/dnsd.c @@ -399,6 +399,8 @@ static int test_dnsd(int argc, char **argv) serverlogslocked = 0; while(argc > arg) { + const char *opt; + curl_off_t num; if(!strcmp("--verbose", argv[arg])) { arg++; /* nothing yet */ @@ -450,7 +452,9 @@ static int test_dnsd(int argc, char **argv) else if(!strcmp("--port", argv[arg])) { arg++; if(argc > arg) { - port = (unsigned short)atoi(argv[arg]); + opt = argv[arg]; + if(!curlx_str_number(&opt, &num, 0xffff)) + port = (unsigned short)num; arg++; } } diff --git a/tests/server/first.h b/tests/server/first.h index 8e69a2b976..3a7255ccb8 100644 --- a/tests/server/first.h +++ b/tests/server/first.h @@ -128,7 +128,6 @@ extern int getpart(char **outbuf, size_t *outlen, extern char *data_to_hex(char *data, size_t len); extern void logmsg(const char *msg, ...); extern void loghex(unsigned char *buffer, ssize_t len); -extern unsigned char byteval(char *value); extern int win32_init(void); extern FILE *test2fopen(long testno, const char *logdir2); extern curl_off_t our_getpid(void); diff --git a/tests/server/mqttd.c b/tests/server/mqttd.c index acb427a37c..c99a0e8084 100644 --- a/tests/server/mqttd.c +++ b/tests/server/mqttd.c @@ -81,10 +81,15 @@ static void mqttd_getconfig(void) while(fgets(buffer, sizeof(buffer), fp)) { char key[32]; char value[32]; + const char *pval; + curl_off_t num; if(sscanf(buffer, "%31s %31s", key, value) == 2) { if(!strcmp(key, "version")) { - m_config.version = byteval(value); - logmsg("version [%d] set", m_config.version); + pval = value; + if(!curlx_str_number(&pval, &num, 0xff)) { + m_config.version = (unsigned char)num; + logmsg("version [%d] set", m_config.version); + } } else if(!strcmp(key, "PUBLISH-before-SUBACK")) { logmsg("PUBLISH-before-SUBACK set"); @@ -95,12 +100,18 @@ static void mqttd_getconfig(void) m_config.short_publish = TRUE; } else if(!strcmp(key, "error-CONNACK")) { - m_config.error_connack = byteval(value); - logmsg("error-CONNACK = %d", m_config.error_connack); + pval = value; + if(!curlx_str_number(&pval, &num, 0xff)) { + m_config.error_connack = (unsigned char)num; + logmsg("error-CONNACK = %d", m_config.error_connack); + } } else if(!strcmp(key, "Testnum")) { - m_config.testnum = atoi(value); - logmsg("testnum = %d", m_config.testnum); + pval = value; + if(!curlx_str_number(&pval, &num, INT_MAX)) { + m_config.testnum = (int)num; + logmsg("testnum = %d", m_config.testnum); + } } else if(!strcmp(key, "excessive-remaining")) { logmsg("excessive-remaining set"); @@ -734,6 +745,8 @@ static int test_mqttd(int argc, char *argv[]) server_port = 1883; /* MQTT default port */ while(argc > arg) { + const char *opt; + curl_off_t num; if(!strcmp("--version", argv[arg])) { printf("mqttd IPv4%s\n", #ifdef USE_IPV6 @@ -787,13 +800,13 @@ static int test_mqttd(int argc, char *argv[]) else if(!strcmp("--port", argv[arg])) { arg++; if(argc > arg) { - int inum = atoi(argv[arg]); - if(inum && ((inum < 1025) || (inum > 65535))) { + opt = argv[arg]; + if(curlx_str_number(&opt, &num, 0xffff) || num < 1025) { fprintf(stderr, "mqttd: invalid --port argument (%s)\n", argv[arg]); return 0; } - server_port = (unsigned short)inum; + server_port = (unsigned short)num; arg++; } } diff --git a/tests/server/rtspd.c b/tests/server/rtspd.c index d13b8ff9a9..c3d4678ae4 100644 --- a/tests/server/rtspd.c +++ b/tests/server/rtspd.c @@ -165,6 +165,8 @@ static int rtspd_ProcessRequest(struct rtspd_httprequest *req) &prot_major, &prot_minor) == 5) { char *ptr; + const char *pval; + curl_off_t testnum; if(!strcmp(prot_str, "HTTP")) { req->protocol = RPROT_HTTP; @@ -211,7 +213,14 @@ static int rtspd_ProcessRequest(struct rtspd_httprequest *req) while(*ptr && !ISDIGIT(*ptr)) ptr++; - req->testno = atol(ptr); + pval = ptr; + if(!curlx_str_number(&pval, &testnum, INT_MAX)) + req->testno = (long)testnum; + else { + req->protocol = RPROT_NONE; + logmsg("rtspd: failed to read the test number from '%s'", doc); + return 1; + } if(req->testno > 10000) { req->partno = req->testno % 10000; @@ -359,8 +368,13 @@ static int rtspd_ProcessRequest(struct rtspd_httprequest *req) /* if the host name starts with test, the port number used in the CONNECT line will be used as test number! */ char *portp = strchr(doc, ':'); - if(portp && (*(portp + 1) != '\0') && ISDIGIT(*(portp + 1))) - req->testno = atol(portp + 1); + if(portp && (*(portp + 1) != '\0') && ISDIGIT(*(portp + 1))) { + pval = portp + 1; + if(!curlx_str_number(&pval, &testnum, INT_MAX)) + req->testno = (long)testnum; + else + req->testno = DOCNUMBER_CONNECT; + } else req->testno = DOCNUMBER_CONNECT; } @@ -1006,6 +1020,8 @@ static int test_rtspd(int argc, char *argv[]) serverlogslocked = 0; while(argc > arg) { + const char *opt; + curl_off_t num; if(!strcmp("--version", argv[arg])) { printf("rtspd IPv4%s" "\n" @@ -1055,7 +1071,9 @@ static int test_rtspd(int argc, char *argv[]) else if(!strcmp("--port", argv[arg])) { arg++; if(argc > arg) { - port = (unsigned short)atol(argv[arg]); + opt = argv[arg]; + if(!curlx_str_number(&opt, &num, 0xffff)) + port = (unsigned short)num; arg++; } } diff --git a/tests/server/sockfilt.c b/tests/server/sockfilt.c index 48728bfd01..91ebb7c9a1 100644 --- a/tests/server/sockfilt.c +++ b/tests/server/sockfilt.c @@ -1240,6 +1240,8 @@ static int test_sockfilt(int argc, char *argv[]) server_port = 8999; while(argc > arg) { + const char *opt; + curl_off_t num; if(!strcmp("--version", argv[arg])) { printf("sockfilt IPv4%s\n", #ifdef USE_IPV6 @@ -1291,7 +1293,9 @@ static int test_sockfilt(int argc, char *argv[]) else if(!strcmp("--port", argv[arg])) { arg++; if(argc > arg) { - server_port = (unsigned short)atol(argv[arg]); + opt = argv[arg]; + if(!curlx_str_number(&opt, &num, 0xffff)) + server_port = (unsigned short)num; arg++; } } @@ -1300,13 +1304,13 @@ static int test_sockfilt(int argc, char *argv[]) doing a passive server-style listening. */ arg++; if(argc > arg) { - int inum = atoi(argv[arg]); - if(inum && ((inum < 1025) || (inum > 65535))) { + opt = argv[arg]; + if(curlx_str_number(&opt, &num, 0xffff) || num < 1025) { fprintf(stderr, "sockfilt: invalid --connect argument (%s)\n", argv[arg]); return 0; } - server_connectport = (unsigned short)inum; + server_connectport = (unsigned short)num; arg++; } } diff --git a/tests/server/socksd.c b/tests/server/socksd.c index 1f2460ec29..f14ee13ee4 100644 --- a/tests/server/socksd.c +++ b/tests/server/socksd.c @@ -106,12 +106,6 @@ static void socksd_resetdefaults(void) strcpy(s_config.password, "password"); } -static unsigned short shortval(char *value) -{ - unsigned long num = (unsigned long)atol(value); - return num & 0xffff; -} - static void socksd_getconfig(void) { FILE *fp = fopen(configfile, FOPEN_READTEXT); @@ -122,26 +116,40 @@ static void socksd_getconfig(void) while(fgets(buffer, sizeof(buffer), fp)) { char key[32]; char value[260]; + const char *pval; + curl_off_t num; if(sscanf(buffer, "%31s %259s", key, value) == 2) { if(!strcmp(key, "version")) { - s_config.version = byteval(value); - logmsg("version [%d] set", s_config.version); + pval = value; + if(!curlx_str_number(&pval, &num, 0xff)) { + s_config.version = (unsigned char)num; + logmsg("version [%d] set", s_config.version); + } } else if(!strcmp(key, "nmethods_min")) { - s_config.nmethods_min = byteval(value); - logmsg("nmethods_min [%d] set", s_config.nmethods_min); + pval = value; + if(!curlx_str_number(&pval, &num, 0xff)) { + s_config.nmethods_min = (unsigned char)num; + logmsg("nmethods_min [%d] set", s_config.nmethods_min); + } } else if(!strcmp(key, "nmethods_max")) { - s_config.nmethods_max = byteval(value); - logmsg("nmethods_max [%d] set", s_config.nmethods_max); + pval = value; + if(!curlx_str_number(&pval, &num, 0xff)) { + s_config.nmethods_max = (unsigned char)num; + logmsg("nmethods_max [%d] set", s_config.nmethods_max); + } } else if(!strcmp(key, "backend")) { strcpy(s_config.addr, value); logmsg("backend [%s] set", s_config.addr); } else if(!strcmp(key, "backendport")) { - s_config.port = shortval(value); - logmsg("backendport [%d] set", s_config.port); + pval = value; + if(!curlx_str_number(&pval, &num, 0xffff)) { + s_config.port = (unsigned short)num; + logmsg("backendport [%d] set", s_config.port); + } } else if(!strcmp(key, "user")) { strcpy(s_config.user, value); @@ -157,12 +165,18 @@ static void socksd_getconfig(void) o X'02' USERNAME/PASSWORD */ else if(!strcmp(key, "method")) { - s_config.responsemethod = byteval(value); - logmsg("method [%d] set", s_config.responsemethod); + pval = value; + if(!curlx_str_number(&pval, &num, 0xff)) { + s_config.responsemethod = (unsigned char)num; + logmsg("method [%d] set", s_config.responsemethod); + } } else if(!strcmp(key, "response")) { - s_config.connectrep = byteval(value); - logmsg("response [%d] set", s_config.connectrep); + pval = value; + if(!curlx_str_number(&pval, &num, 0xff)) { + s_config.connectrep = (unsigned char)num; + logmsg("response [%d] set", s_config.connectrep); + } } } } @@ -754,6 +768,8 @@ static int test_socksd(int argc, char *argv[]) server_port = 8905; while(argc > arg) { + const char *opt; + curl_off_t num; if(!strcmp("--version", argv[arg])) { printf("socksd IPv4%s\n", #ifdef USE_IPV6 @@ -786,8 +802,12 @@ static int test_socksd(int argc, char *argv[]) } else if(!strcmp("--backendport", argv[arg])) { arg++; - if(argc > arg) - backendport = (unsigned short)atoi(argv[arg++]); + if(argc > arg) { + opt = argv[arg]; + if(!curlx_str_number(&opt, &num, 0xffff)) + backendport = (unsigned short)num; + arg++; + } } else if(!strcmp("--logfile", argv[arg])) { arg++; @@ -834,7 +854,9 @@ static int test_socksd(int argc, char *argv[]) else if(!strcmp("--port", argv[arg])) { arg++; if(argc > arg) { - server_port = (unsigned short)atol(argv[arg]); + opt = argv[arg]; + if(!curlx_str_number(&opt, &num, 0xffff)) + server_port = (unsigned short)num; arg++; } } diff --git a/tests/server/sws.c b/tests/server/sws.c index caa5605534..ab73ec3369 100644 --- a/tests/server/sws.c +++ b/tests/server/sws.c @@ -308,6 +308,8 @@ static int sws_ProcessRequest(struct sws_httprequest *req) int prot_major = 0; int prot_minor = 0; char *end = strstr(line, end_of_headers); + const char *pval; + curl_off_t num; req->callcount++; @@ -391,7 +393,9 @@ static int sws_ProcessRequest(struct sws_httprequest *req) ptr++; /* skip the slash */ - req->testno = atol(ptr); + pval = ptr; + if(!curlx_str_number(&pval, &num, INT_MAX)) + req->testno = (long)num; if(req->testno > 10000) { req->partno = req->testno % 10000; @@ -450,12 +454,12 @@ static int sws_ProcessRequest(struct sws_httprequest *req) portp = strchr(doc, ':'); if(portp && (*(portp + 1) != '\0') && ISDIGIT(*(portp + 1))) { - int inum = atoi(portp + 1); - if((inum <= 0) || (inum > 65535)) + pval = portp + 1; + if(curlx_str_number(&pval, &num, 0xffff) || + (num <= 0) || (num > 65535)) logmsg("Invalid CONNECT port received"); else - req->connect_port = (unsigned short)inum; - + req->connect_port = (unsigned short)num; } logmsg("Port number: %d, test case number: %ld", req->connect_port, req->testno); @@ -491,8 +495,13 @@ static int sws_ProcessRequest(struct sws_httprequest *req) /* check for a Testno: header with the test case number */ char *testno = strstr(line, "\nTestno: "); if(testno) { - req->testno = atol(&testno[9]); - logmsg("Found test number %ld in Testno: header!", req->testno); + pval = &testno[9]; + if(!curlx_str_number(&pval, &num, INT_MAX)) { + req->testno = (long)num; + logmsg("Found test number %ld in Testno: header!", req->testno); + } + else + logmsg("No Testno: number"); } else { logmsg("No Testno: header"); @@ -517,7 +526,9 @@ static int sws_ProcessRequest(struct sws_httprequest *req) while(*ptr && !ISDIGIT(*ptr)) ptr++; - req->testno = atol(ptr); + pval = ptr; + if(!curlx_str_number(&pval, &num, INT_MAX)) + req->testno = (long)num; if(req->testno > 10000) { req->partno = req->testno % 10000; @@ -2005,6 +2016,8 @@ static int test_sws(int argc, char *argv[]) serverlogslocked = 0; while(argc > arg) { + const char *opt; + curl_off_t num; if(!strcmp("--version", argv[arg])) { puts("sws IPv4" #ifdef USE_IPV6 @@ -2082,13 +2095,13 @@ static int test_sws(int argc, char *argv[]) else if(!strcmp("--port", argv[arg])) { arg++; if(argc > arg) { - int inum = atoi(argv[arg]); - if(inum && ((inum < 1025) || (inum > 65535))) { + opt = argv[arg]; + if(curlx_str_number(&opt, &num, 0xffff) || num < 1025) { fprintf(stderr, "sws: invalid --port argument (%s)\n", argv[arg]); return 0; } - port = (unsigned short)inum; + port = (unsigned short)num; arg++; } } @@ -2102,13 +2115,13 @@ static int test_sws(int argc, char *argv[]) else if(!strcmp("--keepalive", argv[arg])) { arg++; if(argc > arg) { - int inum = atoi(argv[arg]); - if(inum && (inum > 65535)) { + opt = argv[arg]; + if(curlx_str_number(&opt, &num, 0xffff)) { fprintf(stderr, "sws: invalid --keepalive argument (%s), must " "be number of seconds\n", argv[arg]); return 0; } - keepalive_secs = (unsigned short)inum; + keepalive_secs = (unsigned short)num; arg++; } } diff --git a/tests/server/tftpd.c b/tests/server/tftpd.c index 31ae5c8a39..819f06bf2d 100644 --- a/tests/server/tftpd.c +++ b/tests/server/tftpd.c @@ -561,6 +561,8 @@ static int test_tftpd(int argc, char **argv) serverlogslocked = 0; while(argc > arg) { + const char *opt; + curl_off_t num; if(!strcmp("--version", argv[arg])) { printf("tftpd IPv4%s\n", #ifdef USE_IPV6 @@ -608,7 +610,9 @@ static int test_tftpd(int argc, char **argv) else if(!strcmp("--port", argv[arg])) { arg++; if(argc > arg) { - port = (unsigned short)atol(argv[arg]); + opt = argv[arg]; + if(!curlx_str_number(&opt, &num, 0xffff)) + port = (unsigned short)num; arg++; } } @@ -1095,6 +1099,8 @@ static int validate_access(struct testcase *test, char partbuf[80]="data"; long partno; long testno; + const char *pval; + curl_off_t num; FILE *stream; ptr++; /* skip the slash */ @@ -1104,7 +1110,13 @@ static int validate_access(struct testcase *test, ptr++; /* get the number */ - testno = atol(ptr); + pval = ptr; + if(!curlx_str_number(&pval, &num, INT_MAX)) + testno = (long)num; + else { + logmsg("tftpd: failed to read the test number from '%s'", filename); + return TFTP_EACCESS; + } if(testno > 10000) { partno = testno % 10000; diff --git a/tests/server/util.c b/tests/server/util.c index 0e34f4ab21..6de8000327 100644 --- a/tests/server/util.c +++ b/tests/server/util.c @@ -132,12 +132,6 @@ void logmsg(const char *msg, ...) } } -unsigned char byteval(char *value) -{ - unsigned int num = (unsigned int)atoi(value); - return num & 0xff; -} - #ifdef _WIN32 /* use instead of perror() on generic Windows */ static void win32_perror(const char *msg)