tests/server: replace atoi() and atol() with curlx_str_number()

Closes #19510
This commit is contained in:
Viktor Szakats 2025-11-13 12:46:59 +01:00
parent 833c429627
commit bb1391f943
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
9 changed files with 141 additions and 62 deletions

View file

@ -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++;
}
}

View file

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

View file

@ -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++;
}
}

View file

@ -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++;
}
}

View file

@ -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++;
}
}

View file

@ -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++;
}
}

View file

@ -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++;
}
}

View file

@ -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;

View file

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