From 29ed1f9834047887a232423e715b0f2b72d744e9 Mon Sep 17 00:00:00 2001 From: Viktor Szakats Date: Tue, 11 Mar 2025 11:09:43 +0100 Subject: [PATCH] tests/server: use `curlx_str_numblanks()` to avoid `errno` Replacing `strtoul()` calls and glue code. Closes #16671 --- lib/strparse.h | 1 + tests/server/rtspd.c | 18 ++++++------------ tests/server/sws.c | 18 ++++++------------ 3 files changed, 13 insertions(+), 24 deletions(-) diff --git a/lib/strparse.h b/lib/strparse.h index 739ea31a17..ebad485259 100644 --- a/lib/strparse.h +++ b/lib/strparse.h @@ -106,5 +106,6 @@ void Curl_str_passblanks(const char **linep); #define curlx_str_octal(x,y,z) Curl_str_octal(x,y,z) #define curlx_str_single(x,y) Curl_str_single(x,y) #define curlx_str_passblanks(x) Curl_str_passblanks(x) +#define curlx_str_numblanks(x,y) Curl_str_numblanks(x,y) #endif /* HEADER_CURL_STRPARSE_H */ diff --git a/tests/server/rtspd.c b/tests/server/rtspd.c index 73e83f1589..b8a066c43b 100644 --- a/tests/server/rtspd.c +++ b/tests/server/rtspd.c @@ -436,23 +436,17 @@ static int rtspd_ProcessRequest(struct rtspd_httprequest *req) request including the body before we return. If we've been told to ignore the content-length, we will return as soon as all headers have been received */ - char *endptr; - char *ptr = line + 15; - unsigned long clen = 0; - while(*ptr && ISSPACE(*ptr)) - ptr++; - endptr = ptr; - CURL_SETERRNO(0); - clen = strtoul(ptr, &endptr, 10); - if((ptr == endptr) || !ISSPACE(*endptr) || (ERANGE == errno)) { + curl_off_t clen; + const char *p = line + strlen("Content-Length:"); + if(curlx_str_numblanks(&p, &clen)) { /* this assumes that a zero Content-Length is valid */ - logmsg("Found invalid Content-Length: (%s) in the request", ptr); + logmsg("Found invalid '%s' in the request", line); req->open = FALSE; /* closes connection */ return 1; /* done */ } - req->cl = clen - req->skip; + req->cl = (size_t)clen - req->skip; - logmsg("Found Content-Length: %lu in the request", clen); + logmsg("Found Content-Length: %zu in the request", (size_t)clen); if(req->skip) logmsg("... but will abort after %zu bytes", req->cl); break; diff --git a/tests/server/sws.c b/tests/server/sws.c index fcc0716038..5a1d46a07b 100644 --- a/tests/server/sws.c +++ b/tests/server/sws.c @@ -588,26 +588,20 @@ static int sws_ProcessRequest(struct sws_httprequest *req) request including the body before we return. If we've been told to ignore the content-length, we will return as soon as all headers have been received */ - char *endptr; - char *ptr = line + 15; - unsigned long clen = 0; - while(*ptr && ISSPACE(*ptr)) - ptr++; - endptr = ptr; - CURL_SETERRNO(0); - clen = strtoul(ptr, &endptr, 10); - if((ptr == endptr) || !ISSPACE(*endptr) || (ERANGE == errno)) { + curl_off_t clen; + const char *p = line + strlen("Content-Length:"); + if(curlx_str_numblanks(&p, &clen)) { /* this assumes that a zero Content-Length is valid */ - logmsg("Found invalid Content-Length: (%s) in the request", ptr); + logmsg("Found invalid '%s' in the request", line); req->open = FALSE; /* closes connection */ return 1; /* done */ } if(req->skipall) req->cl = 0; else - req->cl = clen - req->skip; + req->cl = (size_t)clen - req->skip; - logmsg("Found Content-Length: %lu in the request", clen); + logmsg("Found Content-Length: %zu in the request", (size_t)clen); if(req->skip) logmsg("... but will abort after %zu bytes", req->cl); } -- 2.47.3